General use, usually first initialized configuration, an application only need to initialize once
Mapper.initialize (cfg = cfg. Createmap<order, orderdto>()); // or var New Mapperconfiguration (cfg = cfg. Createmap<order, orderdto> ());
var mapper = config. Createmapper (); // or var New = mapper. Map<orderdto>(order); // or Orderdto DTO = mapper.map<orderdto> (order);
Convert between entity and DTO using automapper, mapping between different fields
AutoMapper.Mapper.Initialize (cfg = cfg. Createmap<streetevent, streeteventdto>() = dest. EVTSRC, opt = opt. Mapfrom (src = src.evt_src)) = dest. MapId, opt = opt. Mapfrom (src = src.map_id)) ; var targetlist = automapper.mapper.map<list<streeteventdto>> (List);
For custom type conversions between entities, refer to:
Https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters
Public classSource { Public stringValue1 {Get;Set; } Public stringValue2 {Get;Set; } Public stringValue3 {Get;Set; } } Public classDestination { Public intValue1 {Get;Set; } PublicDateTime Value2 {Get;Set; } PublicType Value3 {Get;Set; } }[test] Public voidExample () {mapper.initialize (cfg={cfg. Createmap<string,int>(). Convertusing (Convert.ToInt32); Cfg. Createmap<string, Datetime> (). Convertusing (NewDatetimetypeconverter ()); Cfg. Createmap<string, Type> (). Convertusing<typetypeconverter>(); Cfg. Createmap<source, destination>(); }); Mapper.assertconfigurationisvalid (); varSource =NewSource {Value1="5", Value2="01/01/2000", Value3="automappersamples.globaltypeconverters.globaltypeconverters+destination" }; Destination result= Mapper.map<source, destination>(source); Result. Value3.shouldequal (typeof(Destination)); } Public classdatetimetypeconverter:itypeconverter<string, datetime> { PublicDateTime Convert (stringsource, DateTime destination, Resolutioncontext context) { returnSystem.Convert.ToDateTime (source); } } Public classtypetypeconverter:itypeconverter<string, type> { PublicType Convert (stringsource, Type destination, Resolutioncontext context) { returncontext. SourceType; } }
The conversion of custom entity values, which is applicable to the value type calculation of the target and the source entity directly, refer to
Https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers
Public classSource { Public intValue1 {Get;Set; } Public intValue2 {Get;Set; } } Public classDestination { Public intTotal {Get;Set; } } Public classCustomresolver:ivalueresolver<source, Destination,int> { Public intResolve (source source, Destination Destination,intmember, Resolutioncontext context) { returnSource. Value1 +source. Value2; }}mapper.initialize (cfg=CFG. Createmap<source, destination>() . Formember (dest= = Dest. Total, opt + = opt. Resolveusing<customresolver>()); Mapper.assertconfigurationisvalid (); varSource =NewSource {Value1=5, Value2=7 }; varresult = Mapper.map<source, destination>(source); Result. Total.shouldequal ( A);
Public classmultby2resolver:ivalueresolver<Object,Object,int> { Public intResolve (ObjectSourceObjectDestintDestmember, Resolutioncontext context) { returnDestmember *2; }} mapper.initialize (cfg= cfg. Createmap<source, destination>() . Formember (dest=Dest. Total, opt= = Opt. Resolveusing (NewCustomresolver ())); Mapper.initialize (CFG={cfg. Createmap<source, destination>() . Formember (dest=Dest. Total, opt= = Opt. Resolveusing<customresolver,decimal> (src =src. SubTotal)); Cfg. Createmap<othersource, otherdest>() . Formember (dest=Dest. Othertotal, opt= = Opt. Resolveusing<customresolver,decimal> (src =src. othersubtotal);}); Public classcustomresolver:imembervalueresolver<Object,Object,decimal,decimal> { Public decimalResolve (ObjectSourceObjectDestinationdecimalSourcemember,decimalDestinationmember, Resolutioncontext context) {//Logic here }}
Use of AutoMapper