Hi,
I am a recent "convert" from ejb2.x to Hibernate. I am all excited about how a true ORM tool like Hibernate can enable a "domain driven design" which basically encapsulates all domain data and behaviour within the domain objects (almost all of which are persistent object).
I just finished the book "Hibernate in Action" and tried to imbibe all design and implementation concepts taught in the book.
Now, there is a real "conflict of interest" between completely encapsulating business logic in Domain objects and avoiding dependence of domain objects on the underlying persistence framework so that they can be freely passed around among different layers.
In order to achieve this, we need the service layer to retrieve all persistent data required to execute all business rules using DAOs and pass it as arguments to the domain business objects.
This leads to partial leakage of business rule execution concern out of domain objects to the service layer.
For exapmple, I have a use case to modify an Operation for which I define a method Operation.modify(...). This method needs to check whether there is any other "duplicate operation" in the database. So, I need to pass number of "duplicate operations" to this business method. The service layer needs to call DAO to get the number of duplicate operations. Now, retrieving "duplicate operations" based on certain search criteria itself is a business rule, which leaks out of the domain model.
From this point of view, it would be better to evan retrieve the number of duplicate events within the business method, but this will make POJO dependent on DAO and hence the underlying persistence framwork.
So we have two design scnarios:
1. service layer->dao
|
POJO
pros: POJOS have no direct/indiect dependency on persistence framework.
cons: partial leakage of domain business rule execution to service layer
2. service layer -> pojo -> dao
pros: Domain logic completely encapsulated in domain objects.
cons: Although decoupled, pojos have run time dependency on the persistence framework.
Considering the tradeoff, I think scenario one is better if we want to pass around our pojos across different layers/tiers and scenario two is better if we have to use dtos (may be because our view object hierarchy does not at all match our domain object hierarchy) to pass data to the presentation layer, because in that case we need to deploy pojos only to the business layer/tier.
|