Skip to content

Latest commit

 

History

History
117 lines (89 loc) · 3.53 KB

mediatr.md

File metadata and controls

117 lines (89 loc) · 3.53 KB
description
MediatR event handling implementation in RCommon

MediatR

Overview

Most enterprise applications have a need for a simple in-memory event bus which implements the observer pattern through the pub/sub pattern. RCommon implements a simple dependency-free event bus for this utility but there are some advantages to using MediatR. MediatR is a mediator pattern implementation which can be used as an event bus. MediatR is a mature library and allows developers to use aspect oriented concepts to manipulate the request pipeline.

Configuration

using Examples.EventHandling.MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RCommon;
using RCommon.EventHandling.Producers;
using RCommon.MediatR;
using RCommon.MediatR.Producers;
using System.Diagnostics;
using System.Reflection;
using static System.Net.Mime.MediaTypeNames;

try
{
    var host = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, builder) =>
                {

                    ConfigurationContainer.Configuration = builder
                        .Build();
                })
                .ConfigureServices(services =>
                {
                    // Configure RCommon
                    services.AddRCommon()
                        .WithEventHandling<MediatREventHandlingBuilder>(eventHandling =>
                        {
                            eventHandling.AddProducer<PublishWithMediatREventProducer>();
                            
                            eventHandling.AddSubscriber<TestEvent, TestEventHandler>();
                        });

                }).Build();         
}
catch (Exception ex)
{   
    Console.WriteLine(ex.ToString());
    
}

Implementation

{% hint style="info" %} The implementation details for how you produce and subscribe to events will not change due to a consistent producer/subscriber API in RCommon. {% endhint %}

Producing the Event

Console.WriteLine("Example Starting");
var eventProducers = host.Services.GetServices<IEventProducer>();
var testEvent = new TestEvent(DateTime.Now, Guid.NewGuid());

foreach (var producer in eventProducers)
{
    Console.WriteLine($"Producer: {producer}");
    await producer.ProduceEventAsync(testEvent);
}

Console.WriteLine("Example Complete");
Console.ReadLine();

Subscribing to the Event

using RCommon.EventHandling.Subscribers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Examples.EventHandling.MediatR
{
    public class TestEventHandler : ISubscriber<TestEvent>
    {
        public TestEventHandler()
        {
                
        }

        public async Task HandleAsync(TestEvent notification, CancellationToken cancellationToken = default)
        {
            Console.WriteLine("I just handled this event {0}", new object[] { notification.ToString() });
            await Task.CompletedTask;
        }
    }
}

{% hint style="info" %} Try running some examples {% endhint %}

{% content-ref url="../../../examples/event-handling-mediatr.md" %} event-handling-mediatr.md {% endcontent-ref %}