Switch between configurations in AutoMapper

2019-09-09 07:09发布

问题:

I have this situation:

// Core Business classes
public class Invoice
{
    public Guid Id { get; protected set; }
    public int Number { get; set; }
    public IList<InvoiceItem> Items { get; protected set; }
}

public class InvoiceItem
{
    public Guid Id { get; protected set; }
    public string Product { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

// MVC Models
public class InvoiceModel
{
    public Guid Id { get; set; }
    public int Number { get; set; }
    public IList<InvoiceItemModel> Items { get; set; }
}

public class InvoiceItemModel
{
    public Guid Id { get; set; }
    public string Product { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

The automapper configuration

public class MyProfile : Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<Invoice, InvoiceModel>();
        Mapper.CreateMap<InvoiceItem, InvoiceItemModel>();
    }
}

Then when I want to pass a model to my view, for example to edit an Invoice object, I do:

...
var invoice = Repository.Get<Invoice>(id);
return View("Update", Mapper.Map<InvoiceModel>(invoice));
...

And then I can iterate the Items collection with InvoiceItemModels.

The issue is when I want to retrieve a bunch of Invoices, for example in an index.

...
var invoices = Repository.ListAll<Invoice>();
return View("Index", invoices.Select(Mapper.Map<InvoiceModel>).ToList());   
...

I don't want the "Items" to be loaded. A better configuration for this case will be:

public class MyFlatProfile : Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<Invoice, InvoiceModel>()
            .ForMember(m => m.Items, opt => opt.Ignore());

        Mapper.CreateMap<InvoiceItem, InvoiceItemModel>();
    }
}

But I have no idea how to switch between "Profiles". Is there a way to "pick" a particular configuration of mapping?

回答1:

Unfortunately, you have to create separate Configuration objects, and create a separate MappingEngine for each.

First, declear a static class to hold the mappers

public static class MapperFactory
{
   public static MappingEngine NormalMapper()
   {
       var normalConfig = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
       normalConfig.CreateMap<Invoice, InvoiceModel>();
       normalConfig.CreateMap<InvoiceItem, InvoiceItemModel>();

       var normalMapper = new MappingEngine(normalConfig);
       return normalMapper;
   }

    public static MappingEngine FlatMapper()
   {
        var flatConfig = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
        flatConfig.CreateMap<Invoice, InvoiceModel>()
        .ForMember(m => m.Items, opt => opt.Ignore());
        flatConfig.CreateMap<InvoiceItem, InvoiceItemModel>();

        var flatMapper = new MappingEngine(flatConfig);
        return flatMapper;
   }
}

Then you can call the MappingEngine to do the mapping (The syntax is the same as Mapper object).

return View("Update", MapperFactory.FlatMapper().Map<InvoiceModel>(invoice));
return View("Update", MapperFactory.NormalMapper().Map<InvoiceModel>(invoice));