Object Property Mapping Using Reflection

By Gord at April 17, 2010 00:18
Filed Under: .Net Framework 4.0

The new Entity Framework 4.0 has the capability of keeping the entity objects in a separate project from the data definitions (.edmx file) making it possible to build proper n-tier applications – bing POCO for more info on this topic.  This was all well and good until i realized that a change in the database tables would more or less necessitate a change in the entity objects.  While many people may consider this to be a non-issue, I had hoped that I could use these entity objects in a WCF service.  In my situation, I could simply change the data contract of the WCF service whenever I want as this would force clients to update at the same time – that wasn’t going to happen.  As a result, I was forced to make separated data transfer objects (DTOs) specifically for the WCF service being built.  In doing so, I was built up a fun chunk of code that uses reflection and a custom configuration section to dynamically map objects of any type.

 

The ObjectMappingUtility project is very easy to use in code.  Below is an example of how to map from values from one object to another in code.

 

SourceTestObject sourceObject = new SourceTestObject()
{
    sourceStringValue1 = "SV1",
    sourceStringValue2 = "SV2",
    sourceIntValue1 = 99,
    sourceIntValue2 = 42,
    sourceLongValue1 = 6543L,
    sourceLongValue2 = 3456L
};

DestinationTestObject destinationObject = new DestinationTestObject();

destinationObject = (DestinationTestObject)
ObjectMappingUtility.MapObjects(sourceObject, destinationObject);

 

To tell the utility what properties to map, you simply have to add some configuration options to your App.config or Web.config file.  First you need to add the configuration section:

 

  <configSections>
    <sectionGroup name="objMapCustomConfig">
      <section name="objectMappingConfiguration" type="ObjectMapping.ObjectMappingConfiguration,
ObjectMapping,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null
"/> </sectionGroup> </configSections>

 

Then your object and property mappings:

 

  <objMapCustomConfig>
    <objectMappingConfiguration>
      <objectMappings>
        <add name="ObjectMappingUtilityTests.SourceTestObject_
ObjectMappingUtilityTests.DestinationTestObject
"
sourceType="ObjectMappingUtilityTests.SourceTestObject"
destinationType
="ObjectMappingUtilityTests.DestinationTestObject" > <propertyMappings> <add sourceProperty="sourceStringValue1" destinationProperty="destinationStringValue1" /> <add sourceProperty="sourceStringValue2" destinationProperty="destinationStringValue2" /> <add sourceProperty="sourceIntValue1" destinationProperty="destinationIntValue1" /> <add sourceProperty="sourceIntValue2" destinationProperty="destinationIntValue2" /> <add sourceProperty="sourceLongValue1" destinationProperty="destinationLongValue1" /> <add sourceProperty="sourceLongValue2" destinationProperty="destinationLongValue2" /> </propertyMappings> </add> <add name="ObjectMappingUtilityTests.SourceTestObject2_
ObjectMappingUtilityTests.DestinationTestObject2
"
sourceType
="ObjectMappingUtilityTests.SourceTestObject2"
destinationType="ObjectMappingUtilityTests.DestinationTestObject2"> <propertyMappings> <add sourceProperty="sourceStringValue1" destinationProperty="destinationStringValue1" /> <add sourceProperty="sourceIntValue1" destinationProperty="destinationIntValue1" /> <add sourceProperty="sourceLongValue1" destinationProperty="destinationLongValue1" /> </propertyMappings> </add> </objectMappings> </objectMappingConfiguration> </objMapCustomConfig>

 

The only real restriction here is the “name” of the mappings must be the object.GetType().FullName value of the source object followed by the destination object separated with an underscore.

 

When I built this utility, the point was to map Entity Framework POCOs (Plain Old CLR Objects) to specific DTOs (Data Transport Objects) for my WCF service.  If you are using EF POCOs you need to understand that EF builds POCOs with the object type full name System.Data.Entity.DynamicProxies.myObject_a8e7b8e8a6f812.  When using this utility just use the System.Data.Entity.DynamicProxies.myObject and it will handle removing the junk at the end.

 

You can download the code here: ObjectMappingUtility.zip (83.56 kb)

About the author

Gord graduated from Carleton University with a bachelors of Computer Science with minors in both Philosophy and Mathematics.  His post graduate career started in 2005 as a Systems Administrator until becoming a .Net Web Developer 2 years later.  Since then, he has moved on to being a more generic Systems Developer, focusing on intersystem communication, design, and architecture.

Page List