Search code examples
domain-driven-designcqrssolid-principles

Example of a AggregateRoot and single responsibility principle


I am doing a bit of research into DDD, CQRS and ES. Looking at this source code of a aggregate root, I am just curious whether this does not violate the single responsibility principle? Or is this to be expected for a aggregate root/DDD? Any feedback would be very much appreciated. Thanks a lot.


Solution

  • Another way to think of SRP is "there must be a single reason for this code to change". If there is more than one reason then we can say it's an SRP violation.

    So looking at the code I can see a few reasons it might change:

    Additional account information is required to be stored, more than just an account name. Customer services need information to contact the account holder, address, email, phone number.

    The memento object deserialization needs to become more complex. At the moment it's only a single pipe | separating amount and account:

     var split = ledger.Value.Split(new[] { '|' });
    

    There's a number of events, AccountOpenedEvent AccountClosedEvent etc. this class could change if there are more events required in future, e.g. AccountSuspendedEvent.

    I could probably go on, but yes this class violates SRP in my opinion. Does it need to be changed now? No, probably not, I would hope there is a decent test suite (this does here) and just refactor later as a first step before implementing a change.