When should I use an event handler over an event aggregator?
Asked Answered
S

2

12

When should I be using an Event Handler versus an Event Aggregator?

In my code, I have two ViewModels that controlled by a parent ViewModel, I am trying to decide if I should just use an event handler to talk between them? Or use an Event Aggregator? It is going to just be simple method call, I don't require parameters to be passed between them.

Schramke answered 26/11, 2012 at 13:24 Comment(0)
R
10

The way I see it, the EventAggregator is usually the heavy gun used when you want to publish an event to the entire application and more specifically - when you don't know who exactly is listening.

In your scenario that's not really the case, you have 2 view models that want to communicate, but they both know each other. So there is no real reason you can't use events.

I'll just mention that if you want to keep it a little more loosely-coupled - make an interface for each of the viewmodels that exposes the event. This way each VM will use the other VM's Interface instead of a specific instance.

Rosalba answered 26/11, 2012 at 13:50 Comment(3)
Can you explain the last section with an example?Schramke
My last section is mostly relevant if you use some kind of IoC such as MEF in which you don't specifically know who implements a specific interface, you only know the interface itself. So in this example, you will have 2 interfaces IVM1 and IVM2 which will each have an event. VM1 (which implements IVM1) does not know VM2, he only knows IVM2, but since the event is declared on IVM2 this is sufficient.Rosalba
The view models don't have to both know each other.Karolyn
K
6

Here is a link with some good info (that is alive as of 5/2019)... https://learn.microsoft.com/en-us/previous-versions/windows/apps/xx130639(v%3dwin.10) (Microsoft, Prism)

The "Making key decisions" section describes when to use it.

Events in .NET implement the publish-subscribe pattern. The publisher and subscriber lifetimes are coupled by object references to each other, and the subscriber type must have a reference to the publisher type.

Event aggregation is a design pattern that enables communication between classes that are inconvenient to link by object and type references. This mechanism allows publishers and subscribers to communicate without having a reference to each other. Therefore, .NET events should be used for communication between components that already have object reference relationships (such as a control and the page that contains it), with event aggregation being used for communication between loosely coupled components (such as two separate page view models in an app). For more info see Event aggregation.

I crudely see this as suggesting C# events are good for layers (UI listening to bus logic) or parent/child (an instrument listening to its contained devices) and event aggregation is good for siblings (e.g., sibling UI panels or device to device communication).

Karolyn answered 14/5, 2019 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.