I have business models named Product
and Category
like below in which I add the validations:
public class Product
{
public int ProductId {get; set;}
[Required]
[StringLength(25)]
public string Name {get; set;}
public string Description {get; set;}
public int CategoryId {get; set;}
}
public class Category
{
public int CategoryId {get; set;}
public string Name {get; set;}
}
For the view model I have created something like this:
public class ProductViewModel
{
public Product Product {get; set;}
public IList<Category> Categories {get; set;}
}
A friend of mine suggested keeping all the validations in the view model and mapping all the properties of the business model in the view model like this:
public class ProductViewModel
{
public int ProductId {get; set;}
[Required]
[StringLength(25)]
public string Name {get; set;}
public string Description {get; set;}
public int CategoryId {get; set;}
public IList<SelectListItem> CategoryDropdownValues {get; set;}
}
I asked him the advantages of this approach to the above one, he wasn't very sure. But he insisted that you shouldn't use the business models directly in your views and that only view models should be validated.
My questions:
As stated by @Nagy Robi, you could clear the ViewModel by call viewModelStore. clear() . The problem with this is that it will clear ALL the view model scoped within this ViewModelStore . In other words, you won't have control of which ViewModel to clear.
Anything that is important to the logical behavior of the application should go into the view model.
ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment . It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).
Your friend is right. About your questions
There is user input validation and business rules validation. Most of the time input validation is part of the business rules validation, however in asp.net mvc the framework does that validation automatically. In order to prevent duplication, this means that the UI validation should use the business validation. This can be easily done with FluentValidations (Data annotations are too rigid IMO).
So in this case the UI vlaidation is done at the UI level, using the business model validation.
View models are always depending on the business model at least to a degree, but there are not the same thing. They are different models with diferent purposes so they should be kept separated. The fact that probably your view model is 90% identical to the business (well, data structures) model is just a coincidence. We want to keep each model in its own layer and it just happens they have the same properties.
Validation should be kept at the domain/business level; otherwise you will find yourself duplicating validation rules throughout the entire application. This is the lowest common denominator all of you service and presentation layers will interact with.
Using domain models in a presentation view model is a different issue, with pros and cons. In your particular case, wrapping your models with a view-specific view model might alleviate some of the duplication you are encountering. However, be sure not to "dump" models inside view models just as they might be needed: this will quickly hurt performance as a lot of unnecessary information is being loaded.
The ASP.NET MVC framework will correctly parse and validate attributes from the System.ComponentModel.DataAnnotations
namespace. You can use these to annotate your domain models and if needed, you could augment your view model presentation if necessary using components limited to the MVC framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With