Giter VIP home page Giter VIP logo

mediatr's Introduction

MediatR

CI NuGet NuGet MyGet (dev)

Simple mediator implementation in .NET

In-process messaging with no dependencies.

Supports request/response, commands, queries, notifications and events, synchronous and async with intelligent dispatching via C# generic variance.

Examples in the wiki.

Installing MediatR

You should install MediatR with NuGet:

Install-Package MediatR

Or via the .NET Core command line interface:

dotnet add package MediatR

Either commands, from Package Manager Console or .NET Core CLI, will download and install MediatR and all required dependencies.

Using Contracts-Only Package

To reference only the contracts for MediatR, which includes:

  • IRequest (including generic variants)
  • INotification
  • IStreamRequest

Add a package reference to MediatR.Contracts

This package is useful in scenarios where your MediatR contracts are in a separate assembly/project from handlers. Example scenarios include:

  • API contracts
  • GRPC contracts
  • Blazor

Registering with IServiceCollection

MediatR supports Microsoft.Extensions.DependencyInjection.Abstractions directly. To register various MediatR services and handlers:

services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Startup>());

or with an assembly:

services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly));

This registers:

  • IMediator as transient
  • ISender as transient
  • IPublisher as transient
  • IRequestHandler<,> concrete implementations as transient
  • IRequestHandler<> concrete implementations as transient
  • INotificationHandler<> concrete implementations as transient
  • IStreamRequestHandler<> concrete implementations as transient
  • IRequestExceptionHandler<,,> concrete implementations as transient
  • IRequestExceptionAction<,>) concrete implementations as transient

This also registers open generic implementations for:

  • INotificationHandler<>
  • IRequestExceptionHandler<,,>
  • IRequestExceptionAction<,>

To register behaviors, stream behaviors, pre/post processors:

services.AddMediatR(cfg => {
    cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly);
    cfg.AddBehavior<PingPongBehavior>();
    cfg.AddStreamBehavior<PingPongStreamBehavior>();
    cfg.AddRequestPreProcessor<PingPreProcessor>();
    cfg.AddRequestPostProcessor<PingPongPostProcessor>();
    cfg.AddOpenBehavior(typeof(GenericBehavior<,>));
    });

With additional methods for open generics and overloads for explicit service types.

mediatr's People

Contributors

jbogard avatar khellang avatar simoncropp avatar driekus77 avatar alexandruchirita4192 avatar remcoros avatar bartosz6 avatar lilasquared avatar shumigaj avatar maniglia avatar henkmollema avatar asimmon avatar sq735 avatar chrismissal avatar jordy-asebp avatar sergergood avatar dadhi avatar fredimachado avatar mderriey avatar jordywells avatar daniel-white avatar geirsagberg avatar kahbazi avatar dariogriffo avatar seesharper avatar jchannon avatar lodewijksioen avatar twadrianlis avatar z4kn4fein avatar vegar avatar

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.