0

I have a best practice question for how to exchanage messages between objects ? I have found 2 ways to do this:

  1. Use simple Action and Func delegate.
  2. Use the mvvm light toolkit messenger. in case one, it's no dependency to any toolkit, and it's easy to use,but I’m wondering if there is any benefit to case 2 or if there are any principle that are violated with case 1.

Any thoughts are welcome.

Elham Azadfar
  • 709
  • 2
  • 17
  • 34
  • 1
    Child viewmodels would communicate with parents or siblings via events. Parent viewmodels would communicate with children by calling methods or setting properties. What kind of objects will be communicating with each other? The benefit to case 2, I imagine, would be that every time you replace a powerful, simple, and universal mechanism with something obscure, specialized, and dependency-ridden, an architecture astronaut gets his wings. Objectively, this is probably opinion based, I suppose. – 15ee8f99-57ff-4f92-890c-b56153 Nov 21 '17 at 19:48
  • 2
    There are too many ways to approach this problem. The answers would just turn into a straw-poll for which one people liked. The best thing is to do some research on the topic yourself, find two or three, _analyze_ them, determine if they work for you or not, and _try them out_. Come to us when you have a specific question about something you have attempted to do. – gunr2171 Nov 21 '17 at 19:49
  • 1
    ...lastly, viewmodels communicate with views via INotifyPropertyChanged/INotifyCollectionChanged, while views communicate with viewmodels by setting properties or executing commands. Or possibly code-behind may set viewmodel properties, call viewmodel methods, or handle viewmodel events. – 15ee8f99-57ff-4f92-890c-b56153 Nov 21 '17 at 19:53
  • Great comment! Thanks for the time taken to write it. – Elham Azadfar Nov 23 '17 at 09:14

1 Answers1

1

If you want to use Action and Func delegates, ViewModels should be aware of each other. But ViewModels in MVVM are like Controllers in MVC and they are not supposed to have direct references to each other. That's why using MVVM Light Toolkit's Messenger is a preferred way here. In this case you will have a loosely coupled messaging system without any hard references to any part of the application.

VahidN
  • 18,457
  • 8
  • 73
  • 117
  • 1
    *"ViewModels in MVVM are like Controllers in MVC and they are not supposed to have direct references to each other."* -- that's entirely wrong. That's the workaround you resort to if you break MVVM by writing a view-centric design. – 15ee8f99-57ff-4f92-890c-b56153 Nov 22 '17 at 18:13
  • 1
    No, in MVVM a `ViewModel` "Interprets user actions (e.g., via ICommand), performs them, updates application state", it's just like the functionality of a `Controller` in MVC. more info: https://stackoverflow.com/questions/1939403/mvvm-viewmodel-vs-mvc-viewmodel – VahidN Nov 23 '17 at 04:32
  • 1
    Your comment was about similarities and yes they are very similar. The whole purpose of this topic is about exchanging information between different parts of an MVVM based system and using `MVVM Light Toolkit's Messenger` is a very acceptable solution here. Messenger class decreases coupling between viewmodels. Every viewmodel can communicate with another viewmodel without any association between them. – VahidN Nov 23 '17 at 06:09
  • 2
    “They are not supposed to have direct references to each other” — no, parent viewmodels should have references to their children and attempting to avoid that results in madness. I shouldn’t have copied the part about MVC; it’s irrelevant to the point I intended to make. I didn’t say anything about similarities. – 15ee8f99-57ff-4f92-890c-b56153 Nov 23 '17 at 12:41