Skip to content

SilasReinagel/LiteMediator.DotNet

Repository files navigation

LiteMediator

Build status Nuget Codacy Badge License

LiteMediator Logo

Mediator pattern is a very useful way to build your API in a modular, decomposed way, and then aggregate your functionality into one common application interface. The senders and receivers of messages do not know about each other, instead they depend upon the message itself. It is one of the best ways of building horizontally-organized systems.

The purpose of this project is to provide a very simple, no-fluff, single source file .NET Mediator implementation. This project is designed to offer maximum value with minimal setup/configuration/learning. It is unintrusive and avoids the complexity and lack of clarity provided by other .NET Mediator implementations such as MediatR and Mediator.Net.


Usage

Add to Project

This library contains a single source file with two variants: AsyncMediator.cs and SyncMediator.cs

Use AsyncMediator.cs in projects that are primary asynchonrous and use a lot of async Task. Use SyncMediator.cs in all other projects.

Copy-paste the selected file directly into your project.

If you are required to use NuGet for a company project, this library is also available on Nuget.org. Install-Package LiteMediator

Request / Response

Async Mediator

var mediator = new AsyncMediator();

mediator.Register<CreateUserRequest, UserCreatedResponse>(
    async req => await _datasource.InsertUserAsync(req));

var response = await mediator.GetResponse<CreateUserRequest, UserCreatedResponse>(
    new CreateUserRequest(...));

Sync Mediator

var mediator = new SyncMediator();

mediator.Register<CreateUserRequest, UserCreatedResponse>(
   req => _datasource.InsertUser(req));

var response = mediator.GetResponse<CreateUserRequest, UserCreatedResponse>(
    new CreateUserRequest(...));

Messaging

Async Mediator

var mediator = new AsyncMediator();

mediator.Register<NewProductAdded>(
    async msg => await _catalog.Update(msg));

await mediator.Publish(new NewProductAdded(...));

Sync Mediator

var mediator = new SyncMediator();

mediator.Register<NewProductAdded>(
    msg => _catalog.Update(msg));

mediator.Publish(new NewProductAdded(...));

License

You may use this code in part or in full however you wish.
No credit or attachments are required.

About

Lightweight Single Source File .NET Mediator library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages