
Originally Posted by
rebornspirit
1. make your services that way that you return DTO or view objects or whatever. Problem with that is that you will be returning very specific objects. The problem starts when your second project needs to call that service and there goes the adjustments of the view object...
Perhaps you could solve this by introducing view modes which would determine which specific DTO class should be used.
Code:
class DTOFactory {
DTO createDTO(ModelObject modelObject, ViewMode viewMode) {
DTO dto = createDTO(viewMode.getDTOClass(modelObject));
dto.populateDTO(modelObject);
}
}
A given service would be invoked in the context of a viewMode and would use the factory (or factories) to create any DTO necesary.
This way if a different project needs a different view, a new DTO can be created and a new viewMode which would map that modelObject to that new DTO class and the specific population logic would be inside the new DTO so the previous (already working) code doesn't need to be touched. Of course, all this DTOs implementations implements a basic DTO interface, the downside of this is that each client of the service should cast the returned DTO to a specific implementation.
Also, if you had a DTO that is created from several model object there should be no problem since you could have an interface whose populateDTO method takes the necesary modelObjects.