Wether I use Automapper or manually mapping, that plays no role.
All the data for a ReleaseViewModel must be first in the Release because it is filled in the data access layer with it. 90% of my model are like this. Why the overhead of duplicating everything?
What about the KISS principle and over-engineering?
Of course every tool for its appropriate task, but very often I read on SO that not using ViewModels in asp.net mvc is a NO-GO.
Where to draw the line? Should I use ViewModels when they differentiate to 50 %, 75% or 99% from my models ?
I have a model Release:
public class Release
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public DateTime CreatedAt { get; set; }
public int FailedTestsCount { get; set; }
public int SucceededTestsCount { get; set; }
public int SumTestsCount
{
get
{
return SucceededTestsCount + FailedTestsCount;
}
}
public int SumTestingTime { get; set; }
}
a viewmodel ReleaseViewModel:
public class ReleaseViewModel
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required(ErrorMessage = "Name must not be empty.")]
[StringLength(30, ErrorMessage = "Enter max. 30 chars for a name.")]
[Remote("ReleaseExists", "Release", ErrorMessage = "This name already exists.")]
public string Name { get; set; }
public string Author { get; set; }
public DateTime CreatedAt { get; set; }
public int FailedTestsCount { get; set; }
public int SucceededTestsCount { get; set; }
public int SumTestsCount
{
get
{
return SucceededTestsCount + FailedTestsCount;
}
}
public int SumTestingTime { get; set; }
}
ViewModelis something which is for the VIEW. most of the time it is similar to your entity model. But not always.Look at your example. In your
ViewModel, you have theRemoteAttribute and some Validation attributes. So this Remote Name checking is something you add to give a better user experience to your User. It is specific to the View.Another scenario you need a Viewmodel is for screens where you have more than one models involved. Ex : You have a
UserEntity and aProjectEntity and you want to provide a screen where a Project can be added to a User. So in this case, you can create a viewmodel to handle thatDo not use ViewModels for all your Model Entities. Create it when your VIEW really need it. I use my Model entity objects directly in some views without create a viewmodel sometimes because those are exactly same. Ex : Country / State/ City ( Look up table data.No Add/Edit)