【问题标题】:transfer DTO to ViewModel将 DTO 转移到 ViewModel
【发布时间】:2013-06-08 16:07:23
【问题描述】:

这是我的数据传输对象

public class LoadSourceDetail
{
  public string LoadSourceCode { get; set; }
  public string LoadSourceDesc { get; set; }
  public IEnumerable<ReportingEntityDetail> ReportingEntity { get; set; }
}

public class ReportingEntityDetail
{
  public string ReportingEntityCode { get; set; }
  public string ReportingEntityDesc { get; set; }
}

这是我的 ViewModel

public class LoadSourceViewModel
{
    #region Construction

        public LoadSourceViewModel ()
        {
        }

        public LoadSourceViewModel(LoadSourceDetail data)
        {
            if (data != null)
            {
                LoadSourceCode = data.LoadSourceCode;
                LoadSourceDesc = data.LoadSourceDesc;
                ReportingEntity = // <-- ?  not sure how to do this 
            };
        }


    #endregion
    public string LoadSourceCode { get; set; }  
    public string LoadSourceDesc { get; set; }
    public IEnumerable<ReportingEntityViewModel> ReportingEntity { get; set; }  
}

public class ReportingEntityViewModel 
{ 
    public string ReportingEntityCode { get; set; }
    public string ReportingEntityDesc { get; set; } 
}

}

我不确定如何将数据从 LoadSourceDetail ReportingEntity 传输到 LoadSourceViewModel ReportingEntity。我正在尝试将数据从一个 IEnumerable 传输到另一个 IEnumerable。

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


    【解决方案1】:

    我会使用 AutoMapper 来做到这一点:

    https://github.com/AutoMapper/AutoMapper

    http://automapper.org/

    您可以轻松映射集合,请参阅https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays

    看起来像这样:

    var viewLoadSources = Mapper.Map<IEnumerable<LoadSourceDetail>, IEnumerable<LoadSourceViewModel>>(loadSources);
    

    如果您在 MVC 项目中使用它,我通常在 App_Start 中有一个 AutoMapper 配置,用于设置配置,即不匹配的字段等。

    【讨论】:

    • 我使用 automapper 进行所有与 ViewModel 之间的映射
    • 感谢您的建议,但我想知道如何在没有 AutoMapper 的情况下“手动”执行此操作。
    • 没问题,对我来说这将是一个带有 To() 和 From() 方法的老式 DTO。我会把我的答案留在那里,因为这就是我现在要做的方式。
    • 有理由不使用 Automapper 吗?请参阅stackoverflow.com/questions/14317583/… 的答案和stackoverflow.com/questions/5995140/… 的答案以了解更多信息
    • 我不负责这个项目,我无法做出这些决定,而且更资深的人决定不使用 Automapper
    【解决方案2】:

    你可以把它指向同一个IEnumerable

    ReportingEntity = data.ReportingEntity;
    

    如果你想做一个深拷贝,你可以使用ToList(),或者ToArray()

    ReportingEntity = data.ReportingEntity.ToList();
    

    这将实现IEnumerable 并将快照存储在您的视图模型中。

    【讨论】:

    • 我收到错误“无法将类型 'System.Collections.Generic.IEnumerable' 隐式转换为 'System.Collections.Generic.IEnumerable'。存在显式转换(您是否缺少演员表?)“
    【解决方案3】:

    如果没有 AutoMapper,您将不得不一个一个地映射每个属性,

    类似这样的:

      LoadSourceDetail obj = FillLoadSourceDetail ();// fill from source or somewhere
    
         // check for null before
        ReportingEntity = obj.ReportingEntity
                         .Select(x => new ReportingEntityViewModel() 
                            { 
                               ReportingEntityCode  = x.ReportingEntityCode,
                               ReportingEntityDesc  x.ReportingEntityDesc
                             })
                         .ToList(); // here is  'x' is of type ReportingEntityDetail
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-06
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多