View Model should have related classes?

3

I have three entities: Manager , PersonalData and Address . A Manager has a PersonalData and a PersonalData has a Address .

My question is when creating ViewModels , I would have to have a property of type ManagerViewModel inside PersonalDataViewModel and within PersonalDataViewModel a property of type AddressViewModel ?

Note that PersonalData is used in other entities, such as Client , Supervisor , because it is an entity that contains common data such as name, phone, email, p>     

asked by anonymous 12.07.2016 / 23:15

2 answers

2

ViewModels represent data that will be displayed in view . Your goal is to put together a model that will make it easier to use when you present them, probably each in a different screen control. They serve both to not use everything in the model , and to merge data from multiple models . You can even get data that is not even in the models used. It is common that they even have formatted data for the presentation, as opposed to the model .

Then you should ask yourself what is best suited for your presentation, have the data placed directly on the object or have an indirection for them in another object? So both can work according to need. The indirection can be for another ViewModel or for a model directly.

Will it help you have the data of PersonalData and Address on ManagerViewModel properties? Or is it enough to have something that indicates where that data is and the view is able to turn out well? Remember that views should be very simple and should have minimal processing.

Eventually you can have property indirection and have auxiliary methods that help you access the data within that property.

If you are using ViewModels because someone said it is good, you are using it for the wrong reasons. Use if it makes your life easier. If it is to reproduce what is in the models , then it has no real function for your code.

ViewModels can be automatically updated by the framework for annotations or events placed on it, or can be created by the controller . It depends on the need. They can upgrade the templates as well.

The important thing is to understand that ViewModel is a template for the view. It works to meet the needs of view , that's all that matters.

Do you know the view of SQL? That's basically it (despite the confusion of names). You create a logical model easier to consume in a specific situation.

A deepening the theme .

    
12.07.2016 / 23:26
3
  

My question is when creating the ViewModels, I would have to have a property of the type "PersonalDataViewModel" inside the PersonalDataViewModel and an "AddressViewModel" property?

It's a way to do it, but it's not strictly necessary. You can use a ViewModel whose properties are Models , if there is no need to restrict some properties of those Models .

  

One observation is that PersonalData is used in other entities, such as Client, Supervisor, because it is an entity that contains data in common such as name, phone, email, etc.

The simplest way is not using ViewModels . Models with [Bind] to avoid mass allocation, restricting critical variables, if any. That is, if something is not filled out on screen, it should not be mentioned in the [Bind] decoration.

    
12.07.2016 / 23:21