开发者

methods in DDD entities vs services

开发者 https://www.devze.com 2022-12-18 15:17 出处:网络
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.

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 peo开发者_开发知识库ple in real life.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号