Custom mapping of AutoMapper porters

Source: Internet
Author: User

Review

The previous section talked about the simple use of automapper and the need for powerful customization for complex mapping, which we'll look at AutoMapper's custom mapping (Projection)

Transported from Git:https://github.com/automapper/automapper/wiki/projection

Instance

This scenario is a simple calendar event where we first define a calendar event as follows:

1  Public classcalendarevent2 {3     //Event Date4      PublicDateTime Date {Get;Set; }5     //Event Title6      Public stringTitle {Get;Set; }7}

So on the page, we need to display the specific information of this event, and is editable, defining a ViewModel dto as follows:

1  Public classCalendareventform2 {3     //Event Date4      PublicDateTime Eventdate {Get;Set; }5     //hours corresponding to the time6      Public intEventhour {Get;Set; }7     //minutes corresponding to the time8      Public intEventminute {Get;Set; }9     //Event TitleTen      Public stringTitle {Get;Set; } One}

Perhaps you will be a little tangled under, why to define so, the page JS direct conversion and the like, OK, this is an example, you understand it good.

Next, as we have done in this series, let's look at the way handwritten mapping:

1 //Define a calendar event2 varCalendarevent =Newcalendarevent3 {4Date =NewDateTime ( the, A, the, -, -,0),5Title ="Corporate Gatherings"6 };7 8Calendareventform form =NewCalendareventform9 {TenEventdate =CalendarEvent.Date.Date, OneEventhour =CalendarEvent.Date.Hour, AEventminute =CalendarEvent.Date.Minute, -Title =Calendarevent.title -};

This mapping is still very simple, no doubt, let's compare the use of AutoMapper:

1 //Configure mapping2Mapper.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 //Mapping8Calendareventform form = mapper.map<calendarevent, calendareventform> (calendarevent);

How do you feel? is not feel code tall on, but with handwriting of no difference, haha, don't worry, this is just an example. For complex scenes, you will naturally realize that the AutoMapper makes the code simple and uniform in configuration.

Said configuration here need to explain: mapping configuration is not so every time to create, specific instructions, please look forward to the next "AutoMapper configuration"

Back to the code above, let's further illustrate this formember

Formember function

AutoMapper provides the relevant API to support the custom mapping configuration, and he supports Lamda oh.

Take a look at the definition of Formember:

1 imappingexpression<tsource, tdestination> formember (expression<func<tdestination, Object>> Destinationmember

The first parameter is the target property, the second argument is a defined mapping expression, and expression makes the mapping more dynamic, either directly using an expression or simply calling a function directly:

1. Formember (dest = dest. Title, opt + = opt. Mapfrom (src =changetitle (src. (Title));//Call Method2. Formember (dest = dest. Title, opt + = opt. Mapform (src = src. Title.where (...); /Expression3 4 voidChangetitle (stringtitle)5 {6     //Custom Processing7     ...8     9     return "MyName"+title; Ten}

For the content and structure of expression, please refer to the article on expressions, where the water is very deep, and I have the opportunity to delve into the share of the content.

Careful you may find that if the mapping is written more than what? After testing, the conclusion is that only the first configuration is valid, and then the same parameter mapping AutoMapper will be ignored automatically.

In addition, there is a formembers function but is not very common, other functions please self-study, if I have research using the following series I will share. This section is still simple content, if you have better suggestions please leave a message, I think it may be too simple to carry, no specific image of the feeling.

Custom mapping of AutoMapper porters

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.