AutoMapper (4), automapper

Source: Internet
Author: User
Tags to domain

AutoMapper (4), automapper
Total returned directory

Custom value Parsing

Although AutoMapper covers a considerable number of target Member ing scenarios, there are still 1-5% target values to be parsed and processed. In many cases, custom value parsing can be applied to domain logic at the domain layer. However, if the logic is only related to the ing operation, it will make the source type messy for unnecessary behaviors. In this case, AutoMapper allows us to configure a custom value Parser for the target Member.

For example, there are two classes of Source and Destination, which are defined as follows:

public class Source{    public int Value1 { get; set; }    public int Value2 { get; set; }}
public class Destination{    public int Total { get; set; } }

We want to generate a calculated value during the ing. That is to say, during the Source ing from Source to Destination, we add the two attribute values of Source to the attribute of Destination. For some reason, we cannot place this logic in the source type. To provide a custom value parser, we first need to create an IValueResolver:

Public class MyValueResolver: IValueResolver {public ResolutionResult Resolve (ResolutionResult source) {// TODO: Implementation logic }}

ResolutionContext contains all context information of the current parsing operation, such as the source type and source value. In most cases, we do not need this more advanced interface. Another method is to implement the abstract class ValueResolver <TSource, TDestination>:

public class MyValueResolver : ValueResolver<Source, int>{    protected override int ResolveCore(Source source)    {        return source.Value1 + source.Value2;    }}

Now that we have implemented our own value parser, we need to tell AutoMapper to use this custom value parser when parsing a specific target Member. There are 3 ways to tell AutoMapper how to use a custom parser, including:

  • ResolveUsing <TValueResolver>
  • ResolveUsing (typeof (CustomValueResolver ))
  • ResolveUsing (aValueResolverInstance)

Next, we will start to use our own value Parser:

class Program{    static void Main(string[] args)    {        Mapper.CreateMap<Source, Destination>().ForMember(dest => dest.Total, opt =>        {            opt.ResolveUsing<MyValueResolver>();        });        var src = new Source {Value1 = 3, Value2 = 5};        var destObj= Mapper.Map<Destination>(src);        Console.WriteLine("destObj.Total={0}", destObj.Total);        Console.Read();    }}

Although the target Member Total does not have any matched source member, the parser will be responsible for providing the target Member value by adding a user-defined parser with valid configurations to it.

The test is successful and the result is as follows:

Mapper. createMap <Source, Destination> (). forMember (dest => dest. total, opt => {opt. resolveUsing <MyValueResolver> (). constructedBy () => new MyValueResolver ());});

During the ing operation, AutoMapper directly executes this callback function without using reflection. This is useful when the parser may need to construct function parameters or be built through the Ioc container.

No demonstration is provided here. Interested partners can study it on their own.

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.