When we see many application codes 70% of projects are not structured right way so it makes it difficult for easily maintainable. we could often see that all the operations for an action would have written in one service layer.
Best Practices are for making a project easily maintainable
example:
consider there is an application in which the user can register, edit his profile and read other user profile information.
register/edit are write operations
class UserWriteService{
final UserReadService userReadService;
@Autowire
public UserWriteService(UserReadService userReadService){
this.userReadService = userReadService;
}
public void createUser(////data......){
//create logic
}
public void edituser(long userId,//...data){
User user = userReadService.findUserById(userId);
//for obtained user object add the data.
}
}
Best Practices are for making a project easily maintainable
- Package Structure
- Having separate read and write service
example for package structure
com.example.demo.user.api
com.example.demo.user.service
com.example.demo.user.domian
com.example.demo.user.validation
com.example.demo.user.data
com.example.demo.user.exception
com.example.demo.account.api
com.example.demo.account.service
com.example.demo.account.domian
com.example.demo.account.validation
com.example.demo.account.data
com.example.demo.account.exception
api - > this package will have all the api end points associated with user
similary service package will have only services related with user
consider there is an application in which the user can register, edit his profile and read other user profile information.
register/edit are write operations
class UserWriteService{
final UserReadService userReadService;
@Autowire
public UserWriteService(UserReadService userReadService){
this.userReadService = userReadService;
}
public void createUser(////data......){
//create logic
}
public void edituser(long userId,//...data){
User user = userReadService.findUserById(userId);
//for obtained user object add the data.
}
}
No comments:
Post a Comment