p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Patterns, Principles and Practices of Domain-Driven Design (http://p2p.wrox.com/forumdisplay.php?f=789)
-   -   Repository in Domain (http://p2p.wrox.com/showthread.php?t=94673)

varghesep July 3rd, 2015 09:11 AM

Repository in Domain
Glad to be back again to ask you another question.

Take a case where I have many aggregates in a domain model. There are many interactions between the aggregates and there are many CRUD operations too. It is a natural tendency to inject a IRepository in a domain model or domain service. You can think of a scenario where the domain service has to read a customer and then read his orders to make a business decision. To get the customer and orders from the persistent media, I can call a repository find method in the domain model.

My question is, is injecting IRepository to domain service or domain model (in an aggregate) anti-pattern?

My understanding is that the layers exit as the following:

App Service
Repository / Domain Service
Domain Model

The domain service or domain model does not have the knowledge of any repository or even a contract. But the App Service does. It is the job of the App service to read any domain entities from the persistent media and pass that to the domain service or domain model. Please correct me if my understanding is not correct.

nick_t July 4th, 2015 04:05 AM


Glad to have you back :)

Repositories are a tricky subject to be fair. Should repository interfaces live in the domain or service layer? It depends.

For me, I would try and keep repositories completely out of the domain. I think that's the ideal, because they are an implementation detail. However, sometimes you need to run queries as part of your domain logic. And so, you almost have to have a repository in the domain so it can be called from within a domain service.

If you ask a handful of DDD practitioners, even those talking at conferences etc, I would expect some will say repositories in the domain, others will say keep them out.

Basically, my opinion is, if you can keep them out... keep them out. If not, don't be afraid to put them in the domain model.

varghesep July 4th, 2015 08:17 AM

Thanks for that response.

All times are GMT -4. The time now is 06:03 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.