Friday, July 24, 2020

.NET IEventAggregator messaging

EventAggregator, a part Microsoft's Prism library provides an event mechanism, that allows the developer to communicate between loosly coupled components in a WPF application. Anyone familiar with the MQQT Subscribe/Publish messaging protocol should find the Prism EventAggregator pattern easier to understand.

event
Figure 1: EventAggregator Publish/Subscribe architecture

In the WPF project I"m currently working on I needed a way to communicate between View Models using the MVVM pattern. I had thought about devising an Event based mechanism but then ran across the EventAggregator pattern and gave it a try. It turned out to be a very simple and effective communication method that allowed me to syncronize very loosly coupled view models.

In your WPF application you will need to obtain the latest release of the Prism.WPF package and either; download from NuGet or in Visual Studio's NuGetPackage manager search for the Prism.,WPF and install.

The instantiation of the EventAggregator needs to be in a place that is available to all modules that intend on using it. In my case I added a static entry in App.xml.cs as a global.

        
public partial class App : Application
{
    // Put globals here!
    public static EventAggregator g_eventAggregator = new EventAggregator();

    // access by App.g_eventAggregator 
}
        
    

Next we need to set up some common classes that we'll be using to communicate. Since the EventAggregator uses Events as the communications medium we'll define an event and since the event requires a message type let's define a message class to be used by the event.

        
public class PartNewEvent : PubSubEvent<PartNewMessage>
{
}

public class PartNewMessage
{
    public string PartId { get; set; }

    public PartNewMessage() { }

    public PartNewMessage(string id)
    {
        PartId = id;
    }
}        
        
    

The set up to publish a message is fairly straight forward and requires that we declare a SendMessageCommand delegate with no parameters. Then in the constructor, or somewhere that we instantiate the delegate and define the method we will use to send the message and finally send the message using the global EventAggregator.

        
public DelegateCommand SendMessageCommand { get; private set; }

public ViewModelConstructor()
{
    // Instantiate the SendMessage Delegate
    SendMessageCommand = new DelegateCommand(SendMessage);

    // As a test I sent the message directly from the constructor
    //  but can be sent from where ever you want.
    SendMessage();
}

public void SendMessage()
{
    PartNewMessage msg = new PartNewMessage(PartId);
    App.g_eventAggregator.GetEvent().Publish(msg);
}
        
    

The Subscribe side is even easier to use we just

        
public ViewModelConstructor()
{
    // Declare the message receive method you will be using
    App.g_eventAggregator.GetEvent().Subscribe(MessageRecvd);
    // More code...
}

private void MessageRecvd(PartNewMessage param)
{
    // This is the method that gets invoked by event
    //  with PartNewMessage data passed.
}
        
    

This article was meant to illustrate a simple usage of the EventAggregator. There is much more that can be done with it such as filtering but this should give you a brief overview.

References

[1]Event Aggregator prismlibrary.com [2]Feb 28, 2014, Magnus Montin .NET Using the event aggregator pattern to communicate between view models blog.magnusmontin.net
Top ^