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
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!