Skip to content

CleanAPI template, integrates ASP.NET Core API with MediatR and FluentValidations

Notifications You must be signed in to change notification settings

bogdanpolak/aspnet-clean-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clean API Template

ASP.NET Core API using MediatR and FluentValidation to separate concerns with middleware for ValidationException error handling. Project contains template and sample ASP.NET Core application which is refactored classic ASP.NET Core API template (Weather Forecast App).

Important notes

New project setup

  1. Add NuGet packages

    dotnet add package MediatR
    dotnet add package MediatR.Extensions.Microsoft.DependencyInjection
    dotnet add package FluentValidation
    dotnet add package FluentValidation.DependencyInjectionExtensions
    
  2. Copy/Create unit:

    • ValidationErrorHandlingMiddleware.cs - link
    • Recommendations:
      • extract ValidationPipelineBehavior class into the separate unit
      • move it into Core/Behaviors directory
  3. Update Startup.cs:

    // Register method:
    services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationPipelineBehavior<,>));
    var assembly = typeof(Startup).Assembly;
    services.AddMediatR(assembly);
    services.AddValidatorsFromAssembly(assembly);
    // Configure method:
    app.UseMiddleware<ValidationErrorHandlingMiddleware>();
  4. Add Mediator Request and Handler (rename Forecast to your domain subject)

    public class GetForecastQuery : IRequest<Forecast> { ... }
    public class GetForecastQueryHandler : IRequestHandler<GetForecastQuery, Forecast> { ... }
  5. Add Fluent Validator

    public class GetForecastQueryValidator : AbstractValidator<GetForecastQuery> { ... }

About

CleanAPI template, integrates ASP.NET Core API with MediatR and FluentValidations

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published