简体   繁体   English

如何将动态ViewModel列表类型映射到DTO

[英]How to map dynamic ViewModel List Type to DTO

Problem is to map dynamic list of nested objects to a flattened DTO. 问题是将嵌套对象的动态列表映射到展平的DTO。 It includes flattening of nested object. 它包括展平嵌套对象。 Also, it is interface type. 另外,它是接口类型。

Interface IfinalOutput
{
   object MainOutput;
   object CalcOutput;
   object SupportOutput;
}

class finalOutput<T,T2,T3> : IfinalOutput where T: IMainOutput, T2: ICalcOutput, T3: ISupportOutput
{
  public object MainOutput;
  public object CalcOutput;
  Public object SupportOutput;
  ...Other Properties
}

We have multiple concrete type of IMainOutput, ICalcOutput, ISupportOutput that can be returned belonging to different source systems. 我们有多种具体类型的IMainOutput,ICalcOutput,ISupportOutput,它们可以返回属于不同的源系统。 eg. 例如。 ABC system has ABCMainOutput, ABCCalcOutput, ABCSupportOutput XYZ system has XYZMainOutput, XYZCalcOutput and so on ABC系统具有ABCMainOutput,ABCCalcOutput,ABCSupportOutput XYZ系统具有XYZMainOutput,XYZCalcOutput等

Now we have DTOs for each source system like ABCDto, XYZDto kind of flattened structure. 现在,我们为每种源系统(如ABCDto,XYZDto)的扁平结构都提供了DTO。

Problem is to serialize and map IEnumerable to IEnumerable or IEnumerable based on object type and keep the list sorted. 问题是根据对象类型将IEnumerable序列化并映射到IEnumerable或IEnumerable,并使列表保持排序。

I tried using Automapper as below 我尝试如下使用Automapper

public List<Tto> Serialize<Tfrom, Tto>(
        IEnumerable<IFinalOutput> outputTableCompositeViewModel)
{
   List<Tto> sourceSystemDTOList = new List<Tto>();
   AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Tfrom, Tto>());
   foreach (var item in outputTableCompositeViewModel)
   {
     Tto outputItem = new Tto();              

     var outputTableItem = item.MainOutput;
     outputItem = AutoMapper.Mapper.Map<Tto>(outputTableItem);
     var supportTableItem = item.SupportOuput;
     var calculatedTableItem = item.CalcOutput;

     sourceSystemDTOList.Add(outputItem);
   }
   return new List<Tto>(sourceSystemDTOList);
}

Now all the properties are not mapped. 现在,所有属性均未映射。 Please guide how to map calc and support output to Dto and avoid using forloop. 请指导如何将calc和支持输出映射到Dto,并避免使用forloop。 I can use Automapper or ValueInjector. 我可以使用Automapper或ValueInjector。 Need an efficient way to do it. 需要一种有效的方法来做到这一点。

I implemented below changes and it worked. 我实现了以下更改,并且有效。

First, I had to create map for all my concrete class of IMainOutput, ICalcOutput, ISupportOutput with specific source system like below: 首先,我必须为IMainOutput,ICalcOutput,ISupportOutput的所有具体类创建具有特定源系统的地图,如下所示:

 Mapper.Initialize(cfg =>
        {

            cfg.CreateMap<ABCMainOutput, ABCdto>();
            cfg.CreateMap<ABCCalcOutput, ABCdto>();
            cfg.CreateMap<ABCSupportOutput, ABCdto>();
        });

Second, I added one automapper extension: 其次,我添加了一个自动映射器扩展:

 public static TDestination Map<TSource, TDestination>(
       this TDestination destination, TSource source)
    {
        return Mapper.Map(source, destination);
    }

And Finally, I used above extension to serialize the final output. 最后,我使用上述扩展序列化了最终输出。

public List<Tto> Serialize<Tfrom, Tto>(
    IEnumerable<IFinalOutput> outputTableCompositeViewModel)
{
   List<Tto> sourceSystemDTOList = new List<Tto>();       
   foreach (var item in outputTableCompositeViewModel)
   {
    Tto outputItem = new Tto();              

    var outputTableItem = item.MainOutput;       
    var supportTableItem = item.SupportOuput;
    var calculatedTableItem = item.CalcOutput;
    outputItem = Mapper.Map<Tto>(outputTableItem)
                                        .Map(calculatedTableItem)
                                        .Map(supportTableItem);
    sourceSystemDTOList.Add(outputItem);
   }
  return new List<Tto>(sourceSystemDTOList);
}

and the solution works. 解决方案起作用。

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

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