AutoMapper and Windsor -
AutoMapper and Windsor -
i using automapper itypeconverter , want maintain within castle windsor. in 1 assembly have itypecoverter , load them using approach:
container .register(types.fromassembly(assembly.getexecutingassembly()) .basedon(typeof(itypeconverter<,>)));
so far can see windsor loading converters properly.
then register automapper within windsor
mapper.initialize(m => m.constructservicesusing(container.resolve)); container.register(component.for<imappingengine>().instance(mapper.engine));
but when inquire instance of imappingengine conventions not loaded. miss here?
i got automapper , windsor play nicely both profiles , typeconverters. think should agnostic plenty you, since automapper automatically pick implementations of itypeconverter.
let me know if solves problem.
first registered automapper windsor:
class="lang-cs prettyprint-override">public class automapperinstaller : iwindsorinstaller { public void install(iwindsorcontainer container, iconfigurationstore store) { // replace whatever assembly contains itypeconverter, ivalueresolver , profile implementations var myassembly = classes.fromassemblynamed("myassembly"); // register automapper such uses singleton configuration container.register( myassembly.basedon(typeof(itypeconverter<,>)).withserviceself(), myassembly.basedon<ivalueresolver>().withservicebase(), myassembly.basedon<profile>().withservicebase(), component.for<ienumerable<iobjectmapper>>().usingfactorymethod(() => mapperregistry.mappers), component.for<configurationstore>() .lifestylesingleton() .usingfactorymethod(x => { var typemapfactory = x.resolve<itypemapfactory>(); var mappers = x.resolve<ienumerable<iobjectmapper>>(); configurationstore configurationstore = new configurationstore(typemapfactory, mappers); configurationstore.constructservicesusing(x.resolve); configurationstore.assertconfigurationisvalid(); homecoming configurationstore; }), component.for<iconfigurationprovider>().usingfactorymethod(x => x.resolve<configurationstore>()), component.for<iconfiguration>().usingfactorymethod(x => x.resolve<configurationstore>()), component.for<imappingengine>().implementedby<mappingengine>().lifestylesingleton(), component.for<itypemapfactory>().implementedby<typemapfactory>() ); // add together profiles var configuration = container.resolve<iconfiguration>(); container.resolveall<profile>().tolist().foreach(configuration.addprofile); } }
then created profile registered itypeconverter implementations using bit of reflection:
class="lang-cs prettyprint-override">public class typeconverterprofile : profile { private readonly iconfiguration _configuration; public typeconverterprofile(iconfiguration configuration) { _configuration = configuration; } protected override void configure() { // replace whatever assembly contains itypeconverter implementations var myassembly = classes.fromassemblynamed("myassembly"); var typeconverters = x in myassembly.gettypes() type in x.getinterfaces() type.isgenerictype && type.getgenerictypedefinition() == typeof(itypeconverter<,>) select new { type = x, source = type.getgenericarguments()[0], destination = type.getgenericarguments()[1] }; // apply type converters automapper configuration foreach (var typeconverter in typeconverters) _configuration.createmap(typeconverter.source, typeconverter.destination).convertusing(typeconverter.type); } }
automapper automatically pick , utilize profiles , itypeconverters implementations in "myassembly".
as example, may have profile:
class="lang-cs prettyprint-override">public class mapprofile : profile { private readonly iconfiguration _configuration; public mapprofile(iconfiguration configuration) { _configuration = configuration; } protected override void configure() { // create auto-map searchresultmodel -> searchresult _configuration.createmap<searchresultmodel, searchresult>(); } }
and itypeconverter:
class="lang-cs prettyprint-override">public class searchrequestconverter : itypeconverter<searchrequest, searchrequestmodel> { private readonly iconfiguration _configuration; public mapprofile(iconfiguration configuration) { // in itypeconverter may have iconfiguration injected in case need _configuration = configuration; } public searchrequestmodel convert(resolutioncontext context) { var model = (searchrequest) context.sourcevalue; homecoming new searchrequestmodel { results = model.results }; } }
finally perform actual mapping in class having dependency on imappingengine:
class="lang-cs prettyprint-override">public class folderrepository { private readonly imappingengine _mappingengine; public folderrepository(imappingengine mappingengine) { _mappingengine = mappingengine; } public searchresult search(searchrequest searchrequest) { // mapping utilize itypeconverter implementation searchrequestmodel searchrequestmodel = _mappingengine.map<searchrequestmodel>(searchrequest); searchresultmodel searchresultmodel = search(searchrequestmodel); // mapping utilize auto-map profile searchresult searchresult = _mappingengine.map<searchresultmodel>(searchresultmodel); homecoming searchresult; } private searchresultmodel search(searchrequestmodel searchrequestmodel) { searchresultmodel result = new searchresultmodel(); // ... perform search ... homecoming result; } }
to have more itypeconverters, have implement them, , automagically picked windsor , automapper. manual auto-maps (createmap<>) can added in profile.
does solve problem?
castle-windsor automapper
Comments
Post a Comment