DTO’s, DDD & The Anemic Domain Model
I had a comment on my last post Tips for ORM Data Access which i would like like to address with this blog post.
I have been trying to wrap my head around the role of the DTO in DDD. My reading of Fowler and Evans seems to indicate that you ought to have your domain objects themselves mapping into your database, rather than dedicated function-less DTOs. Relying on DTOs that are then handled by Services seems to lead to what Fowler calls The Anemic Domain Anti-Pattern: http://martinfowler.com/bliki/AnemicDomainModel.html
However, I have a tough time writing Entity classes that operate in that manner that don?t end up rather painful to change and extend.
Since you recommend the practice of using DTOs, do you have any thoughts on the subject?
Thank you for the question Scott, of course I have thoughts on this 🙂
Disclaimer: This sort of architecture is not applicable to many systems, use the right patterns and tools for the job!
I agree that an anemic domain model is bad, if there is no behavior then what?s the point right? Let?s make sure I am on the same page here:
DTO: To me, a DTO moves data between ?tiers?. They are the packaged data ready for transport. A WCF data contract is a perfect example of a DTO, I also see a JSON object as a DTO.
Read model: This would be a different model than your real Domain model. A Read model is very lightweight, ?thin? and anemic. Its purpose is to serve aggregated data to a specific screen or message. A DTO, to me, can be a read model, as too could be a View Model.
The domain model is rich and full of behavior. This model is most valuable when performing complex business rules during the saving and updating of data within a given transaction. It can also be used to read data too, but consider this contrived example:
Let?s say that we want to display the top 10 products. The products include the Manufacturer Name, Product Name, Vendor Name, Product Price, Customer Ranking. Being that we are good modelers we come up with something like the following entities: Vendor, Manufacturer, Product, & ProductRanking (maybe localization & currency support tables too).
If I use my domain model to get this data, I am going to end up retrieving quite a bit more data than I actually need, which could degrade performance. Not to mention having to deal with dot notation everywhere foo.Name =a.b.c.d.
We only need 5 fields, and they are immutable for this operation.
My preference is to materialize the read model (DTO, View Model) by projecting from the Domain Model, or by using a Stored Procedure for more complicated recursive, spatial or temporal queries.
Splitting the models allows the reads & writes to fluctuate independently, so which leads to higher maintainability. These models can also run on different tiers/nodes to increase scalability (read/cache tier, write tier).
At some point, whether off a view or an inbound DTO, there will be mapping back into the domain model. This ?friction? or ?impedance? is pretty easy to manage using an assembler/translator, or a tool like AutoMapper.
Greg Young & Udi Dahan take this concept further and apply a programming principle called Command-Query Separation with distributed programming and SOA. I think it is very good stuff.
Here are some posts that are all somewhat related: