0

I have a header-detail screen. The screen has a save button that saves all of the graph together (master and details) in a single transaction. The header is represented by JobMajorCategory and the details are a list of JobMinorCategory. JobMinorCategory can't live without its parent. I read a lot about aggregates and their boundaries but I can't decide which pattern to use. How can I model these?

Since the entities will be saved in the same transaction, shall I use a single aggregate that contain both of them? JobMajorCategory woule be the root aggregate and JobMinorCategory would be an entity inside the aggregate.

Or should I use two aggregates, one for JobMajorCategory and one for JobMinorCategory since there is no real invariant or business role between them?

3
  • Aggregates have little to do with screens and buttons. I think you should focus on your domain-model first and how it behaves before visualizing any sort of ui. Dec 28, 2016 at 16:11
  • Yes, I know but the UI related to the business case and my business case said that both of the header and details should be saved in a single transaction
    – roro2012
    Dec 28, 2016 at 16:15
  • "Save" is a CRUD operation and this is what's clouding your judgment. If you only do CRUD then you do not need to bother thinking about ARs, just model transactions and data against UI use cases.
    – plalx
    Dec 29, 2016 at 15:51

1 Answer 1

1

You should look at designing a view model, or equivalent DTO, which satisfies your UI consideration. This allows you to keep your aggregate focused on the domain, and you then can load the representation through translation of the aggregate. I understand that you say that your business use case is talking about the aspect of a "single transaction", but that is a technical detail.

When we look to apply domain-driven design, it is not about the technology choices, or how that goal is achieved. Instead, it is about modelling the actual business domain. A properly designed domain will have no knowledge of the UI, nor will it be influenced by the UI. To that end, I believe that you are saying that a save operation should be in a single operation. That is completely fine, because your application can be more coarse at the higher levels, like the UI, and more finely-grained as you move lower in your stack.

Based on what you are saying, what you may need to do is to create a distinct boundary between your domain and your application. Generally, this is done with application services. These "speak" both application and domain, in such that they can talk to the domain, but only surface DTOs/models. Effectively isolating your domain prevents churn in the domain, and then changes to the UI - which will most definitely change more frequently than the domain, itself - have an isolation layer.

Code and artifacts which are closer to the user will churn more frequently. The domain is about "truths" that relate to the way that the business operates. To that end, your UI is only a window in to your domain - or more appropriately, often times it is a view into an abstraction of your domain.

Edit to specifically address details of the question.

In what you describe, I do not know enough about the domain to provide a full and accurate answer. However, you are on the right track. Since the JobMinorCategory cannot exist without the JobMajorCategory, it is a fair assumption that the JobMinorCategory is either an entity or a value object (if it is just text, for example, and has no other relational impact on other JobMinorCategories). You can still use that premise to work in a "single transaction".

For sake of argument, say that you have an MVC application. Your save can be wired to a generic "Save" action. In turn, that controller can then make the determination in what to do, based on the payload. If you marked a JobMinorCategory as being queued for deletion, at a level lower than the controller, you can iterate through the items, gather up the ones that should be deleted, and enroll them for deletion in whatever you are using to manage your Unit of Work.

The domain is not going to care how you are doing this in the UI. But, your domain can expose a means to delete a JobMajorCategory, which will delete all JobMinorCategories. When you get down to the aggregate, itself, that is where the business logic should live. For example, your JobMajorCategory should have a method for DeleteJobMinorCategory() and AddJobMinorCategory(). Then, once the state of your aggregate reflects the state that you want it to be at, you will persist that aggregate from a higher level. Depending on your overall architecture, this might be from a Domain Service.

There are several ways to do this, varying in complexity. Without knowing more, I would assume that on the backend, you would want to load your actual Aggregate and apply changes based on the user action. So, if you could inspect your model to see that the user removed a JobMinorCategory, you could have your Domain Service retrieve the latest version of the Aggregate, call a RemoveJobMinorCategory() method (which is where your business logic will live), and once that method has completed, you would then have the Domain Service persist the changed Aggregate. Based on the model, you might detect that this happened multiple times - i.e. the user removed more than one JobMinorCategory. This is fine, as well. You would just make multiple calls to RemoveJobMinorCategory() and then persist.

That is one of the beautiful things about domain-driven design, when correctly implemented. You will make multiple operations against the Aggregate, the Aggregate will be the "gatekeeper" of if the applicable business logic allows that change, and then you will persist it. Since the persistence mechanism is also a technical detail, we want to keep it on a layer outside of the actual domain objects, themselves. Much like an application service acts as a bridge between the UI/application and the domain, a domain service can act as a bridge between the Domain Objects and a technical detail.

So, if you are using something like Entity Framework (which contains a Unit of Work, through its context), you would load your entity, apply changes through methods on the entity (partial classes are one way to do this), and then persist it back once you are done. This allows you to do as much as you want to, in a single transaction. Generally, you will want to limit a transaction to a single aggregate, or at least a single bounded context.

Creation would work in exactly the same way. You create the JobMajorCategory aggregate instance, and add JobMinorCategory entities via methods which encapsulate the logic related to adding those. Once you are done constructing and/or modifying, then you commit those changes.

11
  • Hi Joseph, Thanks for your help. Could you tell me if my initial modeling is wrong or not. I initially created two aggregates:- One for header and the other for details. That is right?
    – roro2012
    Dec 29, 2016 at 12:13
  • note, no invariant roles between the header and lines but if i delete the header, all of the lines will be deleted
    – roro2012
    Dec 29, 2016 at 12:14
  • that is mean the minor category can't live without their parent minor category
    – roro2012
    Dec 29, 2016 at 12:17
  • I've added some further detail, although it is really hypothetical. You are on the right track, but it sounds like you will benefit from isolating your domain from your UI through abstraction. Dec 29, 2016 at 12:41
  • Hi Joseph, after a lot of investigations, i decided to create two aggregates. The role 'can't exist without' is a tricky one and we should design our aggregates based on transaction consistency boundary. e.g The invariant shouldn't be violated on transactions. for my situation, for example. User A update Header and it is ok for User B to update one of its line item. You get my point
    – roro2012
    Dec 29, 2016 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.