This is my opinionated, constantly work in progress, starter template, for building REST APIs.
This template is for you if you just want to get started with coding, want a already thought out project structure, that is quite simple and fast to learn, and works on smaller and bigger projects just fine.
- Programming language: C#
- Framework: ASP.NET Core 7 (always the latest version)
- Database: SQL Server (also possible to use other databases)
- ORM: Entity Framework
- Testing: XUnit, Moq & FluentAssertions
- Single process application (monolithic)
- Clean/Onion Architecture
- Follows the most usual REST API principles
- Code structure that handles the most typical use cases with scaling in mind
- Swagger/OpenAPI with UI
- Development/QA/Production configurations
- Database seeding
- Global error handling
- Model mapping
- Validations
- Comprehensive tests (functional, integration and unit tests)
- Logging
- Repository pattern
- Docker support
- ..and more in backlog
app -> App and related files
├─> src -> App source
│ ├─> Web -> Entry point for API - runnable project
│ │ ├─> Controllers -> API endpoints
│ │ ├─> Extensions -> Extension methods
│ │ ├─> Interfaces -> Web project's interfaces
│ │ ├─> Mappers -> Model mappers
│ │ ├─> Middlewares -> Middlewares (e.g. global error handling)
│ │ |─> Models -> Models in and out of API
│ │ ├─> Settings -> Options pattern for App (e.g. connection strings)
│ │ ├─> Validators -> Model validation rules
│ │ ├── Program.cs -> START
│ │ └── Startup.cs -> START
│ ├─> Core -> Holds business logic
│ │ ├─> Dtos -> Data-transfer-object (used for business logic)
│ │ ├─> Entities -> Entity Framework entities for database
│ │ ├─> Interfaces -> Core project's interfaces
│ │ ├─> Mappers -> Dto and Entity mappers
│ │ ├─> Services -> Business logic services
│ └─> Infrastructure -> Data access and external services
│ ├─> Database -> Database access
│ │ ├─> Configurations -> Entity Framework table configurations
│ │ ├─> Migrations -> Entity Framework autogenerated migrations
│ │ ├─> Repositories -> Repository pattern
│ │ ├── AppDbContext.cs -> Base context
│ │ ├── AppSeed.cs -> Database seeding
│ │ └── EfRepository.cs -> Base repository
│ └─> ExternalServices -> Services that access external services (e.g. ext. email service)
└─> tests -> App tests
├── FunctionalTests -> Endpoint tests etc.
│ ├─> Tests -> All tests
│ └─> Utils -> Utility functions (e.g. test database setup)
├── IntegrationTests -> Database tests etc.
│ ├─> Tests -> All tests
│ ├─> Utils -> Utilities functions (e.g. test database setup)
├── UnitTests -> Application logic tests etc.
│ ├─> Tests -> All tests
│ ├─> Utils -> Utility functions
└── Shared -> Model builders and other shared functions for tests
- Download latest .NET SDK (https://dotnet.microsoft.com/download)
- When running first time - Initialize database
-
dotnet tool install --global dotnet-ef dotnet ef database update -c ApplicationDbContext -p ./src/Infrastructure/Infrastructure.csproj -s ./src/Web/Web.csproj
-
- Launch
- Option 1. Launch on Visual Studio
- Open with
MyWebAPITemplate.sln
- Press
F5
orDebug/Start Debugging
- Open with
- Option 2. Launch on CLI
-
dotnet run -p ./src/Web/
-
- Option 1. Launch on Visual Studio
- Navigate to https://localhost:5001/swagger/index.html
- Currently possible only with in-memory
When you need to create a new migration (when you change the database schema), then run this command to tell Entity Framework to handle the needed schema changes for database.
dotnet ef migrations add NewMigrationNameHere --context ApplicationDbContext -p ./src/Infrastructure/Infrastructure.csproj -s ./src/Web/Web.csproj -o Database/Migrations
This project started as my way to handle the information flow of everything that needs to be done when building APIs with single monolithic manner. There are so many things to remember and there are vast amount of choices to be made. Therefore, I made this template, for myself, to store all the practices that I have found practical and I hope this template might also help someone else to learn a thing or two.
In this template, I have collected my knowledge of API building in a practical format. The architectural preferences, library selections and other choices are made, based on best practices, popularity and my own opinionated preferences. The purpose of this template, is to provide a good basic example on how to build Rest APIs with ASP.NET that are scalable, easy to mantain and contain necessary configurations and other goodies. This template is inspired by Microsoft eShopOnWeb reference application and many others.
- To improve the template every time I find something useful, or I come up with a new better way to handle some part of the program.
- To only use concepts or technologies that I have already used in some of my previous projects, so I know if those are worth it. This project is not for tinkering all the experimental stuff, there are other projects for that :)
- To keep the structure simple, but not too simple. This project structure might seem a bit overkill for TODO app, but the structure is here to show one way to handle code structure on bigger projects. The code structure should be able to handle larger systems with larger amount of code and features.
- To keep the template as "monolithic", because I think this might be best way to start a new project. Read more here
- To be opinionated, but I always think to be ready to change anything if there is better way of doing
This project is MIT licensed - So feel free to use it anyway you like. Suggestions and help are welcomed. 🙂