简体   繁体   English

带有嵌套列表的 AutoMapper DTOModel 到 Model

[英]AutoMapper DTOModel with Nested List to Model

I have a Question.我有个问题。 I have 2 Dto Object and one Model.我有 2 个 Dto Object 和一个 Model。

What i'm trying todo is map the AssetDTO to the Asset Model using Automapper .我想做的是 map AssetDTO 使用Automapper资产 Model

I have no clue on how to accomplish this.我不知道如何做到这一点。

    public class AssetDTO
    {
        public string Code { get; set; }
        public string CodeType { get; set; }
        public List<MetricDataDTO> Data { get; set; }            
    }

    public class MetricDataDTO
    {
        public string Value{ get; set; }
        public string Flow { get; set; }           
    }

I have one model that look like this.我有一个 model,看起来像这样。

 public class Asset
    {
        public string Code { get; set; }
        public string CodeType { get; set; }
        public string Value{ get; set; }
        public string Flow { get; set; }
           
    }

I tryed setting up the mapping with automapper but without any luck:( Hope anyone can help me out,Thanks in advance!!我尝试使用 automapper 设置映射但没有任何运气:( 希望任何人都可以帮助我,提前谢谢!!

if suppose the value and flow inside asset is array, I think your current model for Asset need to be change (ie by creating new field List<MetricData> like List<MetricDataDTO> ).如果假设资产内的值和流是数组,我认为您当前的Asset model 需要更改(即通过创建新字段List<MetricData> ,如List<MetricDataDTO> )。 Later you can check this one AutoMapper - Mapping list of objects to bigger list of objects .稍后您可以检查这个AutoMapper - 将对象列表映射到更大的对象列表

In case for each asset there's only one value and flow, your model for AssetDTO will become:如果每项资产只有一个价值和流量,则 AssetDTO 的AssetDTO将变为:

public class AssetDTO
{
    public string Code { get; set; }

    public string CodeType { get; set; }

    public MetricDataDTO Data { get; set; }
}

And for such case, here the example for nested object (based on Using automapper for nested objects ): https://do.netfiddle.net/qVg59r对于这种情况,这里是嵌套 object 的示例(基于对嵌套对象使用自动映射器): https://do.netfiddle.net/qVg59r

Below answer also much more simpler https://stackoverflow.com/a/74471904/10766263下面的答案也简单得多https://stackoverflow.com/a/74471904/10766263

Thanks to Lucian Bargaoanu!感谢 Lucian Bargaoanu!

If you need more control when flattening, you can use IncludeMembers.如果在展平时需要更多控制,可以使用 IncludeMembers。 You can map members of a child object to the destination object when you already have a map from the child type to the destination type.当您已经拥有从子类型到目标类型的 map 时,您可以将 map 的子成员 object 到目标 object。

This allows you to reuse the configuration in the existing map for the child types MetricDataDTO when mapping the parent types AssetDTO and Asset.这允许您在映射父类型 AssetDTO 和 Asset 时,为子类型 MetricDataDTO 重用现有 map 中的配置。

It works in a similar way to mapping inheritance, but it uses composition, not inheritance.它的工作方式类似于映射 inheritance,但它使用组合,而不是 inheritance。

cfg.CreateMap<AssetDTO, Asset>().IncludeMembers(s=>s.MetricDataDTO);

You can check this link for details.您可以查看此链接了解详细信息。 https://docs.automapper.org/en/latest/Flattening.html#includemembers https://docs.automapper.org/en/latest/Flattening.html#includemembers

Hope it can help you.希望它能帮助你。

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

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