Boilerplate of API in .NET Core 3.1
Travis CI | 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.
- Autofac
- Swagger + Swashbuckle
- EF Core
- Tests
- Integration tests with InMemory database
- Unit tests
- Code quality
- EditorConfig (.editorconfig)
- Analizers (Microsoft.CodeAnalysis.Analyzers, Microsoft.AspNetCore.Mvc.Api.Analyzers)
- Rules
- Code coverage
- Docker
- Dockerfile
- Docker-compose
mysql:8
with DB initializationmcr.microsoft.com/mssql/server:2017-latest
with DB initializationnetcore-boilerplate:local
- Serilog
- Sink: Async
- Enrich: CorrelationId
- DbUp as a db migration tool
- Continuous integration
HappyCode.NetCoreBoilerplate.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
- Simple
- Container registration place - ContainerConfigurator.cs
Serilog
configuration place - SerilogConfigurator.cs- Simple exemplary API controllers - EmployeesController.cs, CarsController.cs
- Example of BackgroundService - PingWebsiteBackgroundService.cs
HappyCode.NetCoreBoilerplate.Core
- Simple MySQL DbContext - EmployeesContext.cs
- Simple MsSQL DbContext - CarsContext.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
- 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
HappyCode.NetCoreBoilerplate.Core.UnitTests
- Extension methods to mock
DbSet
faster - EnumerableExtensions.cs - Exemplary tests - EmployeeRepositoryTests.cs, CarServiceTests.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.
- Give us a star!
Just execute dotnet build
in the root directory, it takes HappyCode.NetCoreBoilerplate.sln
and build everything.
At first, you need to have up and running MySQL and MsSQL database servers on localhost with initialized database by mysql script and mssql script.
Then the application (API) can be started by dotnet run
command executed in the src/HappyCode.NetCoreBoilerplate.Api
directory.
By default it will be available under http://localhost:5000
, but keep in mind that documentation is available under
http://localhost:5000/swagger/
.
Just run docker-compose up
command in the root directory and after successful start of services 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.
Need to migrate from .NET Core 2.2
to .NET Core 3.1
? There's an Microsoft article about this, but you can also, just in case, take a look at my migration commit where you can find the complete list of required changes.
- 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 us a star, appreciate our work. Thanks!
You are very welcome to submit either issues or pull requests to this repository!
For pull request please follow this rules:
- Commit messages should be clear and as much as possible descriptive.
- Rebase if required.
- Make sure that your code compile and run locally.
- Changes do not break any tests and code quality rules.