Where to create / receive / cache ViewModels?

Firstly, I am new to MVVM, so please help me with this :)

Suppose I have several views in my application. In my case, I have an editor view and a browser view. Both of them should work with the viewmodel "node", which I am editing.

So where is the viewmodel really created?

Suppose the editor is asked to edit a specific node - he can create a new "NodeViewModel" and work with it. But at the same time, there is a NodeBrowserView that allows people to use the shortcut - select another node. Basically - I need the EditorView to work with the same ViewModel as the BrowserView, so I need the general GetViewModelfor (X) method.

So how should this work? :)

Greetings :)

+4
source share
2 answers

Both types of browsing and browsing should work on some NodeViewModel . You do not need separate viewing models for different viewing scenarios only.

Now can you edit the node not yet shown to the user? If not (as in the case, the user decides what is being edited), view models should be created for the first time when their content should be presented to the user. In most cases, this would be in some browser views / details so that the user can select an item and then decide to edit it.

Edit

Regarding your comment. NodeViewModel must be provided to view the editor.

The providing part can be performed, for example, by injection of the constructor or by setting the context of the presentation data manually. For example, when a user views all nodes in the browser view, he can double-click on the list item and an editor pop-up window appears:

 // this will probably be done in response to event private void ListItemDoubleClick(object sender, EventArgs e) { NodeViewModel currentItem = // extract current list item EditorView editorView = new EditorView(currentItem); editorView.Show(); } 

Alternatively, if you want to avoid such a strong connection between CompositeView and EditorView , you can always use events, however this is not always necessary.

Another thing that I was thinking about in terms of design is to add an additional view model, name it NodesListViewModel . What the program stream might look like:

  • When you start the application, get your nodes (whether from a database, file, service, etc.)
  • Create an instance of NodeListViewModel that accepts a dependency on IList<Node> (list of node objects)
  • NodeListViewModel will create and expose a collection of NodeViewModel elements
  • Create an instance of your main program window that uses the composite view. It needs a NodeListViewModel as a data context.
  • When the user decides that he needs to edit the item, everything is ready. The browser has a list of all NodeViewModels , it can easily pick up the current one and transfer it to the highlighted view.
+1
source

In such cases, I prefer to use one basic view model and have a "current item" to which the view is connected instead. This is much easier to do, instead of passing / creating new view models every time the user clicks on another line node / grid / etc. I really do not see the need for a separate model of representations or when performing the same operations in the general review model. This reduces complexity and reduces the change of creation objects (viewing models) and leaves them hanging because the link to them was not released until the application was closed.

0
source

Source: https://habr.com/ru/post/1382106/


All Articles