MediatorForge 1.0.2
dotnet add package MediatorForge --version 1.0.2
NuGet\Install-Package MediatorForge -Version 1.0.2
<PackageReference Include="MediatorForge" Version="1.0.2" />
paket add MediatorForge --version 1.0.2
#r "nuget: MediatorForge, 1.0.2"
// Install MediatorForge as a Cake Addin #addin nuget:?package=MediatorForge&version=1.0.2 // Install MediatorForge as a Cake Tool #tool nuget:?package=MediatorForge&version=1.0.2
MediatorForge
Overview
MediatorForge
is a robust library for integrating validation, authorization, logging, and mediator behaviors in .NET applications. It streamlines the implementation of common application patterns, improving readability and maintainability.
Features
- Fluent Validation: Integrate FluentValidation seamlessly with custom validation systems.
- Authorization: Handle authorization logic elegantly with custom exception handling.
- Logging: Log requests, responses, and performance metrics effectively.
- Pipeline Behaviors: Implement pipeline behaviors for handling cross-cutting concerns in a clean and maintainable way.
- Command and Query Handling: Simplify the handling of commands and queries using custom interfaces.
Installation
Install the package via NuGet:
dotnet add package MediatorForge
Usage
Command Interfaces
Define and handle commands using custom command interfaces.
Example:
/// <summary>
/// Execute ICommand.
/// </summary>
public interface ICommand : ICommand<Unit>
{
}
/// <summary>
/// Execute ICommand.
/// </summary>
public interface ICommand<out TResponse> : IRequest<TResponse>
{
}
/// <summary>
/// Handle ICommand Request.
/// </summary>
/// <typeparam name="TCommand"></typeparam>
public interface ICommandHandler<in TCommand>
: ICommandHandler<TCommand, Unit>
where TCommand : ICommand<Unit>
{
}
/// <summary>
/// Handle ICommand Request.
/// </summary>
/// <typeparam name="TCommand"></typeparam>
/// <typeparam name="TResponse"></typeparam>
public interface ICommandHandler<in TCommand, TResponse>
: IRequestHandler<TCommand, TResponse>
where TCommand : ICommand<TResponse>
where TResponse : notnull
{
}
Query Interfaces
Define and handle queries similarly to commands.
Example:
/// <summary>
/// Execute IQuery.
/// </summary>
public interface IQuery<out TResponse> : IRequest<TResponse>
{
}
/// <summary>
/// Handle IQuery Request.
/// </summary>
/// <typeparam name="TQuery"></typeparam>
/// <typeparam name="TResponse"></typeparam>
public interface IQueryHandler<in TQuery, TResponse>
: IRequestHandler<TQuery, TResponse>
where TQuery : IQuery<TResponse>
where TResponse : notnull
{
}
ValidationBehavior
The ValidationBehavior
class integrates validation logic into your request pipeline.
Example:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
AuthorizationBehavior
The AuthorizationBehavior
class handles authorization logic, ensuring requests are authorized before proceeding.
Example:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehavior<,>));
LoggingBehavior
The LoggingBehavior
class logs requests, responses, and performance metrics.
Example:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
FluentValidatorAdapter
The FluentValidatorAdapter<TRequest>
integrates FluentValidation validators with a custom validation system.
Example:
services.AddMediatorForgeFluentValidatorAdapter();
Dependency Injection
Add and configure the necessary services in your Startup.cs
or Program.cs
file:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Add MediatR
services.AddMediatR(typeof(Startup));
// Add Validators
services.AddTransient<IValidator<CreateItemCommand>, CreateItemCommandValidator>();
// Add Pipeline Behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(AuthorizationBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
// Add FluentValidatorAdapter
services.AddMediatorForgeFluentValidatorAdapter();
// Other service registrations
}
Custom Error Handling Middleware
Ensure your application handles exceptions gracefully and returns meaningful error responses:
public class ErrorHandlerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ErrorHandlerMiddleware> _logger;
private readonly IProblemDetailsService _problemDetailsService;
public ErrorHandlerMiddleware(RequestDelegate next, ILogger<ErrorHandlerMiddleware> logger, IProblemDetailsService problemDetailsService)
{
_next = next;
_logger = logger;
_problemDetailsService = problemDetailsService;
}
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
_logger.LogError(exception, "An unhandled exception has occurred");
context.Response.ContentType = "application/problem+json";
var statusCode = exception switch
{
ValidationException => (int)HttpStatusCode.BadRequest,
AuthorizationException => (int)HttpStatusCode.Forbidden,
UnauthorizedAccessException => (int)HttpStatusCode.Unauthorized,
TooManyRequestsException => (int)HttpStatusCode.TooManyRequests,
_ => (int)HttpStatusCode.InternalServerError
};
context.Response.StatusCode = statusCode;
var problemDetails = exception switch
{
ValidationException validationException => new ValidationProblemDetails(validationException.Errors)
{
Status = statusCode,
Title = "Validation Error",
Instance = context.Request.Path
},
AuthorizationException => _problemDetailsService.CreateProblemDetails(context, statusCode, "Access Denied", exception.Message, exception),
UnauthorizedAccessException => _problemDetailsService.CreateProblemDetails(context, statusCode, "Unauthorized", exception.Message, exception),
TooManyRequestsException => _problemDetailsService.CreateProblemDetails(context, statusCode, "Too Many Requests", exception.Message, exception),
_ => _problemDetailsService.CreateProblemDetails(context, statusCode, "An unexpected error occurred", exception.Message, exception)
};
return context.Response.WriteAsJsonAsync(problemDetails);
}
}
Documentations
License
This project is licensed under the MIT License.
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.4.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- ResultifyCore (>= 1.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MediatorForge:
Package | Downloads |
---|---|
MediatorForge.Adapters
MediatorForge.Adapters provides a seamless integration of MediatorForge validators with custom validation systems in C# applications. Key Features: - Provides adapters to connect MediatorForge validation with custom systems. - Ensures consistent validation logic across different layers. - Simplifies integration and enhances code maintainability. This library is designed to work with MediatorForge, ensuring smooth and efficient validation processes in your applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.