MediatR.Extensions.RegisterGenericHandlers
1.0.1
dotnet add package MediatR.Extensions.RegisterGenericHandlers --version 1.0.1
NuGet\Install-Package MediatR.Extensions.RegisterGenericHandlers -Version 1.0.1
<PackageReference Include="MediatR.Extensions.RegisterGenericHandlers" Version="1.0.1" />
paket add MediatR.Extensions.RegisterGenericHandlers --version 1.0.1
#r "nuget: MediatR.Extensions.RegisterGenericHandlers, 1.0.1"
// Install MediatR.Extensions.RegisterGenericHandlers as a Cake Addin #addin nuget:?package=MediatR.Extensions.RegisterGenericHandlers&version=1.0.1 // Install MediatR.Extensions.RegisterGenericHandlers as a Cake Tool #tool nuget:?package=MediatR.Extensions.RegisterGenericHandlers&version=1.0.1
MediatR.Extensions.RegisterGenericHandlers
Simple extension method for MediatR to register open generic request handlers.
Created as supplement to the famous MediatR library. (MediatR is the only Dependency).
Installing MediatR.Extensions.RegisterGenericHandlers
You should install MediatR.Extensions.RegisterGenericHandlers with NuGet:
Install-Package MediatR.Extensions.RegisterGenericHandlers
Or via the .NET Core command line interface:
dotnet add package MediatR.Extensions.RegisterGenericHandlers
Either commands, from Package Manager Console or .NET Core CLI, will download and install MediatR.Extensions.RegisterGenericHandlers and all required dependencies.
Registering with IServiceCollection
Extension method should be called on the service collection in your Program.cs file. I typically do it immediatlely after the MediatR registration.
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Startup>())
.RegisterGenericHandlersFromAssemblyContaining<Startup>();
or with an IEnumerable<Assembly>
of assemblies to scan:
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly))
.RegisterGenericHandlers([typeof(Startup).Assembly]);
This registers open generic implementations for:
IRequestHandler<,>
open implementations as transientIRequestHandler<>
open implementations as transient
Example Handler
Below is an example open generic request / handler implementation.
Some concrete class (Foo
) that implements an interface (IFoo
). This will be used as a type parameter for the example.
public interface IFoo
{
string Bar { get; set; }
}
public class Foo : IFoo
{
public string Bar { get; set; } = "Foo Bar";
}
Open request implementation that constrains the type parameter to IFoo
public class GenericRequest<T> : IRequest<string>
where T : class, IFoo
{
public T? Foo { get; set; }
}
Open handler implementation for GenericRequest<T>
public class GenericRequestHandler<T> : IRequestHandler<GenericRequest<T>, string?>
where T : class, IFoo
{
public Task<string?> Handle(GenericRequest<T> request, CancellationToken cancellationToken)
{
request.Foo!.Bar = "Hello From Handler!!";
return Task.FromResult(request.Foo?.Bar);
}
}
Usage from MediatR Send method.
var request = new GenericRequest<Foo>
{
Foo = new()
};
var result = await _mediator.Send(request);
Console.WriteLine(result); //outputs "Hello From Handler!!"
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- MediatR (>= 12.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.