I like to cleanly separate public and domain objects (so, nHibernate isn\'t going to help here) from each other which ends up forcing me to write a lot of code to m
You might want to give AutoMapper a try. It sounds like what you're looking for.
There is also an interesting project called Otis. Below is the example *.otis.xml mapping taken from the documentation page:
<?xml version="1.0" encoding="utf-8" ?>
<otis-mapping xmlns="urn:otis-mapping-1.0">
<class name="Otis.Tests.UserDTO, Otis.Tests" source="Otis.Tests.Entity.User, Otis.Tests" >
<member name="Id" />
<member name="Age" />
<member name="UserName" expression="$UserName.ToUpper()" nullValue="[unknown]" />
<member name="FullName" expression="[$FirstName + ' ' + $LastName]" />
<member name="ProjectCount" expression="$Projects.Count" />
<member name="Title" expression="$Gender" >
<map from="Gender.Male" to="Mr." /> <!-- projections -->
<map from="Gender.Female" to="Mrs." />
</member>
<member name="Birthday" expression="$BirthDate" format="Born on {0:D}"/>
<member name="ProjectCount" expression="$Projects.Count" />
<member name="AvgTaskDuration" expression="avg:$Projects/Tasks/Duration" />
<member name="MaxTaskDuration" expression="max:$Projects/Tasks/Duration" />
</class>
To read the mapping files from the assembly:
// configure the new Configuration object using metadata of types in the current assembly
Configuration cfg = new Configuration(); // instantiate a new Configuration, one per application is needed
cfg.AddAssembly(Assembly.GetExecutingAssembly()); // initialize it
Hmm, where have I seen it before? ;)
So, seemingly dissatisfied with a runtime solution, I have written a small utility that will create the mappings in code. You can download the source below and write better error handling, etc., etc. I'd appreciate any cool modifications you make, this was made in haste, but works. Please respect that the code is being released under the LGPL.
Object To Object Mapping Utility Source Code
UPDATE 23 JUN 2009: I made some updates to the code that cleaned it up (a little bit) and also added the ability to save a mapping to a file so that you can later modify it.
use ValueInjecter, with it you can map anything to anything e.g.
and it has cool features like: flattening and unflattening