2
votes

Hi im new to ddd design and is trying to develop my first application using this pattern working in C#

In my application i have an aggregate Contract that have child entity assets, when an asset is added or settled i should perform an accounting operation in another aggregate Accounts and ensure it in business logic.

Should i create a domain service that ensures that each operation in contract assets will raise an account operation, and call this service in application layer sending a collection of account entity. Or should I inject repository to this service load the account list and save the changes in account and operations list.

Or even make the methods in asset entity raise an event that enforce account changes. If this is the right approach, the event handle should be in the domain or application? If in the domain should the handler in the account entity perform the changes through respository injected?

Im a bit confused

2
This question rather belongs to softwareengineering.stackexchange.com - Markus Deibel

2 Answers

2
votes

generally this kind of problems can be elegantly solved using events, and focusing on one aggregate per transaction.

Let's say your use case is to add an Asset to a Contract.

You will have an application service with a ContractRepository that will retrieve the Contract, and a method addAsset will be called on that Contract.

When you add an asset to your Contract aggregate, this aggregate will record a domain event, like AssetAdded, with all relevant information about that action. Then your application service will persist the updated Contract in the database and then it will publish the event to an asynchronous bus. In this moment you can send a response.

Some subscriber, inside your application, will be notified about that event and will do stuff. In this case you could have an UpdateAccountOnAssetAdded that internally will do the rest of the job.

This article will help you understand how everything is organized inside this kind of architecture.

Good luck!

-1
votes

Let’s take the last question first. Events are for things that can be done asynchronously and in this case async won’t work. Any time an aggregate is saved it should satisfy all business rules so you have to deal with asset and the account at the same time.

Services should be used sparingly. They operate on more than one AR where none has an enforced relationship with the others. In your case, Contract owns all the other entities involved so all work should be done inside a method on Contract. If that requires a repository, then inject it into the Contract.