在AutoMapper中的配置之间切换 [英] Switch between configurations in AutoMapper

查看:96
本文介绍了在AutoMapper中的配置之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:

// 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; }
}

自动映射器配置

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

然后,当我想将模型传递到视图中(例如,编辑Invoice对象)时,我会这样做:

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));
...

然后我可以用InvoiceItemModel s迭代Items集合.

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?

推荐答案

不幸的是,您必须创建单独的Configuration对象,并为每个对象创建单独的MappingEngine.

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;
   }
}

然后,您可以调用MappingEngine进行映射(语法与Mapper对象相同).

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));

这篇关于在AutoMapper中的配置之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆