DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • Agile and Quality Engineering: A Holistic Perspective
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  1. DZone
  2. Coding
  3. Frameworks
  4. Event Aggregator for ASP.NET Core 3 Razor Components/Blazor

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

We take a look at this new, lightweight event aggregator and how to implement it in our ASP.NET Core projects using Razor Components.

By 
Mikael Koskinen user avatar
Mikael Koskinen
·
Feb. 20, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
12.7K Views

Join the DZone community and get the full member experience.

Join For Free

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 aggregators are used for indirect component to component communication. In event aggregator patterns, you have message/event publishers and subscribers. In the case of Razor Components, the 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 the app's ConfigureServices:

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 aggregators without the code-behind model.

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 injectingIEventAggregator:

[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:

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

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.

ASP.NET Event ASP.NET Core

Published at DZone with permission of Mikael Koskinen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: