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 ?
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.
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With