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.
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
dotnet tool install --global dotnet-ef
dotnet ef database update -c ApplicationDbContext -p ./src/Infrastructure/Infrastructure.csproj -s ./src/Web/Web.csprojMyWebAPITemplate.slnF5 or Debug/Start Debuggingdotnet run -p ./src/Web/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/MigrationsThis 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.
This project is MIT licensed - So feel free to use it anyway you like. Suggestions and help are welcomed. ?