AutoMapper 5.2如何配置 [英] AutoMapper 5.2 how to configure

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

问题描述

将AutoMapper配置为全局使用的正确方法是什么.

What is the correct way to configure AutoMapper for global use.

我想设置一次,然后在应用程序中使用.

I want to set it once and then used though out the app.

我有强烈的感觉,这是错误的. 实际上,我知道这是错误的,因为这调用了新实例. 我想要一个全局配置,然后您怎么称呼它. 找不到很好的例子!

i have a strong feeling this is wrong. in fact i know this is wrong as this calls an new instance. I want a global config and then how do you call it. Can not find a good example!

这是香港专业教育学院得到的:但不是我想要的

this is what ive got: but its not what im wanting

public static class AutoMapperConfig
{
      public static IMapper GetMapper()
      {
          var config = new MapperConfiguration(cfg => {
              cfg.CreateMap<R_Logo, LogoDto>();
              //lots more maps...?
          });

          IMapper mapper = config.CreateMapper();
          return mapper;
      }
}

然后使用:

  var imapper = AutoMapperConfig.GetMapper();
  var dest = imapper.Map<R_Logo, LogoDto>(logo);

更新基于:pinkfloydx33

UPDATE based on: pinkfloydx33

调用一次,然后完成配置.

Call this once and then the config is done.

public static class AutoMapperConfig
{
   public static void RegisterMappings()
   {
        AutoMapper.Mapper.Initialize(cfg => {
           cfg.CreateMap<R_Logo, LogoDto>();
            /* etc */
        });
    }
}

推荐答案

您可以按照例如,在应用程序中的某个位置,可能在启动期间,您将使用以下方式配置静态(全局)映射器:

For example, somewhere in your application, probably during startup you would configure the static (global) mapper using something like:

AutoMapper.Mapper.Initialize(cfg => { 
   cfg.CreateMap<Type1, Type2>(); 
   /* etc */
});

然后,每当需要使用全局"配置的映射器时,都可以通过静态Mapper属性(它是IMapper)访问它:

Then, any time you need to use your "globally" configured mapper, you access it via the static Mapper property (which is an IMapper):

Type1 objectOfType1 = new Type1();
var result = AutoMapper.Mapper.Map<Type2>(objectOfType1);

然后,您将为应用程序持续时间内提供的所有类型/配置/配置文件配置了一个映射器,而无需配置单个映射器实例.

You then have one mapper that has been configured for all the types/configurations/profiles you provide for the duration of your application without needing to configure individual mapper instances.

简而言之,只需配置一次(也许在应用程序启动时).然后,通过AutoMapper.Mapper访问静态映射器实例(IMapper)即可在整个应用程序中的任何位置使用它.

In short, you configure it once (perhaps at application startup). The static mapper instance (the IMapper) is then available anywhere throughout your application by accessing it via AutoMapper.Mapper.

通过此静态属性进行的访问就是您在注释中所说的全局".只要您第一次调用Initialize,就可以在任何需要的地方使用AutoMapper.Mapper.Map(...).

Access via this static property is what you refer to as "globally" in your comments. Anywhere you need it just use AutoMapper.Mapper.Map(...) So long as you've called Initialize once first.

请注意,如果您在静态实例上多次调用Initialize,则每个后续调用都会覆盖现有配置.

Note that if you call Initialize more than once on the static instance, each subsequent call overwrites the existing configuration.

警告 在早期版本的AutoMapper中,静态映射器已删除.后来又重新添加了它,我不知道他们是否保证它会保留在将来的版本中.建议使用您自己配置的映射器实例.如果需要,可以将其存储在某个位置的静态属性中.否则,您可以查看配置文件等内容,以轻松地配置映射器,从而拥有自己的实例不一定是麻烦".

WARNING In a previous release of AutoMapper, the static mapper was removed. It was later added back in and I don't know if they guarantee that it will remain in future versions. The recommendation is to use your own configured instances of a mapper. You can store it in a static property somewhere if you need it. Otherwise you can look into profiles, etc for easy ways to configure your mapper so that having your own instance isn't necessarily a "hassle".

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

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