c# - Global.asax 中的多个 AutoMapper.Configure()

标签 c# asp.net asp.net-web-api automapper

我正在使用 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/

相关文章:

c# - 使用范围 wl.signin 登录真实账户请求太多权限

c# - 如何使用 C#.NET 在 AutoCAD 中以编程方式拉伸(stretch) block

c# - 将从网络服务接收到的二进制图像数据转换为 byte[]

asp.net - 函数无法在 <a> 标记中执行,但可以与 LinkBut​​ton 一起使用

asp.net - 判断应用程序是 ASP.NET 1.1 还是 ASP.NET 2.0 的方法

javascript - 如何在 Angularjs 中的 ng-repeat 中显示单行

c# - ASP.NET 损坏的程序集 "Could not load file or assembly App_Web_*"

c# - 将html表格导出到excel文件编码

asp.net - 使用 2 个参数注入(inject)构造函数不起作用

knockout.js - KnockoutJS 映射,来自 WebAPI 的 JSON