Boilerplate of API in .NET Core 3.1
.NET 8
GitHub | Codecov |
---|---|
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.
Intention - The intention behind this project is to mix a variety of different approaches to show different available paths. That's why you can find here the Service approach mixed-up with Repository approach, or old-fashioned controllers mixed-up with brand new minimal API in a separate module (modular approach). As well as, it's a kind of playground for exploring frameworks, packages, tooling. At the end, You are in charge, so it's your decision to which path you would like to follow.
- Central Package Management (CPM)
[Autofac](https://autofac.org/) (Removed in PR19)- Swagger + Swashbuckle
- FeatureManagement (Feature Flags, Feature Toggles)
- HealthChecks
- EF Core
- Dapper
- Tests
- Integration tests with InMemory database
- FluentAssertions
- xUnit
- Verify
- Verify.Http
- TestServer
- Unit tests
Load tests(Removed in PR135)[NBomber](https://nbomber.com/)
- Integration tests with InMemory database
- Code quality
- Architectural tests (conventional tests)
- Analyzers
- Code analysis rule set HappyCode.NetCoreBoilerplate.ruleset
- Code analysis with CodeQL
- Code coverage
- EditorConfig (.editorconfig)
- Docker
- Dockerfile
- Docker-compose
mysql:8
with DB initializationmcr.microsoft.com/mssql/server:2017-latest
with DB initializationnetcore-boilerplate:compose
- Serilog
- Sink: Async
- DbUp as a db migration tool
- Continuous integration
HappyCode.NetCoreBoilerplate.Api
- The entry point of the app - Program.cs
- 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
- Simple
- Configurations
Serilog
configuration place - SerilogConfigurator.csSwagger
registration place - SwaggerRegistration.cs
- Simple exemplary API controllers - EmployeesController.cs, CarsController.cs, PingsController.cs
- Example of BackgroundService - PingWebsiteBackgroundService.cs
HappyCode.NetCoreBoilerplate.Core
- Models
- Dto models
- DB models
- AppSettings models - Settings
- DbContexts
- MySQL DbContext - EmployeesContext.cs
- MsSQL DbContext - CarsContext.cs
- Core registrations - CoreRegistrations.cs
- Exemplary MySQL repository - EmployeeRepository.cs
- Exemplary MsSQL service - CarService.cs
HappyCode.NetCoreBoilerplate.Db
- 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.Api.IntegrationTests
- 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.UnitTests
- Exemplary tests - EmployeesControllerTests.cs
- Unit tests of
ApiKeyAuthorizationFilter.cs
- ApiKeyAuthorizationFilterTests.cs
HappyCode.NetCoreBoilerplate.Core.UnitTests
- Extension methods to mock
DbSet
faster - EnumerableExtensions.cs - Exemplary tests - EmployeeRepositoryTests.cs, CarServiceTests.cs
HappyCode.NetCoreBoilerplate.ArchitecturalTests
- Exemplary tests - ApiArchitecturalTests.cs, CoreArchitecturalTests.cs
Totally separate module, developed with a modular monolith approach.
The code organized around features (vertical slices).
HappyCode.NetCoreBoilerplate.BooksModule
- Features
- Delete book - Endpoint.cs, Command.cs
- Get book - Endpoint.cs, Query.cs
- Get books - Endpoint.cs, Query.cs
- Upsert book - Endpoint.cs, Command.cs
- Sqlite db initializer - DbInitializer.cs
- Module configuration place - BooksModuleConfigurations.cs
HappyCode.NetCoreBoilerplate.BooksModule.IntegrationTests
- Infrastructure
- Fixture with TestServer - TestServerClientFixture.cs
- Very simple data feeder - BooksDataFeeder.cs
- Exemplary tests - BooksTests.cs
Generally it is totally up to you! But in case you do not have any plan, You can follow below simple steps:
- Download/clone/fork repository
⤵️ - Remove components and/or classes that you do not need to 🔥
- Rename files (e.g. sln, csproj, ruleset), folders, namespaces etc 📝
- Appreciate the work ⭐
Just execute dotnet build
in the root directory, it takes HappyCode.NetCoreBoilerplate.sln
and build everything.
When running on
Linux
(i.e. WSL), make sure that all docker files (dockerfile, docker-compose and all mssql files) have line endingsLF
.
Just run docker-compose up
command in the root directory. After successful build and start of all services visit http://localhost:5000/swagger/.
To check that API has connection to both MySQL and MsSQL databases visit http://localhost:5000/health/.
When running just a container, then features like cars
and employees
might be disabled.
To do so, execute docker build . -t netcore-boilerplate:local
in the root directory, and then docker run --rm -p 5000:8080 --name netcore-boilerplate netcore-boilerplate:local
.
After all, visit http://localhost:5000/swagger/.
When the entire environment is up and running, you can additionally run a migration tool to add some new schema objects into MsSQL DB.
To do that, go to src/HappyCode.NetCoreBoilerplate.Db
directory and execute dotnet run
command.
Run dotnet test
command in the root directory, it will look for test projects in HappyCode.NetCoreBoilerplate.sln
and run them.
- any idea? Please create an issue.
If:
- you like this repo/code,
- you learn something,
- you are using it in your project/application,
then please give me a star
, appreciate my work. Thanks!