Site icon ArcTouch

Why you should use Prism EventAggregator in .NET app development

Computer display showing eventaggregator prism dotnet app development

Prism is an extensive library for app development in .NET that includes many great features to make your life as a C# developer so much easier. One of these features is the EventAggregator. In this post, I’ll explain what the EventAggregator is, why it’s so useful, and where you should use it.

What is Prism EventAggregator?

In short, EventAggregator is Prism’s implementation of the Publisher-Subscriber pattern. Publisher-Subscriber is a messaging pattern designed to facilitate asynchronous communication in your application. Specifically, it resolves the common problem of sending messages between components that are difficult, impractical, or impossible to link.

At its core, the pattern consists of events that are published to an event bus, which in turn passes them to one or more subscribers. The subscribers can handle the event in any way they see fit.

You can find more detailed information here.
diagram of Publisher Subscriber Event Bus using Prism for .NET app development

A simple representation of the Publisher-Subscriber pattern

Why use Prism EventAggregator for .NET app development?

The Publisher-Subscriber model has various benefits:

EventAggregator has all these benefits and adds a few more that are especially helpful in .NET app development:

When to use Prism EventAggregator?

To recap, Publisher-Subscriber is a messaging pattern designed for communication between components that are difficult or impractical to link. In a Prism project, EventAggregator is often used to send messages between two or more ViewModels, or between services that don’t have a reference to each other. Another use case is when one event needs to be handled by many different subscribers, and it wouldn’t be practical to pass a publisher reference to each of these subscribers.

EventAggregator in Xamarin.Forms

If you’re familiar with Xamarin.Forms, you may be thinking, “Wait, doesn’t Xamarin.Forms already have something like this?” And you’d be right — Xamarin.Forms has its own implementation of Publisher-Subscriber, namely MessagingCenter. But I prefer Prism’s EventAggregator for these reasons:

A few words of warning

I hope by now you’re convinced of the flexibility and utility of EventAggregator, but as always, there are some things to watch out for.

Although EventAggregator holds only weak references to subscribers, it’s still possible that it will hold a strong reference IF the publisher passes a parameter AND the subscriber holds a reference to that parameter. So if you need to do this, it’s a good idea to unsubscribe manually when your subscriber is disposed.

Also, while the decoupled structure of EventAggregator is great, it can lead to increased complexity when overused. There may be a separation of concerns between publisher and subscriber, but there isn’t between the events themselves (they’re all kept in the same EventAggregator). In large projects with many events, this may obscure communication flows. My advice is to only use EventAggregator where it’s necessary. Don’t structure your whole project around it. Like with everything in life, moderation is key.


Follow us to learn more about Prism EventAggregator

There are many reasons to use EventAggregator in your Prism project and for .NET app development. In an upcoming post, we’ll explain HOW to use it with code samples. So make sure to follow us on Facebook, Twitter, and LinkedIn to be notified about it.

Exit mobile version