Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Where to put implementation of domain services

Where should Domain Service implementations reside in the DDD project structure? If we have IDomainInterface and DomainInterface implementation, should the DomainInterface implementation reside in the Infrastructure or Core/Domain part of the solution/project ?

like image 219
Robert Avatar asked Sep 21 '16 08:09

Robert


2 Answers

Domain service interfaces and their implementation may reside in the domain layer. However, if the domain service implementation depends on infrastructure concerns then by applying the Dependency Inversion Principle, the implementation would live in the infrastructure layer while depending on an interface defined in the domain.

Most domain services will not need to depend on infrastructure concerns and will be used to model use cases that cannot find a natural home within an existing aggregate, but some domain services will.

Repositories are the most common domain services that requires infrastructure knowledge and therefore you will find their implementation living in the infrastructure layer, but there are other examples.

For instance, in the IDDD's Identity & Access bounded context, the EncryptionService interface lives in the domain while the MD5EncryptionService concrete implementation lives in the infrastructure.

like image 65
plalx Avatar answered Sep 28 '22 06:09

plalx


An onion or hexagonal architecture says that Infrastructure layer depends on inner layers. If a contract lives in Domain layer it is because it represents some business requirement, something that represents the ubiquitous language, therefore I consider it a domain service.

If the domain service implementation requires some specific technology (for example database access, or SMTP server access or whatever), its implementation must live in the infrastructure layer. The domain simply doesn't care about implementations, if the business experts talk about something and we decide to make this "something" a contract, it must live in Domain layer. It's all about the Domain language.

Infrastructure services should not be in the Domain layer by definition. If it is something related to infrastructure, then I doubt it has anything to do with the ubiquitous language. I would expect to see infrastructure services contracts living in Application layer, because by definition an application layer is an orchestrating layer to help domain. If the implementation requires some specific technology, again, the implementation will be in the infrastructure layer.

So, to summarize and answer this question: Where to put domain service implementations? It depends:

  • If the implementation does not require anything from application or from a specific technology. Place the implementation in domain layer. (Example: an order number that is calculated)
  • If the implementation requires anything from application layer (for example it requires accessing the Aggregate repository which, in my opinion lives in Application layer), then place the implementation in the application layer.
  • If the implementation requires a specific technology (for example access to a SMTP server, or a concrete http client) then place it in a infrastructure layer.

At the end, the important is that in Domain we care about the ubiquitous language, in application we orchestrate domain and the implementations go where they make sense to be placed depending on their dependencies (domain cannot depend on anything, application can only depend on domain).

like image 36
diegosasw Avatar answered Sep 28 '22 06:09

diegosasw