I'm implementing a feature in legacy system right now. Instead of putting all my logic in some dummy application services and having anemic models, I thought I will try to do some OOP this time.
I'm not using DDD, but trying to follow DDD-like-style a little bit.
So imagine a situation where you have:
public class CustomerAccount { private CustomerAccountId customerAccountId; Collection<AccountEntry> accountHistory; (or maybe some AccountHistory class) } now, our Account has some domain logic. It should be able to calculate total debit, credit and averageBalance within some time period.
So it would be like following
public Money calculateTotalDebit(Date start, Date end) // or maybe some TimeInterval, it doesn't matter public Money calculateTotalCredit(Date start, Date end) public Money calculateAverageBalance(Date start, Date end, FancyCalculationStrategy strategy) It also has some constraint that should be preserved while invoking other operations like addAccountEntry(AccountEntry entry)
So I can assume, that my Account is something like DDD's aggregate root. Obviously, it's not real aggregate root, because I'm not applying DDD, but let's just say I see some similarity.
And now my Account class grows - there's quite a lot logic in each of those methods. How is this related to Single Responsibility Principle? A class should have only one reason to change, but in this case, there are more. Or maybe not? Somehow I feel all this logic should belong to one place.
Yeah, I can create AccountHistory class, which encapsulates the behaviour, but this doesn't solve the problem - in that case, AccountHistory class will start to grow.
What I can do, is just to create some, let's say
AverageBalanceCalculator and delegate the behaviour.
But isn't it going towards anemic model again? This calculator will probably have no state and it will behave just like a service.
Of course it doesn't have to be visible to end-user - from the implementation point of view, it could be a package private class.
How is SRP related to aggregates root and other DDD's tactical patterns in general?
I'm little confused, would appreciate some answers.