使用Automapper映射后,嵌套对象成员为null [英] Nested object members null after mapping with Automapper

查看:221
本文介绍了使用Automapper映射后,嵌套对象成员为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象

public class Tenant : EntityBase
{
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual string CreatorName { get; set; }
    public virtual TenantState State { get; set; }
    public virtual Address Address { get; set; }

    public virtual IList<TenantActivity> Activities { get; set; }
    public virtual IList<AppUser> Users { get; set; }
    public virtual IList<TenantConfig> TenantConfigs { get; set; }

    ....
}

像这样的DTO:

public class TenantDto
{
    public Guid Id { get; set; }
    public DateTime CDate { get; set; }
    public string CUser { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string CreatorName { get; set; }
    public TenantState State { get; set; }
    public AddressDto Address { get; set; }
    public List<TenantActivityDto> Activities { get; set; }
    public List<AppUserDto> Users { get; set; }
    public List<TenantConfigDto> TenantConfigs { get; set; }
    ....
}

Address是一个ValueObject,它也具有DTO.我使用以下映射(标准,无特殊配置)

The class Address is a ValueObject which also has a DTO. I use the following mapping (standard, no special configs)

public class TenantMapperProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Tenant, TenantDto>();
        CreateMap<TenantDto, Tenant>();
    }
}

public class AddressMapperProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Address, AddressDto>();
        CreateMap<AddressDto, Address>();
    }
}

在我的App.xaml.cs中,我有:

And in my App.xaml.cs I have:

        var mapperConfig = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<TenantMapperProfile>();
            cfg.AddProfile<TenantActivityMapperProfile>();
            cfg.AddProfile<AppUserMapperProfile>();
            cfg.AddProfile<AddressMapperProfile>();
            //cfg.ShouldMapProperty = p => p.SetMethod.IsPrivate || p.GetMethod.IsAssembly;
        });
        Mapper = mapperConfig.CreateMapper();

然后我在这样的服务中调用它:

Then I call it in a service like so:

    public TenantDto CreateTenant(TenantDto tenantDto)
    {
        tenantDto.CreatorName = creatingUserName;
        var tenant = _mapper.Map<Tenant>(tenantDto); 
        tenant = _tenantManagerService.CreateTenant(tenant);//<-- Screenshot made here
        return _mapper.Map<TenantDto>(tenant);
    }

在第一次调用mapper之后,我制作了调试器窗口的屏幕截图.如您所见,tenantDto实例包含Address对象中的数据,但是tenant对象包含正确映射的自身数据,但嵌套的Address对象仅具有空值!我在这里做错什么了吗?

After the first call to mapper I made a screenshot of my debugger window. As you can see the tenantDto instance contains data in the Address object but the tenantobject contains its own data correctly mapped but the nested Address object has only null values! What am I doing wrong here??

编辑-其他信息

调用mapperConfig.AssertConfigurationIsValid();会返回一个错误:

不能映射Bedisoco.BedInventory.Sys.Models.Entities.Role上的以下属性: 的角色 添加自定义映射表达式,忽略,添加自定义解析器或修改目标类型Bedisoco.BedInventory.Sys.Models.Entities.Role.

The following property on Bedisoco.BedInventory.Sys.Models.Entities.Role cannot be mapped: Roles Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Bedisoco.BedInventory.Sys.Models.Entities.Role.

Role对象位于列表之一的某个位置,我认为List<AppUser>集合内的User对象本身包含一个Role对象的列表.它甚至还没有完全实现.

The Role object is somewhere inside one of the Lists, I think the User object inside the List<AppUser> collection contains itself a list of Role objects. It is not even implemented completely.

我是否认为Automapper会默默地忽略这些东西? ??

推荐答案

好吧,这是我自己的问题.

Ok solved it, was my own problem.

我的大多数值对象都包含一个具有所有字段的构造函数和一个私有的setter.如果配置正确(cfg.ShouldMapProperty = p => p.SetMethod.IsPrivate || p.GetMethod.IsAssembly;),则自动设置程序对Automapper来说没有问题.

Most of my value objects contain a constructor with all fields and a private setter. Private setters are NO problem with Automapper if configured correctly (cfg.ShouldMapProperty = p => p.SetMethod.IsPrivate || p.GetMethod.IsAssembly;).

但是VS或ReSharper建议将自动属性设为仅获取" .这种重构使VS满意,但对Automapper不利!

But VS or ReSharper suggested to make auto properties 'get-only'. This refactoring pleases VS but NOT Automapper!

这篇关于使用Automapper映射后,嵌套对象成员为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆