简体   繁体   中英

automapper with runtime mapping configuration

In my ASP.NET MVC application I need to implemenet mapping from one object to another with some kind of UI for mapping configuration in runtime, so the user can define mapping "on the go". Is there any libraries that supports such functionality?

Description

This is objects in my application. I need to somehow allow user to configure mapping of this objects via UI during application runs. For exmaple some kind of page in my application where user will be able to define mapping in simple way like so map Amout of OrderDTO to Order Qty and later without application recompile change this mapping for exmaple for ExactAmmount

//Object in DAL
public class Order 
{
    public int Id {get; set;}
    public string Name {get; set;}
    public decimal Qty {get; set;}
    //Lots of other fields 
}

//Object from XSD generation (for example)
public class OrderDTO 
{
    public int Id {get; set;}
    public string Description {get; set;}
    public decimal Ammout {get; set;}
    public decimal VAT {get; set;}
    public decimal ExactAmmount {get; set;}
    //Lots of other fields
}

Note: for legacy reasons I based this answer on AutoMapper 4.2.1 instead of the current 5.x version. The overall approach should be similar with the new version.

It is possible to create different mapping configurations and different mappers within a program. Also, it is possible to create member mappings by member names ( string ) instead of lambda expressions. However, some static type information is still necessary (as far as my example goes).

See the following example of a profile, that prepares a custom mapping based on property names:

class MemberProfile : Profile
{
    private string from;
    private string to;

    public MemberProfile(string from, string to)
    {
        this.from = from;
        this.to = to;
    }

    protected override void Configure()
    {
        this.CreateMap<Order, OrderDTO>()
            .ForMember(to, c => c.MapFrom<decimal>(from));
    }
}

This could be extended to support different source property types and a collection of custom mappings instead of a single one.

Usage example:

var order = new Order() { Id = 1, Name = "Test", Qty = 0.5m };

var conf1 = new MapperConfiguration(c => c.AddProfile(new MemberProfile("Qty", "Ammout")));
var conf2 = new MapperConfiguration(c => c.AddProfile(new MemberProfile("Qty", "ExactAmmount")));
var res1 = conf1.CreateMapper().Map<OrderDTO>(order);
var res2 = conf2.CreateMapper().Map<OrderDTO>(order);

For res1 , Qty is mapped to Ammout and for res2 , Qty is mapped to ExactAmmount . Since the difference is described as string property names, it should be possible to let the user influence this configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM