Am I allowed to have “incomplete” aggregates in DDD?

前端 未结 5 615
再見小時候
再見小時候 2021-02-05 10:04

DDD states that you should only ever access entities through their aggregate root. So say for instance that you have an aggregate root X which potentially has a lot of

相关标签:
5条回答
  • 2021-02-05 10:33

    Is it OK to implement a repository then, so that in such scenarios it returns an incomplete aggregate?

    Not at all. Aggregate is a transnational boundary to change the state of your system. Never use aggregates for querying data. Split the system into Write and Read sides. (read about CQR & CQRS). When we think "CRUD" based, we implement our system, based on some resource. Lets say you have "Appointment" aggregate. Thinking "Crudish" means we should implement usecases Create, Update, Delete, GetAll appointments. That means Appointment[] should be returned for GetAll. When you think usecase based, (HexagonalArchitecture) your usecases would be ScheduleAppointment, RescheduleAppointment, CancelAppointment. But for query side it can be: /myCalendar. We return back all appointments for a specific user in a ClientCalendar object. Create separate DTO's for Query sides. Never use aggregates for this purpose.

    0 讨论(0)
  • 2021-02-05 10:41

    Jimmy Nilsson hints in his book that instead of reading a complete aggregate you can read a snapshot of parts of it. But you are not supposed to be able to save changes in the snapshot classes to the database.

    Jimmy Nilsson's book Chapter 6: Preparing for infrastructure - Querying. Page 226.

    Snapshot pattern

    0 讨论(0)
  • 2021-02-05 10:44

    You're really asking two overlapping questions.

    1. The title and first half of your question are philosophical/theoretical. I think the reason for accessing entities only through their "aggregate root" is to abstract away the kinds of implementation details you're describing. Access through the aggregate root is a way to reduce complexity by having a trusted point of access. You're eliminating friction/ambiguity/uncertainty by adhering to a convention. It doesn't matter how it's implemented within the root, you just know that when you ask for an entity it will be there. I don't think this perspective rules out a "filtered repository" as you describe. But to provide a pit of success for devs to fall into, it should be impossible instantiate the repository without being explicit about its "filteredness;" likewise, if shared access to a repository instance is possible, the "filteredness" should be explicit when coding in the caller.

    2. The second half of your question is about implementation on a specific platform. Not sure why you mention delayed execution, I think that's really orthogonal to the filtering question. The filtering itself could be a bit tricky to implement with LINQ. Maybe rather than inlining the Where lambdas, you set up a collection of them and select one depending on the filter you need.

    0 讨论(0)
  • 2021-02-05 10:46

    You are allowed since the code will compile anyway, but if you're going for a pure DDD design you should not have incomplete instances of objects.

    You should look into LazyLoading if you're afraid to load a huge object of which you will only use a small portion of its child entities.

    LazyLoading delays the loading of whatever you decide to lazy-load until the moment they are accessed. They make use of callbacks to call the loading method once the code calls for them.

    0 讨论(0)
  • 2021-02-05 10:52

    I consider an aggregate root with a lot of child entities to be a code smell, or a DDD smell if you will. :-) Generally I look at two options.

    1. Split your aggregate into many smaller aggregates. This means that my original design was not optimal and I need to identify some new entities.
    2. Split your domain into multiple bounded contexts. This means that there are specific sets of scenarios that use a common subset of the entities in the aggregate, while there are other sets of scenarios that use a different subset.
    0 讨论(0)
提交回复
热议问题