AutoMapper property name conversions -
i'm trying register mapping convention handle mapping classes pascal case names classes underscore names postfix , prefix, , again. i've tried follow examples, cannot head around how it's supposed work.
this 1 of many things i've tried, looks should work (in opinion :)), doesn't seem anything:
public class pascalcaseentity { public string callingsystem { get; set; } } public class underscorewithprefixandpostfixentity { public string p_calling_system_ { get; set; } } public class partsmappings { public void apply() { mapper.initialize(cfg => { cfg.addprofile<fromunderscoremapping>(); cfg.addprofile<tounderscoremapping>(); cfg.createmap<pascalcaseentity, underscorewithprefixandpostfixentity>() .withprofile("tounderscoremapping"); cfg.createmap<underscorewithprefixandpostfixentity, pascalcaseentity>() .withprofile("fromunderscoremapping"); }); } } public class fromunderscoremapping : profile { protected override void configure() { recognizeprefixes("p_"); recognizepostfixes("_"); sourcemembernamingconvention = new lowerunderscorenamingconvention(); destinationmembernamingconvention = new pascalcasenamingconvention(); } public override string profilename { { return "fromunderscoremapping"; } } } public class tounderscoremapping : profile { protected override void configure() { recognizedestinationprefixes("p_"); recognizedestinationpostfixes("_"); sourcemembernamingconvention = new pascalcasenamingconvention(); destinationmembernamingconvention = new lowerunderscorenamingconvention(); } public override string profilename { { return "tounderscoremapping"; } } }
what missing here?
i found working solution. created 2 profiles, 1 each "direction", , added mappings them.
i'm not happy it, since i'd rather have mappings in same file (grouping them on business area). @ least works... :)
i tried putting registrations in same profile, , using .withprofile("tounderscorewithprefix") method, didn't work.
mapper.initialize(cfg => { cfg.addprofile(new tounderscorewithprefixmappings()); cfg.addprofile(new fromunderscorewithprefixmappings()); }); public class tounderscorewithprefixmappings : profile { protected override void configure() { recognizedestinationprefixes("p", "p"); sourcemembernamingconvention = new pascalcasenamingconvention(); destinationmembernamingconvention = new lowerunderscorenamingconvention(); createmap<pascalcaseentity, underscorewithprefixandpostfixentity>(); } public override string profilename { get; } = "tounderscorewithprefix"; } public class fromunderscorewithprefixmappings : profile { protected override void configure() { recognizeprefixes("p_", "p_"); recognizepostfixes("_"); sourcemembernamingconvention = new lowerunderscorenamingconvention(); destinationmembernamingconvention = new pascalcasenamingconvention(); createmap<underscorewithprefixandpostfixentity, pascalcaseentity>(); } public override string profilename { get; } = "fromunderscorewithprefix"; }
Comments
Post a Comment