I have a long experience in programming Java EE projects, but as I noticed most of the job I have done was with Transaction Script
(anti)pattern.
So I want to learn using a rich domain model, but then there is a question, shall Domain Object be depended on container/system services (such a persistence, mail, ...)?
I have read this article and I don't agree with it at all!
- How services are initialized and injected into domain entities?
- What about single responsibility paradigm?
- And isn't it against reusablity?
I also read Applying UML and Patterns fantastic book, but I don't know why didn't author describe the how shall responsibilities be assigned to entities after defining data access layer (in chapter 38).
So what is the best practice about having rich domain model, and do not having dependency on container/system services!
An example, consider following scenario in a software:
- There are agents (which work with the system)
- Our agents' business is selling insurance policies.
- Each insurance policy type has a special type of pre-printed card which is given to agents, and agents give them to insureds.
We need to track each agent's number of available cards.
So there is a use case, which records how many cards of each type is given to each agent.
- There is a use case, for recording agent's number of damaged unused cards of each type.
- And the cards are used as the agent sells insurance policies.
I believe Rich Domain Model suggests that I shall have a method as following in agent class:
class Agent {
int computeRemainigCards(CardType cardType) {}
}
But then Agent shall depend on 3 DAO classes for just one responsibility:
- One for calculating SUM of count of each card type given to an agent.
- One for calculating count of used cards (by selling insurance policies).
- One for calculating SUM of damaged unsold cards.
I believe that will make domain entities a mess. So what is the good practice?