Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediatR with ASP.NET Core DI

I'm playing around with the new ASP.NET Core and are currently creating a API that I want to call from a JavaScript frontend.

I want to use the mediator pattern to reduce the coupling, and I have found the Library MediatR from Jimmy Bogard.

My problem consist in wiring it up using the build in DI, I have tried looking at the examples, but can't see to crack how it binds into the ConfigureServices method in the startup class.

Do anybody have any insight?

UPDATE: I got it working, from my ConfigureService method:

services.AddScoped<SingleInstanceFactory>(p => t => p.GetRequiredService(t));  services.Scan(scan => scan         .FromAssembliesOf(typeof(IMediator), typeof(MyHandler.Handler))         .AddClasses()         .AsImplementedInterfaces()); 
like image 451
Nyegaard Avatar asked Feb 15 '16 12:02

Nyegaard


People also ask

What is MediatR in .NET core?

MediatR Requests are very simple request-response style messages, where a single request is synchronously handled by a single handler (synchronous from the request point of view, not C# internal async/await). Good use cases here would be returning something from a database or updating a database.

What is the use of MediatR?

MediatR is an open source implementation of the mediator pattern that doesn't try to do too much and performs no magic. It allows you to compose messages, create and listen for events using synchronous or asynchronous patterns.

What is CQRS and MediatR?

Source from: Microsoft — Command and Query Responsibility Segregation (CQRS) pattern. MediatR is an open source implementation of the mediator pattern that doesn't try to do too much and performs no magic. It allows you to compose messages, create and listen for events using synchronous or asynchronous patterns.


2 Answers

As of July 2016, Jimmy Bogard, author of MediatR, has released a package to register MediatR, and Handlers, with the ASP.Net Core DI service (which is actually the interface IServiceCollection, implemented in Microsoft.Extensions.DependencyInjection and which is not restricted to use solely within ASP.Net Core).

MediatR.Extensions.Microsoft.DependencyInjection

Link to GitHub Project.

Link to NuGet Package information.

A blog post introducing the package and it's capabilities can be found here

Example registration copied directly from the (very short) blog post:

public void ConfigureServices(IServiceCollection services) {   services.AddMvc();    services.AddMediatR(typeof(Startup)); } 

This package performs several functions to enable MediatR, including the required scanning of assemblies for Handlers:

You can either pass in the assemblies where your handlers are, or you can pass in Type objects from assemblies where those handlers reside. The extension will add the IMediator interface to your services, all handlers, and the correct delegate factories to load up handlers. Then in your controller, you can just use an IMediator dependency:

public class HomeController : Controller {   private readonly IMediator _mediator;    public HomeController(IMediator mediator)   {     _mediator = mediator;   }   public IActionResult Index()   {     var pong = _mediator.Send(new Ping {Value = "Ping"});     return View(pong);   } } 
like image 79
dmcquiggin Avatar answered Oct 16 '22 09:10

dmcquiggin


There´s a good tutorial by https://dotnetcoretutorials.com/. This's the example code for the correct installation and configuration of MediatR.

Installing MediatR

The first thing we need to do is install the MediatR nuget package. So from your package manager console run :

Install-Package MediatR

We also need to install a package that allows us to use the inbuilt IOC container in .NET Core to our advantage (We’ll see more of that shortly). So also install the following package :

Install-Package MediatR.Extensions.Microsoft.DependencyInjection

Finally we open up our startup.cs file. In our ConfigureServices method, we need to add in a call to register all of MediatR’s dependencies.

public void ConfigureServices(IServiceCollection services) {     services.AddMediatR(Assembly.GetExecutingAssembly());     //Other injected services.  } 

This is the link: https://dotnetcoretutorials.com/2019/04/30/the-mediator-pattern-part-3-mediatr-library/

I hope this helps.

like image 38
Carlos de Jesus Baez Avatar answered Oct 16 '22 08:10

Carlos de Jesus Baez