Question

I have some what idea that in order to reduce the complexities of publishing and subscribing to an event, an Eventaggregator act as a single source. What else can be understood in this regard. I only want a general description not the example through the code.

Was it helpful?

Solution

From the first line in the EventAggregator MSDN page:


The EventAggregator service is primarily a container for events that allow decoupling of publishers and subscribers so they can evolve independently. This decoupling is useful in modularized applications because new modules can be added that respond to events defined by the shell or, more likely, other modules.


Read the rest of that page to understand more of how it works and for examples of how it is used. A general rule I like to follow is that for Model->ViewModel communication, standard .NET events are good, but for ViewModel->ViewModel (or Module->Module) communication (where neither should really "know" about each other), the EventAggregator is nice.

OTHER TIPS

The idea of EventAggregator is that you don't need to know the source object of event raised. Generally to subscribe/listen to event to you must bring the source object to your class and then subscribe to event like this:

// Object will raise event
public class EventSourceClass
{
   public event EventHandler<EventArgs> OnMyEvent;
}

now - the class/object that want to subscribe to event must somehow to know the EventSourceClass class like this:

// Object will listen to events from very specific (EventSourceClass) class
public class EventListenerClass
{
   ...
   EventSourceClass eventSourceClass = new EventSourceClass(....);
   eventSourceClass.OnMyEvent += ......
   ...
 }

EventAggregator just make it simple:

  • You define class that describe the event (class inherit from CompositePresentationEvent<>.
  • Then in event-raiser class you just EventAggregator.GetEvent<event-type>().Publish(build-your-event-here) when you ready to raise event.
  • And in event-listener class you just EventAggregator.GetEvent<event-type>().Subscribe(function-that-will-start-to-work) to this event.

    It's called decouple the source object from listener because not the class that raise the event nor the event-listener class must know each other - they both should know only the EventAggregator - the ultimate event passing/event-bus object.

    Just let me know if you find this information useful for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top