Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

methods in DDD entities vs services

Our team is fairly new to DDD, and are trying to implement some of the concepts in our current project. One question that has come up is whether to put methods into entity objects, or service objects.

Some team members feel that entities should only hold values and all functionality should be contained in services. Others feel this makes the entity objects anemic, and that they should hold functionality that relates to the entity, while service objects should be utilized for more cross-cutting functionality.

We're wondering what the formal DDD point of view is on this, as well as what has worked for people in real life.

like image 297
alchemical Avatar asked Jan 28 '10 23:01

alchemical


People also ask

What are entities in DDD?

A domain entity in DDD must implement the domain logic or behavior related to the entity data (the object accessed in memory). For example, as part of an order entity class you must have business logic and operations implemented as methods for tasks such as adding an order item, data validation, and total calculation.

What is DDD methodology?

Domain-Driven Design(DDD) is a collection of principles and patterns that help developers craft elegant object systems. Properly applied it can lead to software abstractions called domain models. These models encapsulate complex business logic, closing the gap between business reality and code.

What is anemic domain model?

The Anemic domain model is a programming Anti-pattern where the domain objects contain little or no business logic like validations, calculations, rules, and so forth. The business logic is thus baked into the architecture of the program itself, making refactoring and maintenance more difficult and time-consuming.


2 Answers

There is no formal point of view for DDD, but the whole purpose of a rich Domaim Model is to avoid an Anemic Domain Model, so explicitly refusing to put any behavior on the Domain Objects goes against the spirit of it.

One school of thought holds that Domain Objects should be POCOs/POJOs, which means that they shouldn't contain abstract services as members. However, that doesn't mean that they can't have methods that interact with such services.

The more (relevant) behavior you can give each Domain Object, the better.

like image 113
Mark Seemann Avatar answered Sep 23 '22 19:09

Mark Seemann


According to Domain Driven Design Quickly, there are three basic types of objects.

  • Entities
  • Value Objects
  • Services

Business logic of an application is implemented within all three of these objects, but we are cautioned to partition the logic judiciously.

A Service can group related functionality which serves the Entities and the Value Objects. It is much better to declare the Service explicitly, because it creates a clear distinction in the domain: it encapsulates a concept. It creates confusion to incorporate such functionality in an Entity or Value Object because it won’t be clear what those objects stand for.

On the other hand,

A Service should not replace the operation which normally belongs on domain objects. We should not create a Service for every operation needed. But when such an operation stands out as an important concept in the domain, a Service should be created for it. There are three characteristics of a Service:

  1. The operation performed by the Service refers to a domain concept which does not naturally belong to an Entity or Value Object.
  2. The operation performed refers to other objects in the domain.
  3. The operation is stateless.

So there are some clear criteria for determining where a method belongs. Unfortunately, DDD is not as simple as saying business logic belongs in entities or business logic belongs in services. Neither statement is true. The answer is a combination of both.

Finally, don't add methods to domain objects just to avoid an anemic domain model.

When we create a software application, a large part of the application is not directly related to the domain, but it is part of the infrastructure or serves the software itself. It is possible and ok for the domain part of an application to be quite small compared to the rest, since a typical application contains a lot of code related to database access, file or network access, user interfaces, etc.

like image 29
jaco0646 Avatar answered Sep 19 '22 19:09

jaco0646