Skip to content

lucadallavalle/template-webapi-aspnet

Repository files navigation

ASP.NET Core WebApi template

This template can be used to bootstrap a working full-fledged ASP.NET Web Api project with a single CLI command (see below).

It contains what I consider to be best practices/patterns, such as CQRS, Mediator, Clean Architecture.

⭐ Like it? Give a star

If you like this project, you learned something from it or you are using it in your applications, please press the star button. Thanks!

Motivation

I found implementations of similar samples/templates to often be overly complicated and over-engineered (IMO). This is an effort to create a more approachable, more maintainable solution that can be used as a starting point for the majority of real-world projects while, at the same time, striving to reach a sensible balance between flexibility and complexity.

Features

Usage

1. Bootstrap your project

Here are a couple of ways to bootstrap a new project starting from this template.

dotnet new template (Recommended)

The easiest way to create a new project from this template:

dotnet new install .
dotnet new webapi-undrivendev -n YourProjectName -o ./YourProjectName

👉 See the complete guide: TEMPLATE-SETUP.md

Cookiecutter template

Probably the best way to bootstrap this project, with just one command, but some dependencies are needed.

  1. Make sure Python is installed
  2. Install cookiecutter.
  3. Bootstrap initial project with the following command: cookiecutter gh:undrivendev/template-webapi-aspnet --checkout cookiecutter

GitHub template

You could use this project as a GitHub template and clone it in your personal account by using the Use this template green button on the top of the page.

Then you'd have to rename classes and namespaces.

2. Apply initial migration

When you have the project ready, it's time to create the initial migration using dotnet-ef (or if you use Rider, like me, you can try this plugin).

Here's an example command using the default solution name, if you changed it you would have to adapt it accordingly:

dotnet ef migrations add --project ./src/Infrastructure/Infrastructure.csproj --context AppDbContext --startup-project ./src/WebApi/WebApi.csproj InitialMigration

The above migration is applied automatically during startup in the dev environment.

Enable tests in CI: the integration tests create their schema by migrating on startup, so they need at least one migration. Once you've added the migration above, uncomment the dotnet test step in .github/workflows/ci.yml to run the full suite on every pull request.

3. Start the application

The default API endpoints should be testable from the Swagger UI.

Enjoy!

4. CI/CD

This template ships a CI workflow at .github/workflows/ci.yml that runs on every pull request: it restores and builds the solution, scans dependencies with grype, and builds the Docker image. The dotnet test step is commented out until you add your first migration (see step 2).

It does not ship a release/deployment pipeline — deploy targets vary too much to template usefully. You need to create your own: typically, on push to main, build and push the image from src/WebApi/Dockerfile to your container registry, then trigger a deploy to your host.

About

Template for ASP.NET WebApi based applications/microservices using CQRS, Mediator pattern, Clean Architecture

Topics

Resources

License

Stars

Watchers

Forks

Contributors