Domain-Driven Design – Is a Domain Service Just a Facade or Mediator Pattern?

domain-driven-designobject-orientedobject-oriented-design

In Domain Driven Design, the Domain Layer can have several (traditional) services. For example, for the User domain, we may have:

  • A UserFactory, that builds User objects in different ways
  • A UserRepository, which is responsible for interacting with the Persistence Services in the Infrastructure Layer

Is a UserService in the Domain Layer simply a Mediator and/or Facade to those two services and the Infrastructure Layer, or is there more to it?

Best Answer

Domain services are best described by what they are not:

  • they are neither Entities nor Aggregate roots
  • they are not Value objects
  • carry domain knowledge that doesn’t naturally fit only one Entity or one Value object

An example of a Domain service is a Saga/Process manager: it coordinates a long running process involving multiple Aggregate roots, possible from different Bounded contexts.

That being said, what is a Domain service and how it is implemented are two orthogonal things.

Is a UserService in the Domain Layer simply a Mediator and/or Facade to those two services and the Infrastructure Layer, or is there more to it?

Some domain services like a UserRepository (composed of an interface defined in the Domain layer and a concrete implementation in the Infrastructure layer) can be implemented using the Facade design pattern. Other domain services are not.

There is no hard rule about how to implement them, other than the important rule that the Domain layer must not depend on other layers (and S.O.L.I.D.).