Using AutoMapper in ASP.NET.Core

Source: Internet
Author: User

First you need to reference AutoMapper's class library in NuGet

install-Package   automapperinstall-package   AutoMapper.Extensions.Microsoft.DependencyInjection

Then create the class that you want to convert

 public  class   user{ public   int  ID {get ; set         ;}  public  string  Name {get ; set  ;}}  
 Public class userdto{        publicintgetset;}          Public string Get Set ; }}

Then create a flag interface Iprofile

Internal Interface Iprofile    {    }

Next, create a class to inherit the AutoMapper profile class and implement the flag interface Iprofilethat you just created, and configure the relationship map in the constructor

 Public class Myprofile:profile,iprofile    {        public  myprofile ()        {            createmap<user, userdto>();            Createmap<userdto, user>();        }           }

Then create a class to register the relationship mappings

 Public classMappings { Public Static voidregistermappings () {//get all Iprofile implementation classes            varAlltype =Assembly. getentryassembly ()//get the default assembly. Getreferencedassemblies ()//get all referenced assemblies               . Select (Assembly.Load). SelectMany (y=y.definedtypes). Where (Type=typeof(Iprofile). GetTypeInfo (). IsAssignableFrom (type.            Astype ())); foreach(varTypeInfoinchAlltype) {                varType =Typeinfo.astype (); if(Type. Equals (typeof(Iprofile))) {                    //Registering MappingsMapper.initialize (y ={y.addprofiles (type);//initialise each profile classe                    }); }            }        }    }

From the above code can be seen using the flag interface to determine the registration mapping class for registration mapping,

Finally, simply add the service to the startup class's Configureservices method and add the mappings to the middleware to use

 Public void configureservices (iservicecollection services)        {            services. Addautomapper ();            Services. Addmvc ();                     }
 Public void Configure (Iapplicationbuilder app, ihostingenvironment env)        {            mappings.registermappings ();        }

Then you can use AutoMapper,

 Public classValuescontroller:controller {PrivateImapper _mapper {Get;Set; }  PublicValuescontroller ([Fromservices]imapper mapper) { This. _mapper =mapper; }        //GET api/values[HttpGet] Publicuserdto Get () {User User=NewUser () {ID=1, Name="Dog Doll"            }; varDTO = Mapper.map<user, userdto>(user); returnDTOs; }
}

Because the core uses DI to create objects, you simply add constructors.

Using AutoMapper in ASP.NET.Core

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.