DEV Community

loading...
Cover image for Boilerplate of API in .NET Core 3.1

Boilerplate of API in .NET Core 3.1

lkurzyniec profile image Łukasz Kurzyniec Originally published at github.com ・2 min read

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.

Source code contains

  1. Autofac
  2. Swagger + Swashbuckle
  3. EF Core
  4. Tests
  5. Code quality
  6. Docker
    • Dockerfile
    • Docker-compose
      • mysql:8 with DB initialization
      • mcr.microsoft.com/mssql/server:2017-latest with DB initialization
      • netcore-boilerplate:local
  7. Serilog
  8. DbUp as a db migration tool
  9. Continuous integration

Architecture

Api

  • Simple Startup class - [Startup.cs]
    • MvcCore
    • DbContext (with MySQL)
    • DbContext (with MsSQL)
    • Swagger and SwaggerUI (Swashbuckle)
    • HostedService
    • HttpClient
    • HealthCheck
  • 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

  • Simple MySQL DbContext - [EmployeesContext.cs]
  • Simple MsSQL DbContext - [CarsContext.cs]
  • Exemplary MySQL repository - [EmployeeRepository.cs]
  • Exemplary MsSQL service - [CarService.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

  • 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

Source code

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

Discussion (1)

pic
Editor guide
Collapse
codecubix profile image
codecubix

nice! just what newbie like us needed