Where is the call to persistence in the domain driven design

sventevit

I know there is a lot of similar information online and also on stackoverflow, but I'm still not sure where to put the logic of persistence into my project. I don't yet use any ORM, IoC and UnitOfWork concepts (too much new stuff for a beginner in the ddd world).

I see two options for my Order model:

  1. Inside the Domain assembly there is an Order class and an IOrderRepository interface. Order class has a private instance of the IOrderRepository which is passed in the constructor. Order class has a public Insert method, which calls the IOrderRepository.Insert method. The actual implementetion of the repository is in OrderRepository class of the Infrastructure layer. The Service layer would contain an OrderService class which instantiates my model with the appropriate repository and then calls Order.Insert(). The bad: we have to inject an interface (or multiple instances) of the repository to the model class, persistence logic is inside the model. The good: sometimes something has to be done before or after the insert method is called and this could nicely fit into the Insert method of the Order class, for example raising domain events or whatever.
  2. In the Model assembly there is only Order class. The Service layer creates new Order and new OrderRepository and executes orderRepository.Insert(order).

Could you please explain which concept is better in simple words (explain like I'm Five).

Sergey Berezovskiy

Your domain classes should be focused on business logic of domain only, and they should be persistent ignorant (i.e. persistence should be separated from business logic). Adding persistence-related operations violates Single Responsibility Principle. Also dependency on repository makes your domain classes complex, instead of being simple POCO entities.

Let's think about this design from coding point. You have to provide repository instance to each Order, and then call order.Insert() for this order to pass itself to repository which you have injected into order. Sounds complicated. Much simpler will be use repository.Save(order). Sometimes it's OK to have CRUD methods on class (see Active Record pattern). But this approach is good when you don't have complex domain model.

I think best place for persistence of your domain is application services (maybe you call this layer as Service layer). They load entities from repositories, perform operations (it could be simple operations on entities, or calls to domain services), and then state of domain gets saved.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Where to find Domain Driven Design consultants?

From Dev

Layers in Domain Driven Design

From Dev

Where to put security when using DDD - domain driven design

From Dev

Where does the message bus service live in Domain Driven Design

From Dev

Where to put general purpose serialization class in domain driven design?

From Dev

Domain Driven Design - where should we put repositories?

From Dev

Domain Driven Design - Domain or Security

From Dev

Access Control in Domain Driven Design

From Dev

Domain Driven Design and batch processing

From Dev

Implementing Domain Driven Design Cost

From Dev

Implementing Domain Driven Design Cost

From Dev

Domain Driven Design Bounded Context Domain Objects

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design. Entity type design

From Dev

Implementing Paging and Sorting with Domain Driven Design

From Dev

Implementing Domain-Driven Design and Transactions

From Dev

Domain driven design repository implementation in infrastructure layer

From Dev

Domain Driven Design in Node.js Application

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

Domain Driven Design - CQRS + ES usage

From Dev

Implementing Domain Driven Design Book Confusion

From Dev

Domain Driven Design (DDD) and database generated reports

From Dev

Domain-Driven-Design Entities and Value Objects

From Dev

Proper way to get aggregates in Domain Driven Design

From Dev

Domain Driven Design Auto Incremented Entity Key

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

How to add scheduled jobs in domain driven design

From Dev

How to handle Domain Driven Design when domain is dynamic / changes

Related Related

  1. 1

    Where to find Domain Driven Design consultants?

  2. 2

    Layers in Domain Driven Design

  3. 3

    Where to put security when using DDD - domain driven design

  4. 4

    Where does the message bus service live in Domain Driven Design

  5. 5

    Where to put general purpose serialization class in domain driven design?

  6. 6

    Domain Driven Design - where should we put repositories?

  7. 7

    Domain Driven Design - Domain or Security

  8. 8

    Access Control in Domain Driven Design

  9. 9

    Domain Driven Design and batch processing

  10. 10

    Implementing Domain Driven Design Cost

  11. 11

    Implementing Domain Driven Design Cost

  12. 12

    Domain Driven Design Bounded Context Domain Objects

  13. 13

    Domain Driven Design: infrastructure concern or domain concern?

  14. 14

    Domain Driven Design: infrastructure concern or domain concern?

  15. 15

    Domain Driven Design. Entity type design

  16. 16

    Implementing Paging and Sorting with Domain Driven Design

  17. 17

    Implementing Domain-Driven Design and Transactions

  18. 18

    Domain driven design repository implementation in infrastructure layer

  19. 19

    Domain Driven Design in Node.js Application

  20. 20

    Fetching associated aggregates in Domain driven Design

  21. 21

    Domain Driven Design - CQRS + ES usage

  22. 22

    Implementing Domain Driven Design Book Confusion

  23. 23

    Domain Driven Design (DDD) and database generated reports

  24. 24

    Domain-Driven-Design Entities and Value Objects

  25. 25

    Proper way to get aggregates in Domain Driven Design

  26. 26

    Domain Driven Design Auto Incremented Entity Key

  27. 27

    Fetching associated aggregates in Domain driven Design

  28. 28

    How to add scheduled jobs in domain driven design

  29. 29

    How to handle Domain Driven Design when domain is dynamic / changes

HotTag

Archive