1

我有一个关于视图模型中属性重复的问题。对于我的搜索视图,我有一个看起来像这样的视图模型

public class SearchModel
{
    public IEnumerable<SelectListItem> Genders {get;set;}
    ... other select lists
    // Worker Details
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public DateTime Birthdate {get;set;}
    public int Phone {get;set;}et
    public string Gender {get; set;}

    //Address Details
    public string Street {get;set;}
    public string City {get;set;}
    public string Zip {get; set;}


}

对于我的输入视图,我有以下视图模型

  public IEnumerable<SelectListItem> Genders {get;set;}
  public IEnumerable<SelectListItem> Directions {get;set;}
    ... other select lists
    // Worker Details
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public DateTime Birthdate {get;set;}
    public int Phone {get;set;}et
    public string Gender {get; set;}

    public string SSN {get; set;}
    public string DL {get;set;}

    //Address Details
    public int Number {get;set;}
    public string Direction {get;set;}
    public string Suffix {get;set;}
    .....

    public string Street {get;set;}
    public string City {get;set;}
    public string Zip {get; set;}
}

列表显示模型

public class ListDisplayModel
{
   public IEnumerable<Worker> Workers {get;set;}

   internal class Worker 
   {
       public string FirstName {get;set;}
       public string LastName {get;set;}
       public DateTime Birthdate {get;set;}
       public int Phone {get;set;}et
       public string Gender {get; set;}

       public string SSN {get; set;}
       public string DL {get;set;}

       //Address Details
       public int Number {get;set;}
       public string Direction {get;set;}
       public string Suffix {get;set;}
       public string Street {get;set;}
       public string City {get;set;}
       public string Zip {get; set;}
   }
}

我觉得我正在复制很多属性。我想知道是否适合我继续创建一个名为 worker 的 DTO 类并将其放置在每个视图模型类中,还是有更好的方法来做这样的事情?

谢谢

4

2 回答 2

2

我会像您建议的那样在每个 ViewModel 中使用 WorkerViewModel 来改进代码并减少重复。

于 2010-11-25T17:12:48.527 回答
1

绝对创建一个工人类并将这些属性放在对象中。

于 2010-11-26T05:51:38.193 回答