【问题标题】:Is it possible to use methods with new MapperConfiguration?是否可以使用新的 MapperConfiguration 方法?
【发布时间】:2017-01-17 00:46:29
【问题描述】:

我有一个非常简单的问题... 是否可以像这样设置 AutoMapper:

public IMapper Init()
{
     var config = new MapperConfiguration(cfg =>
     {
         cfg = MappingModelsToViewModels(cfg);
      });

      return config.CreateMapper();
}

我将每个映射拆分成这样的方法:

 public IMapperConfigurationExpression MappingModelsToViewModels(IMapperConfigurationExpression cfg)
 {
     cfg = SKU(cfg);
     cfg = Lot(cfg);
     cfg = SalesRate(cfg);
     cfg = SpecialSalesRate(cfg);
     cfg = Order(cfg);
     //...

     return cfg;
}

        public IMapperConfigurationExpression SKU(IMapperConfigurationExpression cfg)
        {
               // HTTPGET
               cfg.CreateMap<SKU, SKUViewModel>() //...
               return cfg; 
        }

我问是因为我收到了这个错误:

映射器未初始化。使用适当的调用初始化 配置。如果您尝试通过 容器或其他方式,请确保您没有对 静态 Mapper.Map 方法,如果您使用的是 ProjectTo 或 UseAsDataSource 扩展方法,确保传入 合适的 IConfigurationProvider 实例。

我通过将映射代码的一部分移入新的 MapperConfiguration 进行了测试,它正在工作。

谢谢,

大卫

【问题讨论】:

    标签: c# asp.net-mvc automapper-5


    【解决方案1】:

    有很多方法可以配置 AutoMapper。我个人喜欢以下方法 -

    Global.ascx.cs

    protected void Application_Start()
    {   
        ...
        AutoMapperConfiguration.Initialize();
        ...
    }
    

    AutoMapperConfiguration.cs

    public static class AutoMapperConfiguration
    {
        public static void Initialize()
        {
            MapperConfiguration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<SKU, SKUViewModel>();
                cfg.CreateMap<SKUViewModel, SKU>();
    
                cfg.CreateMap<Lot, LotViewModel>();
                cfg.CreateMap<LotViewModel, Lot>();
            });
            Mapper = MapperConfiguration.CreateMapper();
        }
    
        public static IMapper Mapper { get; private set; }
    
        public static MapperConfiguration MapperConfiguration { get; private set; }
    }
    

    用法

    用法会像 -

    SKUViewModel model = AutoMapperConfiguration.Mapper.Map<SKU, SKUViewModel>(sku);
    

    单元测试

    [Test]
    public void AutoMapperConfigurationInitializeValid()
    {
       AutoMapperConfiguration.Initialize();
       AutoMapperConfiguration.MapperConfiguration.AssertConfigurationIsValid();
    }
    

    【讨论】:

      【解决方案2】:

      我通常通过使用profile 选项automapper 来完成此操作。 If 提供了灵活性和更少的修改空间-

      Profile 定义,通过派生自 AutoMapper Profile class-

      public class Profile1 : Profile 
      {
          public Profile1(){ //... for version < 5, use protected void Configure
               CreateMap<SKU, SKUModel>()
               //........
          }
      }
      

      然后初始化自动映射器并加载配置文件 -

      MapperConfiguration = new MapperConfiguration(cfg =>
      {
          cfg.AddProfile<Profile1>();
      });
      Mapper = MapperConfiguration.CreateMapper();
      

      或者您可以让AutoMapper 自己检测配置​​文件 -

      // Assembly objects
      Mapper.Initialize(cfg => cfg.AddProfiles(/*.... supply the assembly here ...*/));
      

      更多可以在这里找到 -

      https://github.com/AutoMapper/AutoMapper/wiki/Configuration#profile-instances

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-04
        • 2018-03-15
        • 1970-01-01
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        相关资源
        最近更新 更多