nikobarli
Topic Author
Posts: 178
Joined: 26 Apr 2017, 06:23

Multiple views sharing the same ViewModel/Model

15 Sep 2017, 09:12

Hi,

We are thinking to create multiple views which shared the same set of ViewModel/Model. Thus, actions on a view may trigger actions on the other views, through events, data bindings, commands, etc. All views will share one GUI thread as their main thread.

Is there anything we need to be careful of in this architecture (known limitations etc) ?

thanks.
 
User avatar
ai_enabled
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Re: Multiple views sharing the same ViewModel/Model

15 Sep 2017, 10:59

Hi,

It seems I'm using this approach for many years.

Model instances can be shared - sometimes models are singletones so this is not really a question as singletones are shared. I see no harm in sharing Models as long as Model can push changes to ViewModels (via events) to keep them properly updated. The only issue is possibility of VMs leaks due to the not unsubscribed events (in C#/CLR).

ViewModel instances can be shared between Views if they're stateless (and even immutable, but that's not a requirement). In that case I sometimes write a ViewModelProvider - it's like an object factory which returns according VM wrapper for a data object (but VM is single instance per each data object) and stores all the created VM instances, and (when necessarily) also listens to the Model events (or directly wired to Model) to update the created VM instances. You can call it VVMVMPM pattern or V/VM/VMP/M, for better clarity, just joking :-D.

An example of a stateless VM - a server information for the servers list in the multiplayer servers menu in our games. It's updated from the Model (the ping is measured over time, the players online count can be refreshed, the server icon loaded slowly in queue, etc) and there is no need for more than one instance of VM per "server data object" instance. A player can invoke ToggleFavorite command in VM (which is pushed to Model) and because VM is reused in multiple places (such as "online list", "history list", "favorites list", "selected server details side block") it will be updated everywhere. Very effective and allows us to reduce amount of the code: we don't need to have IsFavorite changed event in the Model to notify multiple view models - we simply set it once in VM during instantiation and then at every ToggleFavorite call it's changed in VM and then pushed to Model. Of course, some information such as ping and online players count need to be pushed from Model, but it's much easier as we can wire directly (maybe even without any events) a Model and according ViewModelProvider and the latter will find according VM instances and update them. Without ViewModelProvider we would need to have multiple events defined in the Model and it will be important to not forget from unsubscribing from these events when VM is no longer needed as it will cause VM instances leak.

In C#/CLR, to avoid keeping unused VM instances in the ViewModelProvider list, we can store them as the weak references so they will be automatically collected by GC. Managing a list of the weak references instead of a regular list of VM instances is low price for such reliability and convenience.


Stateful ViewModel instances in the most cases should not be shared, as they might hold a temporary modification of the state (which can be committed back to the model or reverted). An example of such VM is a contact entry in any contacts manager application where you can switch to the edit mode. It might be confusing for a user to see that the contact name changes in multiple places (such as a contacts list) when printing text into the editor textbox. And it's easy to make a bug by forgetting to commit/revert these changes when switching to a different view/mode.

Regards!
AtomicTorch Studio Pte. Ltd. http://atomictorch.com
 
nikobarli
Topic Author
Posts: 178
Joined: 26 Apr 2017, 06:23

Re: Multiple views sharing the same ViewModel/Model

19 Sep 2017, 08:53

Hi @ai_enabled,

Thanks for your information !

Yes, my case will be similar like yours. For example, there are application objects that have a list of operations can be acted upon them. This list may be activated as a context menu by right-clicking on the object, or through floating action panel (which will be on different views). Also some states will have different ways of visualization on different views and I am expecting easier coding by making the views to bind to the same VM object.

So following your explanation, I think there are no problem on Noesis GUI layer.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Multiple views sharing the same ViewModel/Model

19 Sep 2017, 14:04

Is there anything we need to be careful of in this architecture (known limitations etc) ?
As long as Views (and its elements) are always accessed from the same thread I think it should be fine. That means that property change notifications of the ViewModels should occur in that thread.
 
nikobarli
Topic Author
Posts: 178
Joined: 26 Apr 2017, 06:23

Re: Multiple views sharing the same ViewModel/Model

20 Sep 2017, 03:17

Ok, thanks for the confirmation. We are thinking using one UI thread for all the views, so I think it should be OK.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Multiple views sharing the same ViewModel/Model

20 Sep 2017, 16:23

Fine, I'm marking this as solved.

Who is online

Users browsing this forum: No registered users and 2 guests