Use of AutoMapper

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.