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:
- The operation performed by the Service refers to a domain concept which does not naturally belong to an Entity or Value Object.
- The operation performed refers to other objects in the domain.
- 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.