c# - 避免构造函数映射字段

标签 c# .net-core automapper

我将 AutoMapper 6.2.2 与 .NET Core 2.0 及其默认依赖注入(inject)机制结合使用,以在模型和 DTO 之间进行映射。 我的 AutoMapper 配置中需要 DI,因为我必须执行 AfterMap<Action>这需要一些注入(inject)的组件。

问题是,对于某些具有参数匹配某些源成员的构造函数的模型,当我为 AutoMapper 启用 DI(添加 services.AddAutoMapper() )时,这些构造函数默认被调用并提供数据,然后中断我的操作英孚。

public class UserDTO
{
    public string Name { get; set; }

    public string Email { get; set; }

    public ICollection<RoleDTO> Roles { get; set; }
}


public class User
{
    public string Name { get; set; }

    public string Email { get; set; }

    public ICollection<RoleInUser> RoleInUsers { get; } = new List<RoleInUser>();

    public ICollection<Role> Roles { get; }

    public User()
    {
        Roles = new JoinCollectionFacade<Role, User, RoleInUser>(this, RoleInUsers);
    }

    public User(string name, string email, ICollection<Role> roles) : this()
    {
        Roles.AddRange(roles);
    }

}

public class UserProfile : Profile
{
    public UserProfile()
    {
        CreateMap<UserDTO, User>()
            .ForMember(entity => entity.Roles, opt => opt.Ignore())
            .AfterMap<SomeAction>();
    }
}

在前面的片段中,User(name, email, roles)使用角色列表调用。

我的映射器配置如下(注意 DisableConstructorMapping() 选项)

    protected override MapperConfiguration CreateConfiguration()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.DisableConstructorMapping();

            // Add all profiles in current assembly
            cfg.AddProfiles(Assemblies);
        });

        return config;
    }

还有我的Startup一切都设置好了:

        var mapperProvider = new MapperProvider();
        services.AddSingleton<IMapper>(mapperProvider.GetMapper());
        services.AddAutoMapper(mapperProvider.Assemblies);

修改配置文件以配置与 ConstructUsing 一起使用的 ctor

    public UserProfile()
    {
        CreateMap<UserDTO, User>()
            .ForMember(entity => entity.Roles, opt => opt.Ignore())
            .ConstructUsing(src => new User())
            .AfterMap<SomeAction>();
    }

它按预期工作,但这迫使我在每个 Map 配置中包含这个样板语句, 而且模型很大。

没有依赖注入(inject)(最近出现了这个需求),它与第一个代码片段一起工作顺利(不需要 ConstructUsing)。

我已经搜索过这个场景,但没有找到任何东西。正在添加 ConstructUsing到每张 map 要走的路?有没有更好的选择?或者也许我正在做某事 完全错误...

最佳答案

一年后,我在使用 AutoMapper 8.0.0 时遇到了这个问题。如果有人还在关注这个,有两种方法:

  1. 添加ConstructUsing给你的每一个CreateMap<Src, Des> .
  2. 修改/添加到您的 ConfigureServices这一行:services.AddAutoMapper(cfg => cfg.DisableConstructorMapping());

但是您必须在每个需要映射的类中创建一个空白构造函数。

关于c# - 避免构造函数映射字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48790180/

相关文章:

.net-core - 如何在 AWS Cloudwatch 上的 Serilog 的结构化日志输出中显示带有替换参数的日志消息

nuget - 如何强制 nuget 采用最新版本的软件包?

node.js - 如何将 nartc/automapper 中的配置文件使用到 Nestjs 应用程序中

c# - 如何根据 DataAnnotation 中的另一个属性验证一个属性

c# - 在 C# 中每天运行一次

C#对象引用问题

c# - 无法在单元测试中设置 AutoMapper,无法加载文件或程序集 Microsoft.AspNetCore.Mvc.ViewFeatures

c# - 格式字符串值精度不起作用

c# - 如何在特定时间触发.NET Core 3.1 Hosted Service?

.net - 尝试将 AutoMapper 添加到 .NetCore1.1 - 无法识别 services.AddAutoMapper()