Custom ing of AutoMapper Porter, automapper Porter

Source: Internet
Author: User

Custom ing of AutoMapper Porter, automapper Porter
Review

As mentioned in the previous section, the simple use of AutoMapper requires powerful customization for Complex Mapping. In this section, let's take a look at the custom projecing (Projection) of AutoMapper)

From Git: https://github.com/AutoMapper/AutoMapper/wiki/Projection

 

 

Instance

The scenario in this article is a simple calendar event. We first define a calendar event as follows:

1 public class CalendarEvent2 {3 // event Date 4 public DateTime Date {get; set;} 5 // event Title 6 public string Title {get; set;} 7}

 

On the page, we need to display the specific information of this event, which can be edited. Define a ViewModel dto as follows:

1 public class CalendarEventForm 2 {3 // event date 4 public DateTime EventDate {get; set;} 5 // Hour 6 public int EventHour {get; set ;} 7 // The Minute 8 public int EventMinute {get; set;} 9 // event Title 10 public string Title {get; set;} 11}

 

Maybe you will have a little bit of struggle. Why do you want to define this? js direct conversion and so on the page? Well, this is an example. You can understand it.

 

Next, let's take a look at the handwriting Mapping method as we have always done in this series:

1 // define a calendar event 2 var calendarEvent = new CalendarEvent 3 {4 Date = new DateTime (2014, 12, 15, 20, 30, 0 ), 5 Title = "" 6}; 7 8 CalendarEventForm form = new CalendarEventForm 9 {10 EventDate = calendarEvent. date. date, 11 EventHour = calendarEvent. date. hour, 12 EventMinute = calendarEvent. date. minute, 13 Title = calendarEvent. title 14 };

 

This Mapping is still very simple and there is no doubt. let's compare it with AutoMapper:

1 // configure Mapping2 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 7 // Mapping8 CalendarEventForm = Mapper. map <CalendarEvent, CalendarEventForm> (calendarEvent );

 

What do you think? Is it true that the code is too tall, but it is no different from handwriting? haha, don't worry. This is just an example. For complex scenarios, you will naturally realize that AutoMapper makes the code concise and the configuration uniform.

Description: The configuration of Mapping does not need to be created every time. For details, please look forward to the next article "AutoMapper configuration".

Back to the above Code, let's further describe this ForMember

 

ForMember Function

AutoMapper provides related APIs to support custom Mapping configurations, and also supports lamda.

Let's take a look at the definition of ForMember:

1 IMappingExpression <TSource, TDestination> ForMember (Expression <Func <TDestination, object> destinationMember

The first parameter is the target attribute, and the second parameter is a certain Expression ing Expression. Expression makes the ing more dynamic. You can directly use an Expression or directly call a function:

1. forMember (dest => dest. title, opt => opt. mapFrom (src => ChangeTitle (src. title); // call method 2. forMember (dest => dest. title, opt => opt. mapForm (src => src. title. where (....); // expression 3 4 string ChangeTitle (string title) 5 {6 // custom processing 7... 8 9 return "myName" + title; 10}

For the content and structure of Expression, see the article on Expression. The water in this article is very deep and I will share some in-depth research.

If you are careful, you may find out what will happen if multiple mappings are written? Tested,The conclusion is that only the first configuration is valid, and then the same parameter ing AutoMapper will automatically ignore it.

In addition, there is also a ForMembers function, but it is not very common. Please study other functions on your own. If I have studied and used the subsequent series, I will share them one by one. This section is still a simple content. If you have better suggestions, please leave a message. I think it may be too simple to give a specific image.


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 ......


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.