AutoMapper Porter's custom type conversion, automapper Porter

Source: Internet
Author: User

AutoMapper Porter's custom type conversion, automapper Porter
Preface

I am very busy recently. I am still a little tired and have never updated it. It is really lazy. Before that, let's talk about something else. Recently, the company has to expand and need a lot of development. The leaders say that the recommendation can be used with money. This is a good opportunity, so I will take the opportunity to make an advertisement. ShippingRen.com recruits. NET for advanced development and coordinates Shanghai. If you are interested, please leave a message for me or add qq: 1029183866. Please refer to the blog garden when adding it.

In addition, during the past few days, busy work is inevitable, and amateurs are preparing their own website www.capqueen.com (which is still under record filing ), we are going to use the current technology to build a CMS (blog) from scratch. We will also share some articles with you. I have another idea recently. I have used many frameworks and components because of my work relationship. Most of them are on github. I have a little bit of experience after considering my research, are you sure you want to share it with us. Currently, FluentValidation and ServiceStack are available.

Now let's get back to the topic. In this article, I will share with you the custom conversion rules for AutoMapper. I believe that if you are using it, you will surely encounter a problem where the target object corresponds to the source object property name but the type does not match. How can this problem be solved ?.

Custom type conversion rules (Custom type converters)

In the same scenario, there are two classes:

    public class Source    {        public string Value1 { get; set; }        public string Value2 { get; set; }        public string Value3 { get; set; }    }    public class Destination    {        public int Value1 { get; set; }        public DateTime Value2 { get; set; }        public Type Value3 { get; set; }    }

If the preceding class is converted to a normal one, an error is reported. The error is as follows:

AutoMapper. AutoMapperMappingException: Missing type map configuration or unsupported mapping. // The er Mapping is abnormal. The corresponding type configuration cannot be found or the ing is not supported.

Therefore, we need to specify the corresponding type conversion for Mapping. AutoMapper provides three methods:

void ConvertUsing(Func<TSource, TDestination> mappingFunction);void ConvertUsing(ITypeConverter<TSource, TDestination> converter);void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>;

First, check the first

Mapper.CreateMap<string, int>().ConvertUsing(System.Convert.ToInt32);

This is very simple. A method is directly specified. Here, the default conversion in System. Convert is used. Of course, we can also customize it as long as it is a Func <TSource, TDestination>.

Let's look at the remaining two types. These two types Use the interface ITypeConverter <in TSource, out TDestination>, which is the implementation of custom type conversion provided by AutoMapper. Let's look at the implementation as follows:

Public class DateTimeTypeConverter: ITypeConverter <string, DateTime> {public DateTime Convert (ResolutionContext context) {return System. convert. toDateTime (context. sourceValue); // The default Convert is used. Note that the context is the context with the parameter information during conversion.} public class TypeTypeConverter: ITypeConverter <string, type> {public Type Convert (ResolutionContext context) {return context. sourceType; // type of source attribute used }}

Then let's take a look at the usage:

    Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());    Mapper.CreateMap<string, Type>().ConvertUsing<TypeTypeConverter>();    

 

Summary

The benefit of custom type conversion is that we can use it in all conversions once. But I think there will be some situations, such as TypeA => TypeB. Do we have multiple conversion requirements? In such a scenario, it seems that AutoMapper cannot use custom type conversion. I don't know if anyone knows how to handle it. Please leave a message ~

In fact, in my opinion, the above problem can be solved by using custom ing. type conversion is only a traversal provided by AutoMapper, and it is not particularly necessary.

 

This article from: the https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

In the next article, I will share with you the article on custom attribute processing. <AutoMapper Porter's custom attribute calculation>/I am also writing an article on data verification, you are welcome to come and make a brick.




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.