2

I am trying to design a application using domain driven design, i have a doubt regarding storage of an entity data. First i am creating an entity and applying business rules on the entity. Finally i want to store this data. I am using repository pattern to abstract the database operations. Now where i need to place code related to saving the entity data to database. Is it goes in the entity? or a domain service?

1 Answers1

1

Persistence related code would go in the infrastructure layer. I usually put this in it's own assembly and inject the specific implementation of my repositories into my application layer.

You definitely do not want any database code in your entities or domain services.

Tyler Day
  • 1,690
  • 12
  • 12
  • I also kept persistence related code into the infrastructure layer i.e., concrete classes and i am injecting this concrete class to interfaces available in the domain layer. So some where i need to call the interface method to save the data to persistence, where should i do this, is it in entity or domain service or application service. – Mahesh kumar Chiliveri Apr 02 '15 at 12:52
  • Usually you would call the repository methods in your application layer. Sometimes from your domain services as well. Avoid doing it from your entities though. – Tyler Day Apr 02 '15 at 16:10
  • I have went through this article "http://www.infoq.com/articles/ddd-in-practice" where they mentioned we should inject repositories to domain objects. – Mahesh kumar Chiliveri Apr 03 '15 at 05:27
  • Managing the dependencies between domain objects is a classic problem that developers often run into. The usual design solution to this problem is to have the Service or Facade class call a Repository directly and when invoked the Repository would return the Entity object to the client. This design eventually leads to the afore-mentioned Anemic Domain Model where facade classes start accumulating more business logic and domain objects become mere data carriers. A good design is to inject Repositories and Services into domain objects using DI & AOP techniques. – Mahesh kumar Chiliveri Apr 03 '15 at 05:30
  • Yes, you should define your interfaces such as repositories in the domain layer, and then inject their implementations into your application layer. – Tyler Day Apr 03 '15 at 15:33
  • Saving data to database is application concern, so should i place this code in domain layer or application services? Most of the people saying domain objects should be independent of the persistence concern. – Mahesh kumar Chiliveri Apr 04 '15 at 07:27
  • Domain layer defines the repository interface. Infrastructure layer implements the interface (this is where your actual database code goes). Finally your infrastructure implementations are injected into your application layer handlers via dependency injection. – Tyler Day Apr 04 '15 at 16:22
  • So can i have repository interface in a entity to save it to database or saving entity should go to application services? – Mahesh kumar Chiliveri Apr 05 '15 at 16:56
  • 1
    All repository retrieving/saving should happen in a domain service or application service. – Tyler Day Apr 05 '15 at 16:57
  • One more question, i have facing problem when i am retrieving data from database using repository and assign it to entity, because entity can not be directly created, all properties have private set's.So what we can do in this scenario where domain object and database representation is different. – Mahesh kumar Chiliveri Apr 05 '15 at 17:00
  • I'm not sure what language you are using, but if you are using C# then you can set private properties using reflection. There are also object mappers you can use, such as AutoMapper. – Tyler Day Apr 05 '15 at 17:03
  • i am using C# and data base SQL, is it right to create domain object in repository and returning it to calling method from domain layer? – Mahesh kumar Chiliveri Apr 05 '15 at 17:13
  • So, should I call my domain layer from a service layer (service layer = application services) and then treat the result of the call as a data which I can persist in database? – manymanymore Apr 08 '21 at 13:37
  • Does it mean that such an operation as an entity removal belongs completely in a service layer and not in a domain model? – manymanymore Apr 08 '21 at 13:39