lundi 29 octobre 2018

When to use The Messenger Pattern in MVVM design

I Got confused with the role of the Messenger in MVVM, as I see contradicting articles about it.

This Article by MSDN by:

Communicating from the View-Model to the View
Observant readers will notice that Figure 1 has one arrow missing: in this figure, there is no way for the View-Model to communicate with the view. As mentioned earlier, the View-Model should ideally have no knowledge of the view that it is attached to. In fact, it is very common for a given View-Model to be attached to multiple views—for instance, because one view might become too complex and be split into two pages. To guarantee flexibility, the View-Model must have only an abstracted knowledge of what the view can do.
There are multiple ways to solve this issue. The two solutions that I propose here are using MVVM Light’s Messenger class and using view services.

generally saying that the messages should be from the View-Model to the View in case needed.

Also in this article, it warns from using the Messenger widely as it causes code to be less readable (which I agree with)

But on Another Article
It's used differently to communicate messages between View-Models

UML Diagram showing Messenger communication between View-Models

In this scenario, i didn't get the "Why VM cant have reference to another VM?"
as the referenced View-Model can easily be mocked and tested.

Aucun commentaire:

Enregistrer un commentaire