views:

3336

answers:

4

I've seen a lot of questions related to mapping DTOs to Domain Objects, but I didn't feel they answered my question. I've used many methods before and have my own opinions but I'm looking for something a little more concrete.

The Situation:

We have many domain objects. We are using a CSLA model so our domain objects can be pretty complex and they contain their own data access. You do not want to pass these around on the wire. We are going to be writing some new services that will return data in a number of formats (.Net, JSON, etc.). For this (and other reasons) we are also creating a lean, data transfer object to pass around on the wire.

My question is how should the DTO and Domain object be connected?

My first reaction is to use a Fowler, DTO pattern-type solution. I've seen this done many times and it feels right to me. The domain object contains no reference to the DTO. An outside entity (a "mapper" or "assembler") is called to create a DTO from a Domain Object. Normally there is an ORM on the domain object side. The downside of this is that the "mapper" tends to get extremely complex for any real situation and can be very fragile.

Another idea put forth is for the Domain Object to "contain" the DTO, since it's just a lean data object. The Domain Object properties would internally reference the DTO properties and could just return the DTO if asked for. I can see no problems with this but it feels wrong. I have seen some articles where people using NHibernate appeared to use this method.

Are there other ways? Is one of the ways above worth using? If so or if not, why?

Thanks for any insight in advance.

+8  A: 

You could use an automapper such as the one written by Jimmy Bogard which has no connection between the objects and relies on naming conventions being adhered to.

Garry Shutler
+1 for recommending AutoMapper.
Darin Dimitrov
Automapper could lead to accidentally exposed properties --> security hole. It would be better to say explicitly what should be exposed as a DTO.
deamon
@deamon: valid concern, but so is the bugs (and potential security holes because of human oversight) that can be created writing all that gooey mapping code. I'll go the automagic road and handle the 5% using it's built in custom mapping feature.
Merritt
+6  A: 

A benefit of having a mapper that sits between your domain and your DTO is not as appearent when you are only supporting a single mapping, but as the number of mappings increases, having that code isolated from the domain helps keep the domain simpler and leaner. You won't be cluttering your domain with a lot of extra weight.

Personally I try and keep the mapping out of my domain entities, and put the responsibility in my what I call my Manager / Service layer. This is a layer that sits between the application, and the respository(ies), and provides business logic such as workflow cordination (If you modify A, you might have to also modify B so service a will work with Service b).

If I had a lot of possible ending formats, I might look at creating a plugable formatter, that could use the visitor pattern for example to transform my entities but I've not found a need yet for anything this complex.

JoshBerke
A: 

You can also try Otis, an Object-to-object mapper. Concepts are similar to NHibernate mapping (attribute or XML).

http://code.google.com/p/otis-lib/wiki/GettingStarted

Brian Chavez
A: 

Another option is the beta version of ServiceToolkit.NET, which we started during our last project. Maybe it can help you: http://servicetoolkit.codeplex.com/

Julius Ganns