Reputation: 4420
In my project i have one viewmodel per domainmodel and reuse this viewmodel in more then one view.
Example of one viewmodel
public class ProductViewModel
{
public int ProductId { get; set; }
public int ProductGroupId { get; set; }
public bool IsLinkedToErp { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Standard")]
public bool IsDefault { get; set; }
[MaxLength(50)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Artnr")]
public string ArtNo { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Beskrivning")]
public string Description { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Specifikation")]
public string Specification { get; set; }
[MaxLength(5)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Enhet")]
public string Unit { get; set; }
[MaxLength(4)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Konto")]
public string Account { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Netto")]
public decimal NetPrice { get; set; }
public string ChUser { get; set; }
public DateTime ChTime { get; set; }
public string GetUpdatedDate
{
get { return String.Format("{0:d}", ChTime); }
}
public string GetNetPrice
{
get { return String.Format("{0:0.00}", NetPrice); }
}
}
All my views in ProductController reuse this ViewModel. For List, Add and Edit. But in list i only use a few of all properties.
Then i using Automapper to map to Domain and vice versa.
Now I'm wondering if anyone has experience of this. Do you think I will have problems using this solution as the project grows?
Upvotes: 0
Views: 156
Reputation: 39807
I would consider splitting out the ListViewModel and the Add/Edit view models if only because you are potentially populating and sending more data than you really need to your view.
By leveraging from inheritance, you could quickly make two classes that contain only the data that you need for your view. Based on your example, I would consider the following:
public class ProductListViewModel{
public IEnumerable<ProductListModel> products {get;set;}
public string SomeOtherPotentialVariableForProductList {get;set;}
}
public class ProductListModel{
public int ProductId { get; set; }
public int ProductGroupId { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Beskrivning")]
public string Description { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Netto")]
public decimal NetPrice { get; set; }
}
public class ProductViewModel : ProductListModel
{
public bool IsLinkedToErp { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "Standard")]
public bool IsDefault { get; set; }
[MaxLength(50)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Artnr")]
public string ArtNo { get; set; }
[MaxLength(255)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Specifikation")]
public string Specification { get; set; }
[MaxLength(5)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Enhet")]
public string Unit { get; set; }
[MaxLength(4)]
[Required(AllowEmptyStrings = false)]
[Display(Name = "Konto")]
public string Account { get; set; }
public string ChUser { get; set; }
public DateTime ChTime { get; set; }
public string GetUpdatedDate
{
get { return String.Format("{0:d}", ChTime); }
}
This accomplishes a few things.
Upvotes: 3