【问题标题】:Switch between configurations in AutoMapper在 AutoMapper 中切换配置
【发布时间】:2015-07-30 21:48:06
【问题描述】:

我有这种情况:

// 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 对象,我会这样做:

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

然后我可以使用 InvoiceItemModels 迭代 Items 集合。

问题是当我想检索一堆发票时,例如在索引中。

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

我不想加载“项目”。这种情况下更好的配置是:

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

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

但我不知道如何在“个人资料”之间切换。 有没有办法“选择”特定的映射配置?

【问题讨论】:

    标签: c# asp.net-mvc automapper


    【解决方案1】:

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

    首先,清除一个静态类来保存映射器

    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 对象)。

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

    【讨论】:

    • 非常感谢您的回答。这非常有用。我想我可以将我的普通映射器作为普通配置放在 Global.asax 中,这样我就可以在 MapperFactory 中使用 Mapper.Map 语法和 FlatMapper?
    • 这样就可以了。但是将来,如果您想要超过 3 个映射器(Normal、Flat、Super ...),请考虑使用工厂:D。
    猜你喜欢
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    相关资源
    最近更新 更多