In this post, I am trying to compare and contrast two mindsets , One which believes that everything in the end boils down to a CRUD operation and one which believes in modelling behavior .
Till not so long ago, I believed that everything boils down to a CRUD operation, this mindset reflected in the solutions I modeled for a given requirement, I had kind of heard about behaviorally rich models, however I just could not figure out how they were of any use, given that everything in the end is a CRUD operation :)
After having been introduced to DDD and reading a few books and posts on it, I seem to be getting a hang of it and in this post, I am going to take a simple requirement and then model it with a anemic/CRUD mindset and then a behaviorally rich/DDD mindset.
The requirement is fairly simple we are trying to save the details of a customer.
First to go CRUD mind set. You can find the gist here
I am sure this looks familiar to most of us.
So what is the problem with this approach. At the very least the below four
1) There is little intention revealed by the saveCustomer method.
2) The customer class is more like a DTO rather than a domain class.
3) This method can be used more than a dozen business situations, so how does one unit test this method ?
4) This method will need to be changed in more than a dozen cases, it clearly violates SRP.
So now lets go for the behaviorally rich approach.
The customer class may start looking like the below. Have a look at the gist here, it includes a snippet of the application service too.
Now with this kind of modelling, each method has only 1 reason to change :)
We know the intent of each method.
This API also supports a task based UI.
So now the question that arises is, which approach should we take ?
The answer is "it depends on the context" :) .... If the context we are working on is the core domain we should probably tend towards a behaviorally rich model, else in a lot of cases a anemic approach may turn out just fine.
The behaviorally rich model does involve more thought and work, but in ends up with a much cleaner and more maintainable code base.
The most important thing to take home is that we should debate and discuss if everything should be blindly modeled in a anemic fashion, we do have a very real and appealing alternative , Lets consider it !!!
Disclaimer : A lot of it is directly picked up from the IDDD book ;)
Till not so long ago, I believed that everything boils down to a CRUD operation, this mindset reflected in the solutions I modeled for a given requirement, I had kind of heard about behaviorally rich models, however I just could not figure out how they were of any use, given that everything in the end is a CRUD operation :)
After having been introduced to DDD and reading a few books and posts on it, I seem to be getting a hang of it and in this post, I am going to take a simple requirement and then model it with a anemic/CRUD mindset and then a behaviorally rich/DDD mindset.
The requirement is fairly simple we are trying to save the details of a customer.
First to go CRUD mind set. You can find the gist here
I am sure this looks familiar to most of us.
So what is the problem with this approach. At the very least the below four
1) There is little intention revealed by the saveCustomer method.
2) The customer class is more like a DTO rather than a domain class.
3) This method can be used more than a dozen business situations, so how does one unit test this method ?
4) This method will need to be changed in more than a dozen cases, it clearly violates SRP.
So now lets go for the behaviorally rich approach.
The customer class may start looking like the below. Have a look at the gist here, it includes a snippet of the application service too.
Now with this kind of modelling, each method has only 1 reason to change :)
We know the intent of each method.
This API also supports a task based UI.
So now the question that arises is, which approach should we take ?
The answer is "it depends on the context" :) .... If the context we are working on is the core domain we should probably tend towards a behaviorally rich model, else in a lot of cases a anemic approach may turn out just fine.
The behaviorally rich model does involve more thought and work, but in ends up with a much cleaner and more maintainable code base.
The most important thing to take home is that we should debate and discuss if everything should be blindly modeled in a anemic fashion, we do have a very real and appealing alternative , Lets consider it !!!
Disclaimer : A lot of it is directly picked up from the IDDD book ;)