Review
The first few moved the basic usage of automapper, custom mapping, I believe that the students have to see the AutoMapper this powerful mapping tool has been used. But carefully do you still remember the previous few mentioned that the creation of map is not always need to create, then automapper for these if management? This article we are going to look at the configuration of AutoMapper.
Initialization
AutoMapper provides an initialization function (mapper.initialize) that can be called during program initialization (Web applications can be written in Global.asax) for unified configuration initialization. The createmap of the previous chapters can be written uniformly here, as follows:
1 mapper.initialize (cfg = {2 mapper.createmap<calendarevent, Calendareventform>()3 . Formember (dest = dest. Eventdate, opt = opt. Mapfrom (src = src. date.date))4 . Formember (dest = dest. Eventhour, opt = opt. Mapfrom (src = src. Date.hour))5 . Formember (dest = dest. Eventminute, opt = opt. Mapfrom (src = src. Date.minute)); 6 });
OK, is not very convenient, then the question comes, what is CFG? It doesn't really work here?
Of course not, config in the configuration there are many, but this article we will talk about how to deal with Createmap, other configuration can be tapped to see the code, if there is a problem, you can also message exchange. The following chapters will be spoken on a case by case basis.
So this is the end of the story? No, careful you will not feel in the Global.asax write so many createmap seriously affected the code aesthetics and maintainability. It's not more convenient to write outside to deal with it. Of course, the mighty AutoMapper has been well thought out for you, but obviously here you can actually write a way to deal with it alone.
Configuration file (Profile)
If you are familiar with the network configuration of Windows Netsh WLAN profile of the students are certainly not unfamiliar with the concept of profile, simply said in accordance with the specifications of AutoMapper alone developed a format of the file, This file can be scheduled for a number of automapper configurations, which are used for unified management of the configuration we need when using AutoMapper.
A standard automapper configuration file is this:
1 Public classOrganizationprofile:profile2 {3 protected Override voidConfigure ()4 {5 //put the createmap here6 }7 8 //the name of the configuration, which can be defined by default as the current class name9 Public Override stringProfileNameTen { One Get{return This. GetType (). Name; } A } -}
The configuration file is defined so that it can only be added when AutoMapper is initialized:
1 mapper.initialize (cfg = {23 cfg). Addprofile<organizationprofile>(); 4 });
If you think that the configuration file is only such a simple function, then it is wrong, profile has a more powerful function, that is, each profile created by the map can be configured separately mapping some rules, such as:
Public class organizationprofile:profile { protectedoverridevoid Configure () { //Mapper.createmap write here ... ETC.. here new lowerunderscorenamingconvention (); New pascalcasenamingconvention (); }}
Sourcemembernamingconvention refers to the property matching rules of the source object, which is the default conversion of PropertyName-a PropertyName. An underlined matching lowerunderscorenamingconvention is set here, Property_name-PropertyName
Destinationmembernamingconvention refers to the target object's property matching rules, here pascalcasenamingconvention refers to the Pascal hump naming rules to deal with. These two processing Convention are already provided by AutoMapper, if you need to customize the more powerful convention then, look forward to the following section: "AutoMapper Porter's Custom conversion rules"
Above article transported from: https://github.com/AutoMapper/AutoMapper/wiki/Configuration
Also reference article: http://consultingblogs.emc.com/owainwragg/archive/2010/12/15/automapper-profiles.aspx
If there is a wrong place please advise, if you feel good, please point recommendation, thank you ~
AutoMapper Porter's configuration