Following this post https://stackoverflow.com/questions/21554977/should-services-always-return-dtos-or-can-they-also-return-domain-models and best practices in Software Arch suggestions by Martin Fowler https://martinfowler.com/eaaCatalog/serviceLayer.html#:~:text=A%20Service%20Layer%20defines%20an,the%20implementation%20of%20its%20operations
A Service Layer defines an application's boundary [Cockburn PloP] and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic
I have a problem doing so consider the following:
UserService {
UserDto findUser();
}
UserService should be fine if used in the controller where I just need data only so dto is sufficient,
But here is the problem if I used this service in another service say e.g CustomerService
I need the model itself the User
object since the model should be managed by some persistence context
e.g
CustomerService {
void addCustomer() {
Customer customer = new Customer();
User user = userService.findUser(xxx); // BAM compilation fails since findUser returns UserDto not User
customer.setUser(user);
}
}
What would be best practice here? Should I create 2 copies of findUser
method with 2 different return types or 2 copies of UserService
class one for controller use and other for service or core package use? Or should I consider the proxy pattern?