我正在使用 AutoMapper 在 DTO 对象和我的业务对象之间进行映射。我有两个 AutoMapperConfiguration.cs 文件 - 一个在我的服务层中,另一个在我的 web api 层中。
如以下链接的答案所示 Where to place AutoMapper.CreateMaps?
我正在我的 Global.asax 类中调用这两个文件的 Configure()
AutoMapperWebConfiguration.Configure();
AutoMapperServiceConfiguration.Configure();
但似乎我的服务配置调用(第二次调用)正在覆盖 Web API 层的映射(第一次调用),我收到一个异常,指出映射丢失。
如果我反转 Configure 调用看起来像这样
AutoMapperServiceConfiguration.Configure();
AutoMapperWebConfiguration.Configure();
我没有得到 web api 映射的异常,但我得到了服务层的相同映射异常。
我是不是做错了什么,因为这在上面的堆栈溢出链接中被清楚地标记为答案?
这是我的代码:
public static class AutoMapperServiceConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<CmciFlowTestToGenericFlowTestSimpleMappingProfile>();
x.AddProfile<FsrsFlowTestToGenericFlowTestSimpleMappingProfile>();
});
}
}
public class FsrsFlowTestToGenericFlowTestSimpleMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<FsrsFlowTest, GenericFlowTest>()
.ConvertUsing<FsrsFlowTestToGenericFlowTestSimpleConverter>();
}
}
public class FsrsFlowTestToGenericFlowTestSimpleConverter : TypeConverter<FsrsFlowTest, GenericFlowTest>
{
protected override GenericFlowTest ConvertCore(FsrsFlowTest source)
{
if (source == null)
{
return null;
}
return new GenericFlowTest
{
FlowTestDate = source.FlowTestDates,
StaticPsi = source.HydrantStaticPsi.ToString(),
ResidualPsi = source.HydrantResidualPsi.ToString(),
TotalFlow = source.NffGallonsPerMinute.ToString(),
FlowTestLocation = source.FsrsFlowTestLocations.Any()
? source.FsrsFlowTestLocations.First().LocationDescription
: null
};
}
public class CmciFlowTestToGenericFlowTestSimpleMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<CmciFlowTest, GenericFlowTest>()
.ConvertUsing<CmciFlowTestToGenericFlowTestSimpleConverter>();
}
}
public class CmciFlowTestToGenericFlowTestSimpleConverter : TypeConverter<CmciFlowTest, GenericFlowTest>
{
protected override GenericFlowTest ConvertCore(CmciFlowTest source)
{
if (source == null)
{
return null;
}
return new GenericFlowTest
{
FlowTestDate = source.FlowTestDates,
StaticPsi = source.HydrantStaticPsi.ToString(),
ResidualPsi = source.HydrantResidualPsi.ToString(),
TotalFlow = source.CalculatedHydrantGallonsPerMinute.ToString(),
FlowTestLocation = source.StaticLocationHydrantFlowPSI
};
}
}
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<ServiceToWebApiMappingProfile>();
x.AddProfile<WebApiToServiceMappingProfile>();
});
}
}
public class ServiceToWebApiMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<ServiceFlowTest, FlowTest>();
}
}
public class WebApiToServiceMappingProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<PropertyAddress, ServicePropertyAddress>();
}
}
为了解决这个问题,我在 AutoMapperWebConfiguration 类中添加了服务配置文件,并且只在 global.asax 中调用 AutoMapperWebConfiguration.Configure()。
最佳答案
对 Mapper.Initialize 的调用会重置映射器,以便清除之前消失的所有内容。
将对 AddProfile 的调用移动到一个 Mapper.Initialize 调用中,一切都应该很好:
Mapper.Initialize(x =>
{
x.AddProfile<CmciFlowTestToGenericFlowTestSimpleMappingProfile>();
x.AddProfile<FsrsFlowTestToGenericFlowTestSimpleMappingProfile>();
x.AddProfile<ServiceToWebApiMappingProfile>();
x.AddProfile<WebApiToServiceMappingProfile>();
});
关于c# - Global.asax 中的多个 AutoMapper.Configure(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20430909/