View Model should have related classes?

Asked

Viewed 216 times

4

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

My doubt lies in the time to create the Viewmodels, I would have to have inside ManagerViewModel a property of the type PersonalDataViewModel and within PersonalDataViewModel a property of the type AddressViewModel?

One observation is 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, etc.

2 answers

3

My doubt is at the time of creating the Viewmodels, I would have to have within Managerviewmodel a property of type "Personaldataviewmodel" and within Personaldataviewmodel a property of type "Addressviewmodel"?

It is a way of doing, but it is not strictly necessary. You can use a Viewmodel whose properties are Models, if there is no need to restrict any property of those Models.

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

The simplest way is not using Viewmodels. Is used Models with [Bind] to avoid mass attribution by restricting critical variables, if any. That is, if something is not filled in canvas, it should not be mentioned in the decoration of [Bind].

3


View models represent data that will be presented in the view. Your goal is to assemble a model that makes it easy to use when presenting them, probably each in a different screen control. They serve so much not to use everything in model, how much to gather data from several models. You can even take data that isn’t even in models used. It is common that they even have "formatted" data for the presentation, opposite to what happens with the model.

So you have to ask yourself what is most suitable for your presentation, to have the data placed directly on the object or to have an indirect for them on another object? Therefore both may function according to need. The indirect may be for another view model or for a model directly.

Will help you get the data from PersonalData and Address on properties of ManagerViewModel? Or just have something to indicate where that data is and the view can you manage well like this? Remembering that views should be very simple and should have minimal processing.

Eventually you may have property indirect and have auxiliary methods that help you access data within that property.

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

View models can be updated automatically by framework by annotations or events placed on it, or can be created by controller. Depends on the need. They can update the templates as well.

The important thing is to understand that the view model is a model for vision. It works to meet the needs of view, that’s all that matters.

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

ViewModel

A deepening the theme.

  • The Personaldata and Address data will be used for Manager registration and editing. That’s why I need them in the view, because in the foreground I will have the fields to be filled in. Hence the doubt, create only a view for Manager with all the properties of Personaldata and Address or have in this view two properties that "point" to Personaldata and Address.

  • Need of course you need, whether you will be inside it or not you have to decide. No one can decide for you. I gave you the parameter to decide. Indirect access is sufficient to facilitate view? Or does everything have to be in there to make it easier? Understand what a Viewmodel to make the right decision. It should represent the best model for consuming vision in a simple and easy way. If you want to know how it is usually done, it is denormalizing, that is, putting everything within the very model of vision, but without always this is the most suitable way. The need for vision is in charge

Browser other questions tagged

You are not signed in. Login or sign up in order to post.