c# - Generic Business to ORM object mapping functions -
c# - Generic Business to ORM object mapping functions -
i have 2 objects, businesscustomer , ormcustomer
i want able map 1 other in business layer activities like
say if load ormcustomer db, want populate businesscustomer or if working on businesscustomer , want persist db want populate ormcustomerin business layer want able go like:
mapper.mapcustomer(src, target)
mapcustomer method able decide direction of mapping businesscustomer->ormcustomer or ormcustomer->businesscustomer
i've been tinkering around generics can't seem find appropriate neat solution of how implement in mapper class.
internal void mapcustomer<t, k>(t src, k target) { if (src.gettype() == typeof(businesscustomer)) { mapbusinesscustomertoormcustomer(src, target); } else if (src.gettype() == typeof(ormcustomer)) { mapormcustomertobusinesscustomer(src, target); } }
any thoughts how best implement this?
here simple wrote such task. property names need same.
public static class typeconverter { /// <summary> /// instantiates new destinationtype copying public properties same type , name sourcetype. /// destination object must have parameter less constructor. /// </summary> /// <param name="sourceobject">the source object.</param> /// <param name="destinationtype">type of destination object.</param> public static destinationtype convert<sourcetype, destinationtype>(sourcetype sourceobject, type destinationtype) { if (destinationtype.getconstructors().where(x => x.getparameters().count() == 0).count() == 0) throw new exception("a parameter less constructor required destination type."); // instantiate destination object destinationtype destinationobject = activator.createinstance<destinationtype>(); // public properties source , destination object ienumerable<propertyinfo> sourceprops = sourceobject.gettype().getproperties().where(x => x.propertytype.ispublic); ienumerable<propertyinfo> destinationprops = destinationtype.getproperties().where(x => x.propertytype.ispublic || x.propertytype.isenum); // re-create public properties exist in both source type , destination type foreach (propertyinfo prop in destinationprops) { propertyinfo sourceprop = sourceprops.singleordefault(x => x.name == prop.name); if (sourceprop != null) { seek { object propvalue = new object(); if (prop.propertytype.isenum) { propvalue = enum.parse(prop.propertytype, sourceprop.getvalue(sourceobject, null).tostring()); } else { propvalue = system.convert.changetype(sourceprop.getvalue(sourceobject, null), prop.propertytype); } prop.setvalue(destinationobject, propvalue, null); } grab { } } } homecoming destinationobject; } /// <summary> /// instantiates new destinationtype copying public properties same type , name sourcetype. /// destination object must have parameter less constructor. /// </summary> /// <param name="sourceobject">the collection of source objects.</param> /// <param name="destinationtype">type of destination object.</param> public static ienumerable<destinationtype> convert<sourcetype, destinationtype>(ienumerable<sourcetype> sourceobjects, type destinationtype) { list<destinationtype> convertedobjecs = new list<destinationtype>(); list<sourcetype> sourceobjectlist = sourceobjects.tolist(); foreach (sourcetype item in sourceobjectlist) convertedobjecs.add(convert<sourcetype, destinationtype>(item, destinationtype)); homecoming convertedobjecs; } }
c# generics orm
Comments
Post a Comment