Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD Infrastructure services

I am learning DDD and I am a little bit lost in the Infrastructure layer.

As I understand, "all good DDD applications" should have 4 layers: Presentation, Application, Domain, and Infrastructure. The database should be accessed using Repositories. Repository interfaces should be in Domain layer and repository implementation - in Infrastructure (reference DDD: Where to keep domain Interfaces, the Infrastructure?).

Application, Domain, and Infrastructure layer should/may have services (reference Services in Domain-Driven Design), for example, EmailService in Infrastructure layer which sends e-mail messages.

BUT, inside the Infrastructure layer, we have repository implementations, which are used to access the database. So, in this case, repositories are database services? What is the difference between Infrastructure service and repository?

Thanks in advance!

like image 275
Zygimantas Avatar asked Sep 05 '09 18:09

Zygimantas


People also ask

What is a DDD service?

UPDATED: September 17, 2018. Division-funded services for adults with intellectual and developmental disabilities are not an entitlement and are dependent on resource availability.

What is infrastructure layer DDD?

The infrastructure layer is how the data that is initially held in domain entities (in memory) is persisted in databases or another persistent store. An example is using Entity Framework Core code to implement the Repository pattern classes that use a DBContext to persist data in a relational database.

Is DDD same as microservices?

DDD provides an avenue to facilitate the development of highly cohesive systems through bounded contexts. Microservices is an implementation approach that encourages you to focus your service boundaries on the business domain boundaries.

What is DDD architecture?

Domain-driven design (DDD) is an approach to developing software for complex needs by deeply connecting the implementation to an evolving model of the core business concepts. Its premise is: - Place the project's primary focus on the core domain and domain logic. - Base complex designs on a model.


3 Answers

Sticking with DDD definitions, a Repository is different than a Service. A Repository directly correlates to an Entity, often an Aggregate Root. A Service defines behaviors that don't really belong to a single Entity in your domain. You can absolutely find Services in every layer, though the types of problems they address differ from layer to layer and may be different from DDD's conceptual Service.

When working at the conceptual level, a DDD Repository differs from a DDD service in that it is specifically tied to Entity persistence. A Service can address any Domain, Application, or Infrastructure problem you may have.

You run into terminology clashes with DDD all over the place. For instance, a DDD Repository is NOT the same thing as the Repository pattern found in Martin Fowler's PoEAA book, though it may employ such a pattern. This is often a source of confusion for many people.

It helps with DDD if you always keep the Domain Model at the very center of everything you do. When it comes to layering DDD apps, I often choose Jeffrey Palermo's Onion Architecture. Check it out. Download CodeCampServer, an example app using this architecture. I think it's a perfect fit for DDD programming.

Good luck!

like image 67
Kevin Swiber Avatar answered Oct 09 '22 01:10

Kevin Swiber


Maybe it will help to see a potential project structure.

Possible assembly or package structure:

Project.Domain
Project.Infrastructure.Data
Project.Infrastructure.Components
Project.Infrastructure.Services

Possible namespace or folder structure:

Project.Domain
-n- Modules
----n- Account
-------f- Account.xx
-------f- AccountRepository.xx
-------f- Contact.xx
----n- Marketing
-------f- RegionRepository.xx
-n- Shared
-n- Services

Project.Infrastructure.Data (OR-Mappers)
-n- Tables
-n- Views
-n- Procedures
-n- Functions

Project.Infrastructure.Components (Generic)
-n- Mail
-n- Cryptography
-n- UI

Project.Infrastructure.Services (Special Operations)
-f- DoingSomethingService1.xx
-f- DoingSomethingService2.xx
-f- DoingSomethingService3.xx

Domain Entities and Value Types do not use Domain Services. The Application Layer uses the Services of the Domain. The Domain Repository objects use the Infrastructure.Data objects to return Domain objects.

like image 41
Roy Oliver Avatar answered Oct 09 '22 01:10

Roy Oliver


An unfortunate thing about DDD is the word 'Service'. What it should be is 'Domain Service'. Think of the Domain as entities and value objects, while Services are a way of dealing with actions, operations and activities.

As for the Repositories, they are just a facade that should behave like a collection to your domain. If you are using an ORM or writing your own, this is what all your domain objects should be going through in order to achieve persistence instead of those services directly.

like image 28
Ty. Avatar answered Oct 09 '22 00:10

Ty.