Finally, I implemented the original requirement by my own although I didn't need it anyways (requirement changed). I'll provide the code here in case somebody needs it (and more or less as proof of concept, as a lot of improvement can still be done) or is interested in in, bear in mind, that the XML is a error prone, crucial component in this approach, property names and types have to match exactly, otherwise it wont work, but with a little GUI to edit the file this should be achieveable (I mean not editing the file manually).
I used code from here and here and added class PropertyMapping to store the mappings read from the XML and also classes Foo and Bar to create a nested data structure, to copy to.
Anyways here is the code, maybe it helps somebody some time:
Main:
public class Program
{
public static void Main(string[] args)
{
// Model
var calendarEvent = new CalendarEvent
{
EventDate = new DateTime(2008, 12, 15, 20, 30, 0),
Title = "Company Holiday Party"
};
MyObjectMapper mTut = new MyObjectMapper(@"SampleMappings.xml");
Console.WriteLine(string.Format("Result MyMapper: {0}", Program.CompareObjects(calendarEvent, mTut.TestMyObjectMapperProjection(calendarEvent))));
Console.ReadLine();
}
public static bool CompareObjects(CalendarEvent calendarEvent, CalendarEventForm form)
{
return calendarEvent.EventDate.Date.Equals(form.EventDate) &&
calendarEvent.EventDate.Hour.Equals(form.EventHour) &&
calendarEvent.EventDate.Minute.Equals(form.EventMinute) &&
calendarEvent.Title.Equals(form.Title);
}
}
Mapper implementation:
public class MyObjectMapper
{
private List<PropertyMapping> myMappings = new List<PropertyMapping>();
public MyObjectMapper(string xmlFile)
{
this.myMappings = GenerateMappingObjectsFromXml(xmlFile);
}
/*
* Actual mapping; iterate over internal mappings and copy each source value to destination value (types have to be the same)
*/
public CalendarEventForm TestMyObjectMapperProjection(CalendarEvent calendarEvent)
{
CalendarEventForm calendarEventForm = new CalendarEventForm();
foreach (PropertyMapping propertyMapping in myMappings)
{
object originalValue = GetPropValue(calendarEvent,propertyMapping.FromPropertyName);
SetPropValue(propertyMapping.ToPropertyName, calendarEventForm, originalValue);
}
return calendarEventForm;
}
/*
* Get the property value from the source object
*/
private object GetPropValue(object obj, String compoundProperty)
{
foreach (String part in compoundProperty.Split('.'))
{
if (obj == null) { return null; }
Type type = obj.GetType();
PropertyInfo info = type.GetProperty(part);
if (info == null) { return null; }
obj = info.GetValue(obj, null);
}
return obj;
}
/*
* Set property in the destination object, create new empty objects if needed in case of nested structure
*/
public void SetPropValue(string compoundProperty, object target, object value)
{
string[] bits = compoundProperty.Split('.');
for (int i = 0; i < bits.Length - 1; i++)
{
PropertyInfo propertyToGet = target.GetType().GetProperty(bits[i]);
propertyToGet.SetValue(target, Activator.CreateInstance(propertyToGet.PropertyType));
target = propertyToGet.GetValue(target, null);
}
PropertyInfo propertyToSet = target.GetType().GetProperty(bits.Last());
propertyToSet.SetValue(target, value, null);
}
/*
* Read XML file from the provided file path an create internal mapping objects
*/
private List<PropertyMapping> GenerateMappingObjectsFromXml(string xmlFile)
{
XElement definedMappings = XElement.Load(xmlFile);
List<PropertyMapping> mappings = new List<PropertyMapping>();
foreach (XElement singleMappingElement in definedMappings.Elements("mapping"))
{
mappings.Add(new PropertyMapping(singleMappingElement.Element("src").Value, singleMappingElement.Element("dest").Value));
}
return mappings;
}
}
My model classes:
public class CalendarEvent
{
public DateTime EventDate { get; set; }
public string Title { get; set; }
}
public class CalendarEventForm
{
public DateTime EventDate { get; set; }
public int EventHour { get; set; }
public int EventMinute { get; set; }
public string Title { get; set; }
public Foo Foo { get; set; }
}
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar
{
public DateTime InternalDate { get; set; }
}
Internal mapping representation:
public class PropertyMapping
{
public string FromPropertyName;
public string ToPropertyName;
public PropertyMapping(string fromPropertyName, string toPropertyName)
{
this.FromPropertyName = fromPropertyName;
this.ToPropertyName = toPropertyName;
}
}
Sample XML configuration:
<?xml version="1.0" encoding="utf-8" ?>
<ObjectMapping>
<mapping>
<src>Title</src>
<dest>Title</dest>
</mapping>
<mapping>
<src>EventDate.Date</src>
<dest>EventDate</dest>
</mapping>
<mapping>
<src>EventDate.Hour</src>
<dest>EventHour</dest>
</mapping>
<mapping>
<src>EventDate.Minute</src>
<dest>EventMinute</dest>
</mapping>
<mapping>
<src>EventDate</src>
<dest>Foo.Bar.InternalDate</dest>
</mapping>
</ObjectMapping>