Dsptch 1.1.0
dotnet add package Dsptch --version 1.1.0
NuGet\Install-Package Dsptch -Version 1.1.0
<PackageReference Include="Dsptch" Version="1.1.0" />
<PackageVersion Include="Dsptch" Version="1.1.0" />
<PackageReference Include="Dsptch" />
paket add Dsptch --version 1.1.0
#r "nuget: Dsptch, 1.1.0"
#:package Dsptch@1.1.0
#addin nuget:?package=Dsptch&version=1.1.0
#tool nuget:?package=Dsptch&version=1.1.0
DSPTCH
Lightweight library for implementing CQRS and event dispatching in .NET.
Installation
Install the package from NuGet.
dotnet add package DSPTCH
CQRS
Create a request, query or command by implementing the IRequest, IQuery or ICommand interface.
public record GetProductByIdQuery(Guid Id)
: IQuery<Product>;
public record GetProductByNameQuery(string Name)
: IRequest<List<Product>>;
public record CreateProductCommand(string Name, decimal Price)
: ICommand<Guid>;
IQuery and ICommand inherit from IRequest.
Handlers
Create a handler for each request, query or command by implementing the IRequestHandler interface.
public class GetProductByIdQueryHandler(IProductRepository productRepository)
: IRequestHandler<GetProductByIdQuery, Product?>
{
public Task<Product?> Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
{
return productsRepository.GetById(query.Id);
}
}
Dispatch
Dispatch the request, query or command using the IDispather interface.
app.MapGet("/products/{id}", async (IDispatcher dispatcher, Guid id) =>
{
Product? product = await dispatcher.Dispatch<GetProductByIdQuery, Product?>(new GetProductByIdQuery(id));
return product is not null ? Results.Ok(product) : Results.NotFound();
});
Dispatch decorators
Use dispatch decorators to add cross-cutting concerns to the dispatching process.
// This decorator will be called before and after every request is dispatched
public class LoggingDecorator<TRequest, TResult>(ILogger<TRequest> logger)
: IDispatcherDecorator<TRequest, TResult>
where TRequest : IRequest<TResult>
{
public async Task<TResult> Dispatch(
TRequest command,
RequestHandlerDelegate<TResult> handler,
CancellationToken cancellationToken = default)
{
var requestName = typeof(TRequest).Name;
logger.LogInformation("Dispatching request: {req}", requestName);
try
{
var result = await handler();
logger.LogInformation("Request dispatched: {req}", requestName);
return result;
}
catch (Exception ex)
{
logger.LogError(ex, "Error dispatching request: {req}", requestName);
throw;
}
}
}
// This decorator will be called before and after every query is dispatched
// It won't be called for IRequestHandler or ICommandHandler implementations
public class CachingDecorator<TQuery, TResult>(
IMemoryCache cache,
ILogger<CachingDecorator<TQuery, TResult>> logger)
: IDispatcherDecorator<TQuery, TResult>
where TQuery : IQuery<TResult>
{
}
// This decorator will only be called before and after every IAuthorizableRequest is dispatched
public class AuthorizationDecorator<TQuery, TResult>
: IDispatcherDecorator<TQuery, TResult>
where TQuery : IAuthorizableRequest<TResult>
{
}
Dependency Injection
Use Microsoft DI to register the dispatcher and handlers.
builder.Services.AddDsptch(opts =>
{
opts.RegisterServicesFromAssemblyContaining<Program>();
// Register dispatchers using DsptchConfiguration
opts.RegisterDispatcherDecorator(typeof(LoggingDecorator<,>));
opts.RegisterDispatcherDecorator(typeof(CachingDecorator<,>));
});
// Or register dispatchers manually
builder.Services.TryAddTransient(typeof(IDispatcherDecorator<,>), typeof(LoggingDecorator<,>));
builder.Services.TryAddTransient(typeof(IDispatcherDecorator<,>), typeof(CachingDecorator<,>));
- The
AddDsptchmethod will register the dispatcher and handlers in the specified assemblies.- They will be registered as transient services by default, but this can be changed via
opts.Lifetime.
- They will be registered as transient services by default, but this can be changed via
- The decorators can be registered using the
RegisterDispatcherDecoratormethod.
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
-
net6.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.