Introduction and use of AutoMapper (i.)

Source: Internet
Author: User

Software Environment
    • vs2015
    • ASP. NET MVC 5
    • . NET Framework 4.5.2
    • AutoMapper 5.2.0.0
AutoMapper Installation

New ASP. NET MVC Project Automapperexample, where you should use VS to create a new MVC project, no more

,

Click Tools →NUGETB Package Manager → Manage the NuGet package for the solution, in the popup screen, select "Browse", enter AutoMapper, go to search, select the first one in the search out package, then install

The output, as shown, indicates that the installation AutoMapper successfully

AutoMapper Configuration

With static global mapper registration, it should be placed at the time of application startup, the Application_Start () method of the Global.asax file for ASP.

Create a new AutoMapper folder in your program that holds the class for object mappings under which the new class Automapperconfig, which handles all object mappings

This class deals primarily with the mapping of objects, that is, from one object to another

     Public classAutomapperconfig { Public Static voidConfig () {mapper.initialize (cfg={cfg. Createmap<orderdto, order>(); Cfg. Addprofile<ExtendMapProfile>();        }); }         Public classExtendmapprofile:profile {protected Override voidConfigure () {Createmap<userdto, user>(); }        }    }

Where Order,orderdto,user,userdto is the entity object, where the,createmap<> is no longer described in the automapper conversion of two of the objects, where the following will be described in detail, and then add a new object conversion, Createmag<source,dest> conversions are performed here

The static method is executed in the Application_Start () method of the Global.asax file

        protected void Application_Start ()        {            automapperconfig.config ();            Arearegistration.registerallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (bundletable.bundles);        }

At this point, all automapper configuration is complete

Introduction and use of AutoMapper (i)

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.