Thursday, August 30, 2012

Using mvvm-light messaging to communicate between multiple instances of non-singleton viewmodels?

Using mvvm-light messaging to communicate between multiple instances of non-singleton viewmodels?

I have a Parent View with corresponding viewmodel. That Parent view contains child controls with their own viewmodels.

In the past, with this Parent-Child type relationship, I've usually had the Parent viewmodel contain references to the child viewmodel, and then the child communicates with the parent via events.

When communicating with other non related viewmodels I've used messaging.

In the project I'm currently working on we're right at the early stages and using mvvm-light and someone has created a similar parent-child relationship using the messaging service, which means we're more loosely coupled, which is great. However, we don't have singleton view models, so it's possible that we'll have Parent and child views and viewmodels for Customer A and then other instances again for Customer B. If the child views subscribe to a message - that message is broadcast by Parent A, yet both Child A and B will recieve it, but I only want child A to respond.

What's the best practice for this kind of scenario? I was trying to get some timings to compare messaging approach versus the viewmodel references and events approach, but because one approach happens largely via bindings I found it difficult to figure out where to put stopwatch code. Does anyone know if there is much performance difference betwen the two approaches.

My current thinking is that we should do it with Parent viewmodel containing references to childviewmodels and utilise the events and bindings for communication. Or is there a better way of doing this?

Answers & Comments...




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog