K
K
kid-programmer2014-06-20 17:42:16
ASP.NET
kid-programmer, 2014-06-20 17:42:16

View model how to organize correctly?

The question is how to properly organize models in mvc.
1.Question
For example, there is a product creation page:

public class ProductCreateViewModel
    {
        [Required]
        [Display(Name = "Наименование продукта")]
        public string Name { get; set; }

        //... еще свойства
    }

Product Change Page:
public class ProductEditViewModel
    {
        public int Id { get; set; }
        [Required]
        [Display(Name = "Наименование продукта")]
        public string Name { get; set; }

        //... еще свойства
    }

i.e., in order to change the product, you need the same properties as when creating it, and one additional Id. It turns out some kind of copy-paste from the first to the second. How to do feng shui?
2. Question
The main page displays a list of products that are in a certain section
Section: Dairy 1. Cottage cheese 2. Cheese 3. Ryazhenka
Section : Bakery 1. Cookies 2. Cake
public class ProductListViewModel
    {
        List<SectionTdo> Sections {get; set;}
    }

    public class SectionTdo
    {
         public int Id { get; set; }
         public string Name { get; set; }
          List<ProductTdo> Products
    }

    public class ProductTdo
    {
         public int Id { get; set; }
        public string Name { get; set; }
    }

Is the model formed correctly? ... I just got a little confused, it turns out that each view has its own model, despite the copy-paste of fields from domain entities or how it can be organized differently.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Abakumov, 2014-07-03
@Valeriy1991

Good afternoon everyone! Hmm, what's wrong with inheritance? Make a base view model - BaseViewModel, put all the basic properties there. If you need some properties for Add and others for Edit, just put some in AddViewModel and others in EditViewModel. At the same time, common properties like Id, Name, etc. will also be available to you in both ViewModels, because both AddViewModel and EditViewModel will inherit from BaseViewModel.
In defense of such a decision, I can say the following:
1. All common properties are taken out separately. They are available at any time. If you change common properties , you can edit them in one place .
2. Specialproperties that are needed only for a specific situation (Add, Edit, Delete, ...) can be moved to a special view model.
Why do you complicate your life by duplicating pieces of code? I think there is no need to explain what it threatens in case of changes or application scalability ...
Regarding the choice between model and view : after developing on WPF using the MVVM pattern, I personally use ViewModel in MVC. This allows you to further separate the data from the presentation, adding, as they say in a scientific way, an additional level of abstraction. So here I also fully support you.
Good luck!

R
Railchik, 2014-06-20
@Railchik

You have bad coding style. Name the variables something more descriptive, without ambiguity ProductListViewModel is a Model (Model) or a View (View).
one)
yes, but you don't need to clone the properties, just declare an instance of the class, or inherit

public class ProductCreateViewModel    {
        public string Name { get; set; }
        //... еще свойства
    }
public class ProductEditViewModel  {
        public int Id { get; set; } 
        
        public ProductCreateViewModel productCreate

        //... еще свойства

private static void CreateNewProduct(){
  //  здесь обрабатываете состояние
}

    }

2) Inherit
public class milk{
         public string nameSection { get; set; }
}
public class cheese : milk{
    public string name { get; set; }
}

Just OOP. Something like this. Maybe I didn't understand you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question