有关在ViewModels中复制属性的问题

时间:2010-11-25 16:45:26

标签: asp.net-mvc viewmodel duplication

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

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类,并将其放在每个视图模型类中,或者有更好的方法来做这样的事情吗?

由于

2 个答案:

答案 0 :(得分:2)

我会在每个ViewModel中使用WorkerViewModel,就像你建议改进代码并减少重复一样。

答案 1 :(得分:1)

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