2

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
  • 2
    A rich domain object will be unable to do anything interesting if you force a no dependencies rule on it. The trick is for dependencies to be interfaces, not concrete types, which allows you to mock them for testing or plug in new implementations.
    – Andy
    Apr 14, 2014 at 1:12
  • And how shall I inject the dependencies? Working with something like Spring encourages adding dependencies to other container beans not entities (domain objects). Apr 14, 2014 at 9:02
  • I'm not familiar with Spring, but you would either go the normal DI route and have the container create your BO, have a factory that creates your BO, or your BO can use a service locator. It depends somewhat on what technology you're using and also what you're comfortable with.
    – Andy
    Apr 14, 2014 at 15:35
  • So isn't it bad that Domain Objects depend on infrastructure (service locator)? Apr 14, 2014 at 22:21
  • I think at some point you need to be pragmatic about things. I wouldn't expect the ui layer to need to figure what the domain object needs to do its job, those really feel like implementation details to me that the ui shouldn't know. So you can use service locator or build a factory class that delegates the creation to a container. At the end of the day something needs to actually new the object right?
    – Andy
    Apr 14, 2014 at 22:44

2 Answers 2

2
+50

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
  • Great answer, and thanks for stating that SRP doesn't literally mean the domain object does exactly one thing. Too many people make SRP out to mean a class can do exactly one thing and nothing else...
    – Andy
    Apr 14, 2014 at 15:37
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 Cards, 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
  • Well believe me, these operations do not happen in a transaction, and I can not make cardCount an attribute of Agent. (This is calculatable, and by the way I shall have zillions of attributes in the agent). Apr 14, 2014 at 9:04
  • You can use something like Map<CardType, Card> assignedCard instead (depending on your case). If you really should move your calculation logic to SQL, then maybe your domain objects is not rich enough. Transaction is not the responsibility of domain objects but the layer that uses them (REST service, service layer, or even MVC's controller).
    – newbie
    Apr 15, 2014 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.