zSynopsis
zSynopsis

Reputation: 4854

Question regarding Duplicating Properties in ViewModels

I have a question regarding the duplication of properties within view models. For my Search View i have a viewmodel that looks like this

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;}


}

For my Input View i have the following View Model

  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;}
}

List Display Model

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;}
   }
}

I feel like i'm duplicating a lot of properties. I was wondering if it would be ideal for me to go ahead and create like a DTO class called worker and just place it in each of these view model classes or is there a better way to do something like this?

Thanks

Upvotes: 1

Views: 83

Answers (2)

TalentTuner
TalentTuner

Reputation: 17566

Absoluetely create a worker class and place these properties in the object.

Upvotes: 1

Jackson Pope
Jackson Pope

Reputation: 14660

I'd go with the WorkerViewModel in each ViewModel like you suggested to improve the code and reduce duplication.

Upvotes: 2

Related Questions