2
votes

Folks, Apologies if this has been covered in another thread, but I have searched ddd and mvc articles and have not found a straightforward answer.

I am hoping to apply a DDD approach to the architecture of my MVC projects. Please correct me where I am wrong.

All MVC controller actions that involve hitting the domain model will initially hit and application service layer. The application service layer here acts as a facade between presentation and the domain. Any requests from the application service later that clearly involve discrete domain aggregates will perform fetch or modify operations on aggregate roots using repositories. Each aggregate root will have its own repository.

so the application service layer must be injected with any/all repositories required by the domain.

Where an operation may involve multiple aggregates or requires logic that does not fit neatly into one aggregate, the application service will call a domain service to carry out operations across aggregates.

This does not seem right to me. My confusion is that from a DDD perspective Im not sure whether for example aggregate roots should perform their own persistance i.e. the aggregate gets injected with a repository and then persists/fetches itself or whether as above the application service layer uses repositories to act on or fetch aggregates?

Also if the application service layer is injected with all repositories, does the domain service that the application service layer calls also need repositories injected?

Im keeping CQRS out of this at this point. I want to get the layering and the relationship between services and aggregates sorted out first.

Thanks for any advice.

1

1 Answers

3
votes

All MVC controller actions that involve hitting the domain model will initially hit and application service layer. The application service layer here acts as a facade between presentation and the domain.

There's debate over that but I would consider carefully whether that additional layer is needed or not. It adds a lot of boilerplate code and degrades maintainability - as someone pointed out recently, when you separate things that change for the same reasons (ie your service methods and the corresponding domain methods), you have to make changes in many different places in the system.

On the other hand, you could need that service layer to map your domain objects to DTOs but there again, it could be done directly in the Controller and nothing forces you to use DTOs in the presentation layer.

My confusion is that from a DDD perspective Im not sure whether for example aggregate roots should perform their own persistance i.e. the aggregate gets injected with a repository and then persists/fetches itself or whether as above the application service layer uses repositories to act on or fetch aggregates?

It's usually considered bad practice to have aggregate roots manage their own persistence because it breaks persistence ignorance and violates the Single Responsibility Principle. If you do that, your aggregate root class now has 2 reasons to change, 2 reasons to break the code, it is less maintainable, etc.

You should instead delegate the responsibility of saving the aggregate root in its repository to an object that will be aware of the application execution context (for instance, a Controller or an object in the Application layer).

Also if the application service layer is injected with all repositories, does the domain service that the application service layer calls also need repositories injected?

Yes, I think it pretty much makes sense especially if the domain service heavily relies on the repository.