DDD - Modelling the Domain - Services
When designing a Domain Model the prominent modelling paradigm is objects and as a result we are mapping concepts i.e. nouns to DDD building blocks such as Entities and Value Objects. The behaviour associated to those concepts is mapped to methods using verbs. However, there are domain specific functions that we would like to define in the model but cannot be attached into an existing Entity or Value Object (Evans, 2003). Instead of forcing foreign operations into domain objects, Evans (2003) introduced the concept of Domain Services.
The concept of Domain Service is used here to describe an interface defined within the domain model and named in terms of the Ubiquitous Language using a verb instead of a noun (Evans, 2003). Services may be implemented in the infrastructure layer (Vernon, 2013) but method arguments and returned results should be objects of the domain model (Evans, 2003). Another important characteristic is that Services defined within the domain model should be stateless i.e. the behaviour of a service instance is not affected by its internal state which is common for other domain elements such as Entities (Evans, 2003).
The word service is so often used when designing software systems which rightly causes misunderstandings. For example, in the context of Service Oriented Architectures, the provided services are coarse-grained, typically providing a remote procedure call (RPC) interface (Vernon, 2013). On the contrary, Domain Services are not meant to be distributed and accessed directly through an RPC interface. Additionally, it is important to differentiate between a Domain Service and an Application Service. The application layer has no business logic and is the client of the domain layer (Evans, 2003). It seems unfortunate that operations in the Application layer are also called Services (Evans, 2003; Vernon, 2013). My own view is that the word service is particularly popular in Java based application frameworks which influenced the naming of operations in the application layer.
As an example of a Domain Service consider the case of calculating the routes for a trip. The driver inputs the starting point, the destination and a CalculateRouteService computes the possible routes to the target city. This may require accessing a third party service to retrieve traffic related information.
Finally, bear in mind that Domain Services should not be used to strip Entities and Value Objects from business logic (Evans, 2003; Vernon, 2013), thus creating an Anemic Domain Model (Fowler, 2003).
References
- Evans, E. (2003) Domain-Driven Design: Tacking Complexity In the Heart of Software. Boston: Addison-Wesley.
- Fowler, M. (2003) Anemic Domain Model. [Online] Available at: http://www.martinfowler.com/bliki/AnemicDomainModel.html [Accessed 26th May 2013].
- Vernon, V. (2013) Implementing Domain-Driven Design. Boston: Addison-Wesley.
This post is an excerpt of my MSc Thesis titled "Applying Domain-Driven Design in Python - designing a self-hosted Read-it-Later service" (January 2014).