[英]AutoMapper 5.2 how to configure
为全球使用配置 AutoMapper 的正确方法是什么。
我想设置一次,然后在应用程序中使用它。
我有一种强烈的感觉,这是错误的。 事实上,我知道这是错误的,因为这调用了一个新实例。 我想要一个全局配置,然后你怎么称呼它。 找不到好的例子!
这就是我得到的:但这不是我想要的
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
调用一次,然后配置完成。
public static class AutoMapperConfig
{
public static void RegisterMappings()
{
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<R_Logo, LogoDto>();
/* etc */
});
}
}
以下是在 asp.net core mvc 中配置 automapper 的步骤。
1.创建从Profile
扩展的映射配置文件类
public class ClientMappingProfile : Profile
{
public ClientMappingProfile ()
{
CreateMap<R_Logo, LogoDto>().ReverseMap();
}
}
2.创建 AutoMapper 配置类并在此处添加您的映射配置文件类。
public class AutoMapperConfiguration
{
public MapperConfiguration Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<ClientMappingProfile>();
});
return config;
}
}
3.我们如何使用它。
var config = new AutoMapperConfiguration().Configure();
var iMapper = config.CreateMapper();
var dest = iMapper.Map<R_Logo, LogoDto>(logo);
在您的 StartupConfig 或 StartUp 文件中设置此项。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
.....
MappingDTOModelToModel.Configure();
}
}
映射的配置,
public static class MappingDTOModelToModel
{
private static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<R_Logo, LogoDto>()
.ForMember(x => x.ID,
m => m.MapFrom(a => a.ID))
.ForMember(x => x.FirstName,
m => m.MapFrom(a => a.FirstName)).ReverseMap();
}
}
}
在方法中调用它,
public class MyService
{
public void MyMethod(var model)
{
var myModel = Mapper.Map<LogoDto, R_Logo>(model);
}
}
希望这可以帮助,
您可以使用此处概述的静态映射器 api。
例如,在您的应用程序中的某个地方,可能在启动期间您会使用以下内容配置静态(全局)映射器:
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<Type1, Type2>();
/* etc */
});
然后,任何时候您需要使用“全局”配置的映射器,您都可以通过静态Mapper
属性(这是一个IMapper
)访问它:
Type1 objectOfType1 = new Type1();
var result = AutoMapper.Mapper.Map<Type2>(objectOfType1);
然后,您就有了一个映射器,该映射器已针对您在应用程序期间提供的所有类型/配置/配置文件进行了配置,而无需配置单个映射器实例。
简而言之,您配置一次(可能在应用程序启动时)。 然后通过AutoMapper.Mapper
访问静态映射器实例( IMapper
)在整个应用程序中的任何地方都可用。
通过此静态属性访问就是您在评论中所说的“全局”。 只要您先调用了Initialize
一次,就可以在任何需要它的地方使用AutoMapper.Mapper.Map(...)
。
请注意,如果您在静态实例上多次调用Initialize
,则每个后续调用都会覆盖现有配置。
警告在 AutoMapper 的先前版本中,静态映射器已被删除。 后来又重新添加了它,我不知道他们是否保证它会保留在未来的版本中。 建议使用您自己配置的映射器实例。 如果需要,您可以将其存储在某个静态属性中。 否则,您可以查看配置文件等以轻松配置映射器,以便拥有自己的实例不一定是“麻烦”。
我们对这个问题的解决方案是首先创建一系列可以将类装饰为“可映射”(To、From 或 Both)的属性。 然后您可以在单个位置初始化 AutoMapper,通常在应用程序初始化后使用反射为每个装饰类实例动态创建映射。
下面是一个例子:
var types = _myTypeFinder.Find(type =>
type.IsDefined(typeof(AutoMapperAttribute)) ||
type.IsDefined(typeof(AutoMapperFromAttribute)) ||
type.IsDefined(typeof(AutoMapperToAttribute))
);
Mapper.Initialize(cfg =>
{
foreach (var type in types)
{
AutoMapperHelper.CreateMap(type, cfg);
}
});
我在 .Net Core 中找到了配置自动映射器的最佳解决方案。 多个配置文件。 只需使用这个:
services.AddSingleton(provider => new MapperConfiguration(cfg =>
{
cfg.AddProfile(new sampleProfileMapper());
}).CreateMapper());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.