c# - Most efficient way to write AutoMapper code? -
i gave automapper shot, , once got working it's absolutely fantastic. issue actual code have write setup automapper kind of gross, , i'm wondering if way i'm doing can improved upon or simplified. i'm using (in following example) convert entity viewmodel. field names entirely different, have manually create mapping (afaik)
public static class automapperconfig { public static void registermappings() { mapper.initialize(cfg => { cfg.createmap<customer_master, customerviewmodel>() .formember (dst => dst.firstname, src => src.mapfrom(e => e.cm_firstname)) .formember (dst => dst.lastname, src => src.mapfrom(e => e.cm_lastname)) .formember (dst => dst.id, src => src.mapfrom(e => e.cm_id)) .formember (dst => dst.address1, src => src.mapfrom(e => e.cm_address1)) .formember (dst => dst.address2, src => src.mapfrom(e => e.cm_address2)) .formember (dst => dst.city, src => src.mapfrom(e => e.cm_city)) .formember (dst => dst.state, src => src.mapfrom(e => e.cm_state)) .formember (dst => dst.zip, src => src.mapfrom(e => e.cm_zip)) .formember (dst => dst.country, src => src.mapfrom(e => e.cm_country)) .formember (dst => dst.email, src => src.mapfrom(e => e.cm_email)) .formember (dst => dst.phone, src => src.mapfrom(e => e.cm_phone)) .formember (dst => dst.fax, src => src.mapfrom(e => e.cm_fax)) .formember (dst => dst.active, src => src.mapfrom(e => e.cm_active)) .formember (dst => dst.company, src => src.mapfrom(e => e.cm_company)); cfg.createmap<customerviewmodel, customer_master>() .formember (dst => dst.cm_firstname, src => src.mapfrom(e => e.firstname)) .formember (dst => dst.cm_lastname, src => src.mapfrom(e => e.lastname)) .formember (dst => dst.cm_id, src => src.mapfrom(e => e.id)) .formember (dst => dst.cm_address1, src => src.mapfrom(e => e.address1)) .formember (dst => dst.cm_address2, src => src.mapfrom(e => e.address2)) .formember (dst => dst.cm_city, src => src.mapfrom(e => e.city)) .formember (dst => dst.cm_state, src => src.mapfrom(e => e.state)) .formember (dst => dst.cm_zip, src => src.mapfrom(e => e.zip)) .formember (dst => dst.cm_country, src => src.mapfrom(e => e.country)) .formember (dst => dst.cm_email, src => src.mapfrom(e => e.email)) .formember (dst => dst.cm_phone, src => src.mapfrom(e => e.phone)) .formember (dst => dst.cm_fax, src => src.mapfrom(e => e.fax)) .formember (dst => dst.cm_active, src => src.mapfrom(e => e.active)) .formember (dst => dst.cm_company, src => src.mapfrom(e => e.company)); }); } }
then in global.asax
:
automapperconfig.registermappings();
mostly questions are:
- do field names have explicitely mapped because names different?
- is there way write first map , use second well? entity -> viewmodel same viewmodel -> entity, seems redundant write these twice. guess i'm looking sort of bidirectional option here.
it seems conversions follow same pattern: member x maps member cm_x. if case, can simplify code this:
mapper.initialize(cfg => { // remove "cm_" destination when matching // fact have different casing (address vs cm_address) // fine , handled cfg.recognizedestinationprefixes(new [] {"cm_"}); // remove "cm_" source when mapping cfg.recognizeprefixes(new[] { "cm_" }); // map in both directions cfg.createmap<entity, dto>().reversemap(); });
Comments
Post a Comment