Popular blog tags

ASP.NET Core Blazor(4)- Event Aggregator Components

Published

https://mikaelkoskinen.net/post/blazor-event-aggregator-razor-components

Blazor.EventAggregator - Event Aggregator for ASP.NET Core 3 Razor Components / Blazor

 

Blazor.EventAggregator is a lightweight Event Aggregator for Razor Components. Razor Components (and formerly known as Blazor) is an upcoming technology included in ASP.NET Core 3.0 (currently in Preview 2).

Event aggregator is used for indirect component to component communication. In event aggregator pattern you have message/event publishers and subscribers. In the case of Razor Components, component can publish its events and other component(s) can react to those events.

Note:

Blazor.EventAggregator is completely based on the work done in Caliburn.Micro. The source code was copied from it and then altered to work with Blazor.

Getting Started

First register EventAggregator as singleton in app’s ConfigureServices:

1
2
3
4
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<EventAggregator.Blazor.IEventAggregator, EventAggregator.Blazor.EventAggregator>();
}

The rest depends on if you’re using components with code-behinds (inheritance) or without inheritance. The following guidance is for code-behind scenarios. I’ll add another example of using event aggregator without the code-behind model.

Creating the publisher

To create an event publishing component, first inject IEventAggregator:

1
2
[Inject]
private IEventAggregator _eventAggregator { get; set; }

Then publish the message when something interesting happens:

1
await _eventAggregator.PublishAsync(new CounterIncreasedMessage());

Here’s a full example of a publisher:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class CounterComponent : ComponentBase
{
    [Inject]
    private IEventAggregator _eventAggregator { get; set; }
 
    public int currentCount = 0;
 
    public async Task IncrementCountAsync()
    {
        currentCount++;
        await _eventAggregator.PublishAsync(new CounterIncreasedMessage());
    }
}
 
public class CounterIncreasedMessage
{
}

Creating the subscriber

To create an event subscriber, also start by injecting IEventAggregator:

1
2
[Inject]
private IEventAggregator _eventAggregator { get; set; }

Then make sure to add and implement the IHandle<TMessageType> interface for all the event’s your component is interested in:

1
2
3
4
5
6
7
public class CounterListenerComponent : ComponentBase, IHandle<CounterIncreasedMessage>
...
public Task HandleAsync(CounterIncreasedMessage message)
{
    currentCount += 1;
    return Task.CompletedTask;
}

Here’s full example of a subscriber:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class CounterListenerComponent : ComponentBase, IHandle<CounterIncreasedMessage>
{
    [Inject]
    private IEventAggregator _eventAggregator { get; set; }
 
    public int currentCount = 0;
 
    protected override void OnInit()
    {
        _eventAggregator.Subscribe(this);
    }
 
    public Task HandleAsync(CounterIncreasedMessage message)
    {
        currentCount += 1;
        return Task.CompletedTask;
    }
}

Samples

The project site contains a full working sample of the code-behind model in the samples-folder.

Project Location

Source code is available through GitHub: https://github.com/mikoskinen/Blazor.EventAggregator

Requirements

The library has been developed and tested using the following tools:

  • .NET Core 3.0 Preview 2
  • ASP.NET Core 3.0 Preview 2
  • Visual Studio 2019

Acknowledgements

Work is based on the code available in Caliburn.Micro.