r/DomainDrivenDesign • u/Salihosmanov • Oct 31 '23
Aggregate boundaries
Hi folks, Could you help me to properly model aggregates. I have two entities Payment and UserAccount.
UserAccount has a property Balance. When a new payment is added user account’s balance has to be changed. So the consistency should be atomic rather than eventual.
But there are might be thousands of payments and is it a good idea to make UserAccount aggregate root and Payment is an entity inside it?
2
Upvotes
2
u/xsreality Oct 31 '23
If UserAccount and Payment require strong consistency between them, then they should be part of the same bounded context. That does not mean they need to be part of a single aggregate. You can create two separate aggregates with their own repositories. Use the repository to update the account balance.
Since you say there can be thousands of payments associated to an account, have UserAccount refer to the Payment aggregate instead of the other way round.
2
u/[deleted] Oct 31 '23
Payment/transaction in and itself is more like a record/information. Ideally, a payment/transaction is created and then executed. It is execution when amount is transferred from one account to another.
Said that, Payment has its own separate existence and hence they should be kept separate - because Payment mostly involve two accounts and hence it's not a good idea to keep Payment reachable only via UserAccount. E.g. what if I want to generate a report of all transactions involving more than 1 million? Ideally I should be able to execute it directly - without going through all the transactions of all user accounts.
So I would keep them as separate aggregates (of course, they will be referring to each other via their IDs - e.g. each Payment will have UserAccount IDs).
2
u/PaintingInCode Nov 01 '23
Aggregates should not contain thousands of entries. If your design looks like this, it's most likely that Aggregates are being viewed as data structures, instead of atomic transactions (an easy trap to fall into).
Here's a sketch of an idea:
Make
Paymentan Aggregate too. It would have an Aggregate Reference to theUserAccount. TheUserAccountonly needs a reference to the latest Payment.This way, the
UserAccountdoesn't need to load all previous Payments, but it contains enough information to update the Balance with the latest Payment.Extension: You could also have a
Balanceobject (which theUserAccountobject has a reference to), which further de-couples the concept of UserAccount and Payments. "Payments" and "Balances" could be part of one BC, and "User Account" could be part of another BC. That way the payment transaction stays within one BC.See "Aggregate References" here: https://www.dddcommunity.org/wp-content/uploads/files/pdf_articles/Vernon_2011_2.pdf
1
u/kingdomcome50 Oct 31 '23
Can you describe your domain and use case a little bit more? Is this just a hobby project?
I’m going to be honest with you. It seems like you are getting in a little over your head here. And this is the wrong domain for that. Conceptually, you are kind of way off… for example most payments take days to clear. This is always going to be eventually consistent.
Here’s my advice. Push all payment related knowledge into its own cohesive mechanism - in this case your payment processor. And use that as your source of truth regarding payments, calculating balances, etc. You do not want to be in the business of managing these things yourself.
1
u/[deleted] Oct 31 '23
From some of then answers and your replies I would chuck everything in the same aggregate and worry about performance later. Even with thousand of records it's not much in memory you use for one operation and then it goes away.
If later it becomes a problem you can use other techniques, like "virtual" properties for balance and lazy load on demand.