Git Product home page Git Product logo

blazor.eventaggregator's Introduction

Blazor.EventAggregator

Blazor.EventAggregator is a lightweight Event Aggregator for Blazor.

  • 3/2020: Updated to work with .NET Core v3.1.0

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 Blazor, component can publish its events and other component(s) can react to those events.

NuGet

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.

Also note that the library has only been tested with the server-side version of Blazor. There's no known issues with the WebAssembly-version of Blazor.

Getting Started

First register EventAggregator in app’s ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddEventAggregator();
}

The rest depends on if you’re using components with code-behinds (inheritance) or without inheritance. The following guidance is for code-behind scenarios.

Creating the publisher

To create an event publishing component, first inject IEventAggregator:

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

Then publish the message when something interesting happens:

await _eventAggregator.PublishAsync(new CounterIncreasedMessage());

Here’s a full example of a publisher:

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:

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

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

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

Here’s full example of a subscriber:

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;
    }
}

Note about auto refresh

The library can try to automatically call subscriber component's StateHasChanged after it has handled the event. By default this functionality is disabled. You can enable it through options:

services.AddEventAggregator(options => options.AutoRefresh = true);

Auto refresh is based on reflection and it assumes that the subscriber inherits from ComponentBase.

Samples

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

Requirements

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

  • .NET Core 3.1
  • Visual Studio 2019

Acknowledgements

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.