Currently in doing some code review of stuff taken over from another team and have one doubt about applying SRP and its relation to anemic or rich domain model (as defined by Martin Fowler). Rich domain model concept is to have intelligent object that can not only set/get their properties but also can perform some more complicated business logic. I wonder how it fits into SRP?
Say I have my model class having some properties that can expose those props and provide some simple calculations on its properies. Next requirement is to have possibility to store this object data in some storage object that is not under my control, like this:
class MyObject {
// get set
// parse sth
}
Store method in storage
storage.store(key, object);
Doesn't it violate SRP if MyObject has store method like this
public void store(Storage storage) {
storage.store('keyOne', fieldOne);
storage.store('keyTwo', fieldTwo);
}
From this object's pov it is a good think to be able to store its state. Other way could be to introduce sort of service here and do this like that:
public StorageService {
private Storage;
// constructor here
....
public void store(MyObject myobj);
}
Can you point me any links I can read about this problem? I've found one thread on SO here but it doesn't answer my question completely.
How is it resolved in DDD? Models in DDD are by definition rich and can be seen as having too many responsibilities.
A Rich Domain Model is the technical part when applying DDD, it envolves the building blocks like Entity, Value Objects and Aggregate Root. The goal is to build a ubiquitous language between developers and stakeholders using the a vocabulary that describes the business rules.
A rich domain model (RDM) means that the logic governing the model's behavior belongs within the model, as opposed to treating the model like data with getters/setters. This does not mean everything including persistence, security, how to display the model in the GUI, etc. needs to be within the model.
RDM and SRP go hand-in-hand, they do not conflict with each other.
Violating SRP/RDM:
Car {
// possibly violates SRP
storeInDatabase();
// smells like anemic domain model
getEngineState();
}
Following SRP/RDM:
// usings aspects to remove cross-cutting concerns from the model and follow SRP
@DatabaseSerializable
Car {
// rich domain model encapsulates engine state and exposes behavior
drive();
}
"Models in DDD are by definition rich and can be seen as having too many responsibilities" is a simplistic interpretation of DDD. Always it depends in how good are your models. You could create bad models using DDD (for example creating objects with too many responsabilities or creating anemic models). DDD and SRP are two good practices too follow, as much as refactoring, TDD and many more, but you should complement their use with your experience and judgement (or someone else's). Everything has its pros and cons, don't be dogmatic about applying any practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With