如何使用 Simple Injector 3 注册 AutoMapper 4
How to register AutoMapper 4 with Simple Injector 3
我有一个 Azure 网络作业,它解析包含类别的 CSV 并将结果映射到常规对象。
我试图通过记忆从一个项目中复制 AutoMapper + Simple Injector 配置,但出现错误:
AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping.
Mapping types:
CsvCategory -> Category
WebJobs.Data.CsvCategory -> Data.Category
Destination path:
Category
Source value:
WebJobs.Data.CsvCategory
container.RegisterSingleton<ITypeMapFactory, TypeMapFactory>();
container.RegisterCollection<IObjectMapper>(MapperRegistry.Mappers);
container.RegisterSingleton<ConfigurationStore>();
container.RegisterSingleton<IConfiguration, ConfigurationStore>();
container.RegisterSingleton<IConfigurationProvider, ConfigurationStore>();
container.RegisterSingleton<IMappingEngine>(Mapper.Engine);
Mapper.Initialize(c =>
{
c.ConstructServicesUsing(container.GetInstance);
c.AddProfile<CsvCategoryMappingProfile>();
});
public sealed class CsvCategoryMappingProfile : Profile
{
protected override void Configure() {
CreateMap<CsvCategory, Category>();
}
public override string ProfileName {
get { return typeof(CsvCategoryMappingProfile).Name; }
}
}
public sealed class MappingCategoryConverter : IConverter<CsvCategory, Category>
{
private readonly IMappingEngine _mapper;
public MappingCategoryConverter(IMappingEngine mapper)
{
_mapper = mapper;
}
public Category Convert(CsvCategory category)
{
return _mapper.Map<Category>(category);
}
}
我可以通过用这一行替换整个容器配置来修复它:
Mapper.AddProfile<CsvCategoryMappingProfile>();
但我想了解问题出在哪里,我在哪里做错了。
我不知道如何正确使用 Mapper.Initialize()
,明显的方法行不通。
解决方法如下:
Mapper.Initialize(x =>
{
var config = container.GetInstance<IConfiguration>();
config.ConstructServicesUsing(container.GetInstance);
config.AddProfile<CsvCategoryMappingProfile>();
});
因为在 x
你得到了 IConfiguration
的另一个实例。
我有一个 Azure 网络作业,它解析包含类别的 CSV 并将结果映射到常规对象。
我试图通过记忆从一个项目中复制 AutoMapper + Simple Injector 配置,但出现错误:
AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping.
Mapping types:
CsvCategory -> Category
WebJobs.Data.CsvCategory -> Data.Category
Destination path: Category
Source value: WebJobs.Data.CsvCategory
container.RegisterSingleton<ITypeMapFactory, TypeMapFactory>();
container.RegisterCollection<IObjectMapper>(MapperRegistry.Mappers);
container.RegisterSingleton<ConfigurationStore>();
container.RegisterSingleton<IConfiguration, ConfigurationStore>();
container.RegisterSingleton<IConfigurationProvider, ConfigurationStore>();
container.RegisterSingleton<IMappingEngine>(Mapper.Engine);
Mapper.Initialize(c =>
{
c.ConstructServicesUsing(container.GetInstance);
c.AddProfile<CsvCategoryMappingProfile>();
});
public sealed class CsvCategoryMappingProfile : Profile
{
protected override void Configure() {
CreateMap<CsvCategory, Category>();
}
public override string ProfileName {
get { return typeof(CsvCategoryMappingProfile).Name; }
}
}
public sealed class MappingCategoryConverter : IConverter<CsvCategory, Category>
{
private readonly IMappingEngine _mapper;
public MappingCategoryConverter(IMappingEngine mapper)
{
_mapper = mapper;
}
public Category Convert(CsvCategory category)
{
return _mapper.Map<Category>(category);
}
}
我可以通过用这一行替换整个容器配置来修复它:
Mapper.AddProfile<CsvCategoryMappingProfile>();
但我想了解问题出在哪里,我在哪里做错了。
我不知道如何正确使用 Mapper.Initialize()
,明显的方法行不通。
解决方法如下:
Mapper.Initialize(x =>
{
var config = container.GetInstance<IConfiguration>();
config.ConstructServicesUsing(container.GetInstance);
config.AddProfile<CsvCategoryMappingProfile>();
});
因为在 x
你得到了 IConfiguration
的另一个实例。