Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should be in my View Models? [closed]

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:

  • Should I keep my validation logic in view models or business models?
  • Is it bad to have view models depend on business models?
like image 967
Rat Salad Avatar asked Jan 20 '14 17:01

Rat Salad


People also ask

How do I clear my Android model?

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.

What should be included in a ViewModel?

Anything that is important to the logical behavior of the application should go into the view model.

What is the responsibility of ViewModel?

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).


2 Answers

Your friend is right. About your questions

  1. 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.

  2. 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.

like image 194
MikeSW Avatar answered Oct 01 '22 05:10

MikeSW


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.

like image 42
rae1 Avatar answered Oct 01 '22 07:10

rae1