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?
5
Your question is exactly the question that is answered by reading Domain-Driven Design.
The comment from Andy is spot-on. The entities don’t depend on DAOs. They depend on abstractions that represent operations the entities need to function. The fact those abstractions represent data storage and are implemented using database is irrelevant to entities or any part of domain model. Those abstractions are called Repository in DDD.
To answer your questions:
- How services are initialized and injected into domain entities? – When entity is pulled from persistence, it is initialized with realizations of abstractions it needs, alongside the data of the entity.
- What about single responsibility paradigm? – What about it? The entity has only one responsibility : represent behavior related to single domain concept.
- And isn’t it against reusablity? – It is actually highly reusable. Especially if you apply other SOLID principles and use composition instead of inheritance.
For your example, that can be easily solved by Agent having reference to interface, that allows it to query cards in different way. The methods then call this interface.
1
What do you mean by But then Agent shall depend on 3 DAO classes for just one responsibility? If you’re talking about avoiding DAO call from domain classes, maybe you can design it like this:
class Agent {
List<Card> assignedCards;
int numberOfRemainingCards() {
return assignedCards.size();
}
void sell(Card card) {
// do something
assignedCards.remove(card);
}
void damaged(Card card) {
card.damaged = true;
assignedCards.remove(card);
}
}
class Card {
String id;
boolean damaged;
}
You only need one call to DAO (or repository) to retrieve an Agent
with their assignedCards
. It only involves one DAO, for example:
List<Agent> agents = AgentDAO.getAllAgents();
for (Agent agent: agents) {
System.out.println("Remaining cards for " + agent.name +
" is " + agent.numberOfRemainingCards());
}
Another example:
Agent agentA = AgentDAO.findByName("Agent A");
agentA.sell(aCard);
agentA.damaged(anotherCard);
System.out.println("Number of remaining cards: " + agentA.numberOfRemainingCards());
AgentDAO.update(agentA); // if necessary
If Agent
has a lot of Card
and you don’t want to retrieve all Card
just for determining number of remaining Card
s, you can store the number of remaining cards as state, for example:
class Agent {
List<Card> assignedCards;
int numberOfRemainingCards;
int getNumberOfRemainingCards() {
return this.numberOfRemainingCards;
}
void sell(Card card) {
// do something
assignedCards.remove(card);
numberOfRemainingCards--;
}
void damaged(Card card) {
card.damaged = true;
assignedCards.remove(card);
numberOfRemainingCards--;
}
void assign(Card card) {
assignedCards.add(card);
numberOfRemainingCards++;
}
}
2