c# - replace static AutoMapper API; replace a Map method inside a Profile -
i'm replacing static automapper api:
so, before had profile
like:
public class digitalresourceprofile : automapper.profile { protected override void configure() { automapper.mapper.createmap<dto, domain>(); automapper.mapper.createmap<domain, dto>() .formember(dst => dst.attachs, opts => opts.mapfrom(src => automapper.mapper.map<list<attachdomain>, list<attachdto>>(src.attachs))) .formember(dst => dst.timestamp, opts => opts.mapfrom(s => s.timestamp.touniversaltime())); automapper.mapper.createmap<attachdto, attachdomain>(); automapper.mapper.createmap<attachdomain, attachdto>() .formember(dst => dst.timestamp, opts => opts.mapfrom(s => s.timestamp.touniversaltime())); } }
from on, profile class like:
public class digitalresourceprofile : automapper.profile { protected override void configure() { this.createmap<dto, domain>(); this.createmap<domain, dto>() >>>>>>>>>>>>>>>>>.formember(dst => dst.attachs, opts => opts.mapfrom(src => automapper.mapper.map<list<attachdomain>, list<attachdto>>(src.attachs))) .formember(dst => dst.timestamp, opts => opts.mapfrom(s => s.timestamp.touniversaltime())); this.createmap<attachdto, attachdomain>(); this.createmap<attachdomain, attachdto>() .formember(dst => dst.timestamp, opts => opts.mapfrom(s => s.timestamp.touniversaltime())); } }
however, dont know how replace .formember(dst => dst.attachs, opts => opts.mapfrom(src => automapper.mapper.map<list<attachdomain>, list<attachdto>>(src.attachs)))
.
any ideas?
instead of using static api, can pass mapping engine through profile contructor.
you find below unit test using unity.
[testclass] public class unittest1 { public class testa { public testc valuea { get; set; } } public class testb { public testd valueb { get; set; } } public class testc { public string value { get; set; } } public class testd { public string value { get; set; } } private readonly iunitycontainer appcontainer = new unitycontainer(); public sealed class mappingconfiguration : profile { private readonly imappingengine mappingengine; // inject mapping engine through constructor public mappingconfiguration(imappingengine mappingengine) : base() { this.mappingengine = mappingengine; } protected override void configure() { createmap<testa, testb>() .formember(dest => dest.valueb, o => o.resolveusing(src => this.mappingengine.mapper.map<testd>(src.valuea))); createmap<testc, testd>(); } } [testmethod] public void mappingtest() { // automapper configuration var config = new mapperconfiguration(cfg => { cfg.constructservicesusing(type => appcontainer.resolve(type)); }); var mapper = config.createmapper(); appcontainer.registerinstance<iconfiguration>(config); appcontainer.registertype<imappingengine, mappingengine>(new injectionconstructor(config, mapper)); appcontainer.resolve<iconfiguration>().addprofile(new mappingconfiguration(appcontainer.resolve<imappingengine>())); // check config mapper.configurationprovider.assertconfigurationisvalid(); var testin = new testa { valuea = new testc { value = "this test" } }; var result = mapper.map<testb>(testin); } }
Comments
Post a Comment