Listen to this Post
The .NET community is abuzz with discussions around reducing dependencies on external libraries like MediatR, especially as some open-source tools face commercialization. Pipeline Behaviors can now be replaced by endpoint filters, and FluentValidation can integrate directly with filters, reducing reliance on third-party packages.
You Should Know:
Here’s how to implement CQRS in ASP.NET Core without MediatR, along with practical commands and steps:
1. Endpoint Filters for Pipeline Behaviors
Instead of MediatR’s pipeline behaviors, use ASP.NET Core’s built-in endpoint filters:
public class ValidationFilter<T> : IEndpointFilter where T : class
{
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterFactory next)
{
var validator = context.HttpContext.RequestServices.GetService<IValidator<T>>();
if (validator != null)
{
var model = context.Arguments.FirstOrDefault(a => a?.GetType() == typeof(T)) as T;
if (model != null)
{
var validationResult = await validator.ValidateAsync(model);
if (!validationResult.IsValid)
return Results.ValidationProblem(validationResult.ToDictionary());
}
}
return await next(context);
}
}
2. Manual Command/Query Dispatching
Replace MediatR with a simple dispatcher:
public interface ICommandDispatcher
{
Task Dispatch<TCommand>(TCommand command) where TCommand : ICommand;
}
public class CommandDispatcher : ICommandDispatcher
{
private readonly IServiceProvider _serviceProvider;
public CommandDispatcher(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
public Task Dispatch<TCommand>(TCommand command) where TCommand : ICommand
{
var handler = _serviceProvider.GetRequiredService<ICommandHandler<TCommand>>();
return handler.Handle(command);
}
}
3. FluentValidation with Endpoint Filters
Wire FluentValidation directly into ASP.NET Core:
builder.Services.AddScoped<IValidator<CreateUserCommand>, CreateUserCommandValidator>();
app.MapPost("/users", (CreateUserCommand cmd) => Results.Ok())
.AddEndpointFilter<ValidationFilter<CreateUserCommand>>();
4. Linux/Windows Commands for .NET Devs
- Check .NET Runtime Version (Linux/macOS):
dotnet --list-runtimes
- Clean NuGet Cache (Windows):
dotnet nuget locals all --clear
- Run EF Core Migrations (Cross-Platform):
dotnet ef database update
What Undercode Say
Reducing dependencies on external libraries increases long-term maintainability but requires careful trade-offs. While MediatR simplifies CQRS, native ASP.NET Core features now offer viable alternatives. Manual implementations ensure control but demand rigorous testing. For teams with bandwidth, migrating to endpoint filters and custom dispatchers future-proofs applications against licensing changes.
Expected Output:
A robust, dependency-light CQRS implementation using ASP.NET Core’s native capabilities, validated through FluentValidation and endpoint filters.
Relevant URLs:
References:
Reported By: Iammukeshm Just – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅



