DEV Community

Cover image for Boilerplate of API in .NET 6
Łukasz Kurzyniec
Łukasz Kurzyniec

Posted on • Updated on • Originally published at github.com

Boilerplate of API in .NET 6

Update!

Boilerplate become .NET 6!
Enter fullscreen mode Exit fullscreen mode

Boilerplate is a piece of code that helps you to quickly kick-off a project or start writing your source code. It is kind of a template - instead
of starting an empty project and adding the same snippets each time, you can use the boilerplate that already contains such code.

Here you can find such boilerplate of API that I build in .NET Core 3.1 .NET 6.

Source code contains

  1. [Autofac](https://autofac.org/) (Removed in PR19)
  2. Swagger + Swashbuckle
  3. FeatureManagement (Feature Flags, Feature Toggles)
  4. HealthChecks
  5. EF Core
  6. Tests
  7. Code quality
  8. Docker
    • Dockerfile
    • Docker-compose
      • mysql:8 with DB initialization
      • mcr.microsoft.com/mssql/server:2017-latest with DB initialization
      • netcore-boilerplate:local
  9. Serilog
  10. DbUp as a db migration tool
  11. Continuous integration

Architecture

Api

  • Simple Startup class - [Startup.cs]
    • MvcCore
    • DbContext (with MySQL)
    • DbContext (with MsSQL)
    • Swagger and SwaggerUI (Swashbuckle)
    • HostedService and HttpClient
    • FeatureManagement
    • HealthChecks
    • MySQL
    • MsSQL
  • Filters
    • Simple ApiKey Authorization filter - [ApiKeyAuthorizationFilter.cs]
    • Action filter to validate ModelState - [ValidateModelStateFilter.cs]
    • Global exception filter - [HttpGlobalExceptionFilter.cs]
  • Configurations
    • Dependency registration place - [ContainerConfigurator.cs]
    • Serilog configuration place - [SerilogConfigurator.cs]
    • Swagger configuration place - [SwaggerConfigurator.cs]
  • Simple exemplary API controllers - [EmployeesController.cs], [CarsController.cs]
  • Example of BackgroundService - [PingWebsiteBackgroundService.cs]

HappyCode.NetCoreBoilerplate.Api

Core

  • Dto models
  • DB models
  • Registration module - [GeneralRegisterModule.cs]
  • Exemplary MySQL repository - [EmployeeRepository.cs]
  • Exemplary MsSQL service - [CarService.cs]
  • AppSettings models
  • Simple MySQL DbContext - [EmployeesContext.cs]
  • Simple MsSQL DbContext - [CarsContext.cs]

HappyCode.NetCoreBoilerplate.Core

DB Migrations

  • Console application as a simple db migration tool - [Program.cs]
  • Sample migration scripts, both .sql and .cs - [S001_AddCarTypesTable.sql], [S002_ModifySomeRows.cs]

HappyCode.NetCoreBoilerplate.Db

Tests

Integration tests

  • Infrastructure
    • Fixture with TestServer - [TestServerClientFixture.cs]
    • TestStartup with InMemory databases - [TestStartup.cs]
    • Simple data feeders - [EmployeeContextDataFeeder.cs], [CarsContextDataFeeder.cs]
  • Exemplary tests - [EmployeesTests.cs], [CarsTests.cs]

HappyCode.NetCoreBoilerplate.Api.IntegrationTests

Unit tests

  • Controller tests - [EmployeesControllerTests.cs]

  • Extension methods to mock DbSet faster - [EnumerableExtensions.cs]

  • Exemplary tests - [EmployeeRepositoryTests.cs], [CarServiceTests.cs]

HappyCode.NetCoreBoilerplate.Core.UnitTests

Load tests

Keep in mind that entire environment has to be up and running.

  • Base class for controller - [LoadTestsBase.cs]
  • Exemplary tests - [EmployeesControllerTests.cs], [CarsControllerTests.cs]

HappyCode.NetCoreBoilerplate.Api.LoadTests

Source code

The full source code with detailed information you can find on my GitHub: https://github.com/lkurzyniec/netcore-boilerplate

Discussion (1)

Collapse
codecubix profile image
codecubix

nice! just what newbie like us needed