MediatR extension for FluentValidation using asp.net core
Install-Package MediatR.Extensions.FluentValidation.Core
dotnet add package MediatR.Extensions.FluentValidation.Core
public void ConfigureServices(IServiceCollection services)
{
// Add framework services etc.
services.AddMvc();
// Add MediatR
services.AddMediatR(typeof(Startup));
//Add FluentValidation
services.AddFluentValidation(new[] {typeof(Startup).Assembly});
//Add other stuffs
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
//Add FluentValidation Middleware
app.UseFluentValidationExceptionHandler();
//Add other stuffs
...
}
public class CreateTodoCommandValidator : AbstractValidator<CreateTodoCommand>
{
public CreateTodoCommandValidator()
{
RuleFor(x => x.Task)
.NotEmpty();
}
}
public class CreateTodoCommand : IRequest<Todo>
{
public CreateTodoCommand(string task, bool isCompleted)
{
Task = task;
IsCompleted = isCompleted;
}
public string Task { get; }
public bool IsCompleted { get;}
}