简体   繁体   English

如何将具有嵌套属性的模型映射到平面Dto?

[英]How do I map a model with nested properties to a flat Dto?

I'm trying to create a mapping from our entity models to a Dto but I'm failing everytime in trying to create the mapping. 我正在尝试创建从我们的实体模型到Dto的映射,但是每次尝试创建映射都失败了。

I have two domain classes. 我有两个域类。 This is a simplification of our model ( Device for instance has a lot more properties that represent a bunch of different things): 这是我们模型的简化(例如Device具有更多代表许多不同事物的属性):

class Device
{
    public int Name {get; set;}
}

class DeviceAccessToken
{
    public Device Device {get; set;}
    public string Key {get; set;}
    public string Secret {get; set;}
}

I then want to map DeviceAccessToken instances to this DeviceDto (also simplified, it has most of the fields present in the original Device model): 然后,我想将DeviceAccessToken实例映射到此DeviceDto (也进行了简化,它具有原始Device模型中存在的大多数字段):

class DeviceDto
{
    public int Name {get; set;}
    public string Key {get; set;}
    public string Secret {get; set;}
}

Is there a way to create this mapping without explicitly specifying all fields of the Device domain model in the mapping? 有没有一种方法可以创建此映射而无需在映射中显式指定Device域模型的所有字段?

This is effectively what I want, represented by an AutoMapper profile: 这实际上是我想要的,由AutoMapper配置文件表示:

class DeviceMappingProfile : Profile
{
    protected override void Configure()
    {
        this.CreateMap<DeviceAccessToken, DeviceDto>();

        this.CreateMap<Device, DeviceDto>()
            .ForMember(dest => dest.Key, opt => opt.Ignore())
            .ForMember(dest => dest.Secret, opt => opt.Ignore());
    }
}

The .ForAllMembers call was a failed attempt to make this work, it must not function like I envisioned it. .ForAllMembers调用是使此工作失败的尝试,它不能像我预想的那样起作用。

I understand I could do this by specifying every property of the Device in the DeviceAccessToken->DeviceDto mapping, but it would be a nightmare and very redundant since the names are the same. 我知道我可以通过在DeviceAccessToken->DeviceDto映射中指定Device每个属性来做到这一点,但这将是一场噩梦,而且由于名称相同,因此非常多余。

"Is there a way to create this mapping without explicitly specifying all fields of the Device domain model in the mapping?" “有没有一种方法可以创建此映射而无需在映射中显式指定Device域模型的所有字段?”

Yes, you can use the naming conventions in your Dto object and this would prevent you having to enter them in the create map. 是的,您可以在Dto对象中使用命名约定,这将避免您必须在创建映射中输入它们。

As an example: 举个例子:

Your values Key and Secret exist in DeviceAccessToken and DeviceDto they will not need to be mapped. 您的值Key和Secret存在于DeviceAccessTokenDeviceDto它们将不需要映射。 As Device is a nested object your dto can use the convention of DeviceName . 由于Device是一个嵌套对象,因此dto可以使用DeviceName的约定。

Example: 例:

using System;
using AutoMapper;

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

class DeviceAccessToken
{
    public Device Device {get; set;}
    public string Key {get; set;}
    public string Secret {get; set;}
}

class DeviceDto
{
    public string DeviceName {get; set;}
    public string Key {get; set;}
    public string Secret {get; set;}
}

public class Program
{
    public void Main()
    {

        // Configure AutoMapper
        Mapper.CreateMap<DeviceAccessToken, DeviceDto>();       

        var dat = new DeviceAccessToken { Device = new Device { Name = "Dev Name" }, Key = "Key", Secret = "Secret" };

        var dto = Mapper.Map<DeviceDto>(dat);

        Console.WriteLine(dto.DeviceName);
        Console.WriteLine(dto.Key);
        Console.WriteLine(dto.Secret);
    }
}

Working fiddle 工作提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM