Search code examples
c#eventsarchitecturemvp

Event handling across views/forms (MVP-VM framework)


Architechture, Flow of events -

Type 1

  1. MainForm - Creates presenter
  2. The presenter then creates and display the view.

Type 2 - Grid

  1. MainForm - Create a grid-presenter
  2. Every Row in a grid is linked to a detail form - DetailPresenter
  3. On double-click, the grid-presenter creates a detail presenter, and then the detail presenter then creates and display the view.

The problem - The views need to refresh or the presenter needs to reload the data. For eg there are two forms A and B. When both are open at the same time, Any change in A should trigger a refresh in B. There is also a situation when a Type 1 form should refresh the grid.

I want to avoid the solution where main form keeps a reference of every presenter and triggers the necessary events. As of now I can only think of static events. I want to solve the higher problem of messaging across views. Any better solution?


Solution

  • Enclosing the comments into the answer:

    What's needed here is the Event Aggregator pattern.

    As you suggested, the simple messenger implementation should work quite nice, if you want to roll out your own, you can always peek into the Caliburn.Micro implementation or Mvvm Light Messenger in the MVVM Light Toolkit for inspiration.