So one thing you do not hear much about is inheritance with DDD. Currently I have an Account
aggregate that handles details regarding a financial account and its subaccounts. Then a Transaction
aggregate is used that maintains the invariants related to a financial transaction. The transaction is not a split transaction (yet), but links a credited account and debited account (via entity IDs).
When I compute the balance of an account, I use a domain service that takes in an AccountId
and uses the AccountRepository
and TransactionRepository
to compute the balance.
So far so good, until I get to the idea of tracking investment accounts. This changes how the balance is computed. I need to use the last known share price with the amount of shares purchased to compute the balance.
So with different behavior, it makes sense to have an InvestmentAccount
class that inherits from Account
. However, I'm stuck on how to handle this with regards to using inheritance/polymorphism.
I do not want to have switch statements littered through my code so I thought polymorphism would be the answer. However, re-instantiating a supertype isn't as easy as I suspected.
Am I missing a separate aggregate here? I had the idea of using the Strategy pattern to inject a different algorithm for computing the balance. But behavior isn't the only different. I still need a way to track shares vs dollar amounts so that only gets me half of the way. This makes me think I need another class InvestmentAccount
.