Rich domain model and container services

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Agent {
int computeRemainigCards(CardType cardType) {}
}
</code>
<code>class Agent { int computeRemainigCards(CardType cardType) {} } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
}
</code>
<code>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; } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List<Agent> agents = AgentDAO.getAllAgents();
for (Agent agent: agents) {
System.out.println("Remaining cards for " + agent.name +
" is " + agent.numberOfRemainingCards());
}
</code>
<code>List<Agent> agents = AgentDAO.getAllAgents(); for (Agent agent: agents) { System.out.println("Remaining cards for " + agent.name + " is " + agent.numberOfRemainingCards()); } </code>
List<Agent> agents = AgentDAO.getAllAgents();
for (Agent agent: agents) {
   System.out.println("Remaining cards for " + agent.name + 
                      " is " + agent.numberOfRemainingCards());
}

Another example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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 Cards, you can store the number of remaining cards as state, for example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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++;
}
}
</code>
<code>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++; } } </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Rich domain model and container services

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Agent {
int computeRemainigCards(CardType cardType) {}
}
</code>
<code>class Agent { int computeRemainigCards(CardType cardType) {} } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
}
</code>
<code>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; } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List<Agent> agents = AgentDAO.getAllAgents();
for (Agent agent: agents) {
System.out.println("Remaining cards for " + agent.name +
" is " + agent.numberOfRemainingCards());
}
</code>
<code>List<Agent> agents = AgentDAO.getAllAgents(); for (Agent agent: agents) { System.out.println("Remaining cards for " + agent.name + " is " + agent.numberOfRemainingCards()); } </code>
List<Agent> agents = AgentDAO.getAllAgents();
for (Agent agent: agents) {
   System.out.println("Remaining cards for " + agent.name + 
                      " is " + agent.numberOfRemainingCards());
}

Another example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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 Cards, you can store the number of remaining cards as state, for example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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++;
}
}
</code>
<code>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++; } } </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật