Are there any frameworks or utilities (in the .NET space) for automatically generating data transfer objects from business objects

StackOverflow https://stackoverflow.com/questions/2414312

Question

I'm looking for the best approach for generating data transfer objects from business objects (the type definition, not mapping the data) using a set of conventions (e.g. all public properties), and possibly configurable to determine how deep to go.

I realize that this may not be possible or even desirable for many cases where the dto's don't resemble the business objects (such as when flattening a number of business objects into a single data transfer object).

However, in my case, a large percentage of my data transfer objects are very similar to their business objects (especially for the DTO's for updating the data).

I was wondering whether there are any existing code generation tools (I'm looking for specific solutions with existing templates, not just a general purpose code gen tool like CodeSmith), or dynamic assembly creation tools (e.g. using Reflection Emit under the covers)? Or if this is something you've done before I'd be interested to hear about what technique you used to help me decide between the various options.

Was it helpful?

Solution

You are likely to find AutoMapper quite useful: http://www.codeplex.com/AutoMapper

Edit, in response to OP comment:

T4 templates is included in Visual Studio for code generation. Here is a post on entity-to-DTO code generation with T4:

http://weblogs.asp.net/cibrax/archive/2009/03/11/code-generation-with-t4-an-entities-to-dto-example.aspx

OTHER TIPS

Well to some extent the .NET framework itself can do this with anonymous classes.

For example with Linq to Objects (your business objects for example) you can "project" your result sets into (lists of) anonymous classes.

The main shortcoming of this is right now they would not be very portable, however with .NET 4.0 and dynamic classes I daresay you would not need to use a 3rd party solution, I reckon you would have the tools with the framework itself.

I would also recommend T4 Templates. You can use reflection to feed in metadata and the actual template looks similar to asp markup. This is built into VS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top