BT

Differing Opinions: DTOs vs Domain Objects

by Jonathan Allen on Jun 06, 2011 |

Since the introduction of NHibernate and WCF, .NET developers have been moving closer and closer to the concept of unified entity-models. The end game here is that the same class can be act as your ORM entity, your WCF DTO, and your model for a MVC, MVP, or MVVM framework. Mark Seemann, author of Dependency Injection in .NET, argues this isn’t necessarily a good thing.

The crux of his argument is that “At the Boundaries, Applications are Not Object-Oriented”. As you are undoubtedly aware, most serialization technologies require public, default constructors and writable properties. These requirements essentially force you to break encapsulation and the principals of data hiding when designing your DTOs. Even basic invariants such as requiring fields to be non-null/non-empty are impossible to enforce because the DTO could literally omit anything. He goes on to justify these two tenants:

  • Services share schema and contract, not class.
  • DTOs don’t break encapsulation because they aren’t objects at all.

Given this situation, Mark offers three options for moving forward:

One option is to stick with what we already have. To bridge the gap we must then develop translation layers that can translate the DTOs to properly encapsulated domain objects. This is the route I take with the samples in my book. However, this is a solution that more and more I’m beginning to think may not be the best. It has issues with maintainability. (Incidentally, that’s the problem with writing a book: at the time you’re done, you know so much more than you did when you started out… Not that I’m denouncing the book – it’s just not perfect…)

Another option is to stop treating data as objects and start treating it as the structured data that it really is. It would be really nice if our programming language had a separate concept of structured data… Interestingly, while C# has nothing of the kind, F# has tons of ways to model data structures without behavior. Perhaps that’s a more honest approach to dealing with data… I will need to experiment more with this…

A third option is to look towards dynamic types. In his article Cutting Edge: Expando Objects in C# 4.0, Dino Esposito outlines a dynamic approach towards consuming structured data that shortcuts auto-generated code and provides a lightweight API to structured data. This also looks like a promising approach… It doesn’t provide compile-time feedback, but that’s only a false sense of security anyway. We must resort to unit tests to get rapid feedback, but we’re all using TDD already, right?

If you are interesting in Object Oriented Design and Encapsulation don’t miss his series titled Poka-yoke Design: From Smell to Fragrance.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

I question your generalisation by Colin Jack

"Since the introduction of NHibernate and WCF, .NET developers have been moving closer and closer to the concept of unified entity-models. The end game here is that the same class can be act as your ORM entity, your WCF DTO, and your model for a MVC, MVP, or MVVM framework."

I'm not sure thats correct. Many/most of the .NET developers I've been following in the last few years have been arguing that this often isn't a good idea (other than for trivial cases). So I'm not sure we are moving closer to this end-game at all, in fact I'd hope the average .NET developer today is advanced enough to not walk straight into that anti-pattern.

Re: I question your generalisation by Jonathan Allen

Take a look at WCF RIA Services. We aren't just blundering into that pattern, .NET developers are demanding it and Microsoft is providing it.

Re: I question your generalisation by Colin Jack

Sure some will be, which is entirely different.

OOP is not successor of SP, but part of it by Adam Nemeth

Little known fact, that the mentioning of class construct is there at a book called Structured Programming, written by Dahl-Dijsktra-Hoare in the 60s (Dahl is the inventor of Simula, one of the first OOP languages, but the three of them together are credited with the invention of Structured Programming)

Also, if our world is modelled at the easiest by values and actions, there's nothing wrong with making this distinction; I don't get why it is an anti-pattern.

There are times when "OOP" models are better, but a lot of applications simply cannot enclose actions to the values they operate on; we could pretend such is not true, but still, we've learnt for decades that the distinction of action vs. value is alive.

Let's stop pretending it.

Thanks for heads up by Eugene Tolmachev

But I'd change the title to emphasize: DTOs vs Domain Object.
Also contrast "one DTO to rule them all" with CQRS and "use domain model for updates only" approaches and many writings on the subject. There are good reasons to have both and there are very good reasons to let external parties/requirements to drive the definition of DTOs.

Personally I have high expectations for automated translation between domain model and DTOs and keep an eye on proto-buffers for the purpose.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

5 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT