c# - Emit Mapper Flattering and property name mismatch -
how map user class usermodel class using emit mapper?
public class user { public guid id { get; set; } public string firstname { get; set; } public string lastname { get; set; } public ilist<role> roles { get; set; } public company company { get; set; } } public class usermodel { public guid id { get; set; } public guid companyid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public ilist<rolemodel> roles { get; set; } }
there several problems:
- i need flatten object such have companyid instead of company object.
- company object has property id, in usermodel have companyid corresponds company id, property names not match.
- i need map
list<role>
list<rolemodel>
to flattened model check this example. seems default has convention of having sub class property name prefix in target.
source
public class sourceobject { public sourcesubobject someclass { get; set; } } public sourcesubobject { public int age { get; set; } }
target
public class target { public int someclassage { get; set; } }
second, 1 options let default settings copy properties can copy , manually rest
var target = objectmappermanager.defaultinstance.getmapper<source, target>().map(source); target.companyid = target.company.companyid;
or if need reuse mapping create custom mapper
custom mapper
private target converter(source source) { var target = new target(); target.companyid = source.company.companyid; return target; }
usage
var mapper = new defaultmapconfig().convertusing<source, target>(converter); var target = objectmappermanager.defaultinstance.getmapper<source, target>(mapper).map(source);
update
what comes role & rolemodel mapping. seems in case need have deep copy enabled , depending on class(es) definitions can either copy directly or custom mapping.
objectmappermanager.defaultinstance.getmapper<source, target>(new defaultmapconfig().deepmap<classtodeepmap>().deepmap<classtodeepmap>()).map(source, target);
Comments
Post a Comment