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.
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.
– JcSaint
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
– Maniero