C# AutoMapper 5.2如何配置
配置AutoMapper以供全局使用的正确方法是什么 我想设置一次,然后在应用程序中使用 我强烈感觉这是错误的。 事实上,我知道这是错误的,因为这需要一个新的实例。 我想要一个全局配置,然后你怎么称呼它。 找不到好的例子 这是我得到的,但不是我想要的C# AutoMapper 5.2如何配置,c#,automapper,C#,Automapper,配置AutoMapper以供全局使用的正确方法是什么 我想设置一次,然后在应用程序中使用 我强烈感觉这是错误的。 事实上,我知道这是错误的,因为这需要一个新的实例。 我想要一个全局配置,然后你怎么称呼它。 找不到好的例子 这是我得到的,但不是我想要的 public static class AutoMapperConfig { public static IMapper GetMapper() { var config = new MapperConf
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;
}
}
公共静态类AutoMapperConfig
{
公共静态IMapper GetMapper()
{
var config=new-MapperConfiguration(cfg=>{
CreateMap();
//更多的地图。。。?
});
IMapper mapper=config.CreateMapper();
返回映射器;
}
}
然后是用法:
var imapper = AutoMapperConfig.GetMapper();
var dest = imapper.Map<R_Logo, LogoDto>(logo);
var-imapper=AutoMapperConfig.GetMapper();
var dest=imapper.Map(徽标);
更新依据:pinkfloydx33
调用此函数一次,然后完成配置
public static class AutoMapperConfig
{
public static void RegisterMappings()
{
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<R_Logo, LogoDto>();
/* etc */
});
}
}
公共静态类AutoMapperConfig
{
公共静态无效注册表映射()
{
AutoMapper.Mapper.Initialize(cfg=>{
CreateMap();
/*等*/
});
}
}
在StartupConfig或启动文件中设置此选项
映射的配置
公共静态类映射模型
{
私有静态void Configure()
{
Mapper.Initialize(cfg=>
{
cfg.CreateMap()
.ForMember(x=>x.ID,
m=>m.MapFrom(a=>a.ID))
.ForMember(x=>x.FirstName,
m=>m.MapFrom(a=>a.FirstName)).ReverseMap();
}
}
}
在方法中调用它
公共类MyService
{
公共方法(var模型)
{
var myModel=Mapper.Map(model);
}
}
希望这能有所帮助,以下是在asp.net核心mvc中配置automapper的步骤 1。创建从
profile
public class ClientMappingProfile : Profile
{
public ClientMappingProfile ()
{
CreateMap<R_Logo, LogoDto>().ReverseMap();
}
}
public类ClientMappingProfile:Profile
{
公共客户端映射配置文件()
{
CreateMap().ReverseMap();
}
}
2。创建AutoMapper配置类,并在此处添加映射配置文件类
public class AutoMapperConfiguration
{
public MapperConfiguration Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<ClientMappingProfile>();
});
return config;
}
}
公共类自动映射器配置
{
公共MapperConfiguration配置()
{
var config=new-MapperConfiguration(cfg=>
{
AddProfile();
});
返回配置;
}
}
3.我们如何使用它
var config = new AutoMapperConfiguration().Configure();
var iMapper = config.CreateMapper();
var dest = iMapper.Map<R_Logo, LogoDto>(logo);
var config=new AutoMapperConfiguration().Configure();
var iMapper=config.CreateMapper();
var dest=iMapper.Map(徽标);
我们解决这个问题的方法是首先创建一组属性,这些属性可以将类修饰为“可映射”(to、From或两者都可以)。然后,您可以在单个位置初始化自动映射,通常是在应用程序初始化之后,并使用反射为修饰类的每个实例动态创建映射
下面是一个例子:
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);
}
});
您可以使用所概述的静态映射器api 例如,在应用程序中的某个地方,可能在启动期间,您会使用以下方式配置静态(全局)映射器:
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<Type1, Type2>();
/* etc */
});
然后,您就有了一个映射器,该映射器已针对您在应用程序期间提供的所有类型/配置/概要文件进行了配置,而无需配置单个映射器实例
简而言之,您只需配置一次(可能是在应用程序启动时)。然后通过AutoMapper.mapper
访问静态映射器实例(IMapper
),就可以在整个应用程序的任何位置使用它
通过此静态属性进行的访问是您在注释中称为“全局”的访问。只要您先调用了一次Initialize
,就可以在任何需要它的地方使用AutoMapper.Mapper.Map(…)
请注意,如果在静态实例上多次调用Initialize
,则后续每次调用都会覆盖现有配置
警告
在早期版本的AutoMapper中,静态映射器被删除。它后来被重新添加,我不知道他们是否保证它将保留在未来的版本中。建议使用您自己配置的映射器实例。如果需要,您可以将其存储在某个静态属性中。否则,您可以查看配置文件等或者通过简单的方式配置映射器,这样拥有自己的实例就不一定是一件“麻烦事”。我找到了在.Net Core中配置自动映射器的最佳解决方案。 多个配置文件。 只要用这个:
services.AddSingleton(provider => new MapperConfiguration(cfg =>
{
cfg.AddProfile(new sampleProfileMapper());
}).CreateMapper());
建议使用映射器的实例版本,它允许您配置多个实例,但仍然有一个静态映射器。只需调用
mapper.Initialize
,然后使用staticmapper
属性。这是如何全局使用的我已经阅读了文档…看不到如何全局实现…即这是如何调用的在整个应用程序中。它是一个静态实例。一旦它在应用程序中配置一次(可能在启动时),您就可以调用AutoMapper.Mapper
,这是IMapper的一个实例。一旦您将它配置为“全局”,您就可以在任何地方调用它Initialized请检查我在问题的更新中做了什么。最好链接实例和配置,即我在服务类中使用它,因为我的服务类没有起点(app start或Global.asax),我需要在每个服务类中调用configure…无需更新您的问题,因为您已经接受了答案。如果您有新问题,那么您应该询问iti get ya,但我正在寻找上述示例,而不是试图解释某人的意思。例如,对于您的警告,如果能够实现这一点,我会很高兴看看什么是更好的实现。例如,和一个
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);
}
});
AutoMapper.Mapper.Initialize(cfg => {
cfg.CreateMap<Type1, Type2>();
/* etc */
});
Type1 objectOfType1 = new Type1();
var result = AutoMapper.Mapper.Map<Type2>(objectOfType1);
services.AddSingleton(provider => new MapperConfiguration(cfg =>
{
cfg.AddProfile(new sampleProfileMapper());
}).CreateMapper());