AutoMapper Porter Configuration

Source: Internet
Author: User

AutoMapper Porter Configuration
Review

In the previous articles, we used the basic usage and custom ing of AutoMapper. I believe some of you can use the powerful Mapping tool of AutoMapper. However, do you still remember that the previous articles mentioned that Map creation does not require Create every time? What if AutoMapper manages this? In this article, we will look at the AutoMapper configuration.

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 in the previous chapters can be written 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 it very convenient? So the question is, what is cfg? Is it useless here?

Of course, this is not the case. There are many other configurations in Config, but this article describes how to handle CreateMap in a unified manner. For other configurations, you can check the code by yourself. If there is a problem, you can also leave a message. The subsequent sections will be explained one by one.

So is this article over? No. Do you think writing so many CreateMap files in Global. asax seriously affects the appearance and maintainability of the code. It is not more convenient to write it out for processing. Of course, the powerful AutoMapper is ready for you, but it is clear that you can write a separate method here to handle it.

 

Profile)

If you are familiar with windows Network Configuration netsh wlan profile, you must be familiar with the concept of Profile. Simply put, a file in the format is developed separately according to the AutoMapper specifications, in this file, you can reserve some AutoMapper configurations for unified management of different categories.

A standard AutoMapper configuration file is like this:

 

1 public class OrganizationProfile: Profile 2 {3 protected override void Configure () 4 {5 // put CreateMap here 6} 7 8 // name of the configuration, by default, it can be defined as the current class name 9 public override string ProfileName 10 {11 get {return this. getType (). name;} 12} 13}

 

After the configuration file is defined, you only need to add it during AutoMapper initialization:

1 Mapper.Initialize(cfg => {23     cfg.AddProfile<OrganizationProfile>();4 });

 

If you think that the configuration file only has such simple functions, it will be wrong. Profile has more powerful functions, that is, the Map created in each Profile can separately configure some Mapping rules, for example:

Public class OrganizationProfile: Profile {protected override void Configure () {// Mapper. createMap is written here... etc .. here SourceMemberNamingConvention = new LowerUnderscoreNamingConvention (); DestinationMemberNamingConvention = new PascalCaseNamingConvention ();}}

 

SourceMemberNamingConvention refers to the attribute matching rule of the source object, that is, the conversion of propertyName-> propertyName by default. Here, an underline-matching LowerUnderscoreNamingConvention, that is, property_name-> PropertyName

DestinationMemberNamingConvention refers to the attribute matching rule of the target object. The PascalCaseNamingConvention here refers to processing according to the Pascal hump naming rule. The two Convention processes are already provided by AutoMapper. If you need to customize more powerful Convention, please look forward to the next section: Custom conversion rules for AutoMapper porters

 

Above articles from: https://github.com/AutoMapper/AutoMapper/wiki/Configuration

Also reference: http://consultingblogs.emc.com/owainwragg/archive/2010/12/15/automapper-profiles.aspx

If you have any mistakes, please advise. If you think it is good, please give me some suggestions. Thank you ~


What is the role of automapper in ORM?

AutoMapper? This is the simplest example.
For example, in ORM, the Model used for interacting with databases has many attribute variable methods. When we interact with other systems (or other structures in the system), for coupling or security or performance considerations (in short, various considerations ), we do not want to pass this Model directly to them. In this case, we will create an anemia Model to store and transmit data. Is Shenma an anemia model? The anemia model (DTO, Data Transfer Object) contains only the attributes of Shenma, and can only save the required Data. Wood has any other redundant method Data or something, A type object used for data transmission. During the creation process, if we do this manually, we will see the Code as follows:
B B = new B ();
B. XXX1 = a. XXX1;
B. XXX2 = a. XXX2;
...
...
...
Return B;

In this case, AutoMapper can automatically map A model to A new B model based on the definitions in model A and model B. This prevents code from being smelly, long, and boring. SO, can be understood as a code generator ......


Related Article

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.