Core API Understanding Project Structure
A well-organized project structure makes the code easy to read, maintain, and scale. This topic explains each part of the BookStore API project and introduces a clean folder structure that is used for the rest of this course.
The Default vs Recommended Structure
The default project created by Visual Studio has only a few files. As the project grows, a proper folder structure keeps everything organized. Here is the recommended structure for the BookStore API:
BookStoreAPI/
├── Controllers/ ← API endpoint handlers
│ └── BooksController.cs
├── Models/ ← Data entities (Book, Author)
│ └── Book.cs
├── DTOs/ ← Request and response shapes
│ ├── BookRequestDto.cs
│ └── BookResponseDto.cs
├── Data/ ← Database context and config
│ └── BookStoreDbContext.cs
├── Repositories/ ← Data access layer
│ ├── IBookRepository.cs
│ └── BookRepository.cs
├── Services/ ← Business logic layer
│ ├── IBookService.cs
│ └── BookService.cs
├── Middleware/ ← Custom middleware
│ └── ExceptionMiddleware.cs
├── Properties/
│ └── launchSettings.json
├── appsettings.json
├── appsettings.Development.json
├── Program.cs
└── BookStoreAPI.csproj
The Layered Architecture
A Web API typically follows a layered architecture. Each layer has one responsibility and communicates only with the layer directly next to it.
HTTP Request
|
v
[ Controller Layer ] ← Receives request, returns response
|
v
[ Service Layer ] ← Business rules and logic
|
v
[ Repository Layer ] ← Database queries (read/write)
|
v
[ Database ] ← SQL Server with Book table
| Layer | Folder | Responsibility |
|---|---|---|
| Controller | Controllers/ | Handle HTTP in and out |
| Service | Services/ | Business rules and logic |
| Repository | Repositories/ | Talk to the database |
| Model | Models/ | Define data structures |
| DTO | DTOs/ | Control what data is exposed |
| Data | Data/ | Database context configuration |
What Each File Does
Program.cs – The Entry Point
This is where the application starts. It configures services and the middleware pipeline. Think of it as the "setup instructions" for the entire API.
var builder = WebApplication.CreateBuilder(args);
// Register all services here
var app = builder.Build();
// Configure middleware pipeline here
app.Run();
BookStoreAPI.csproj – The Project File
This XML file defines which version of .NET is used and which NuGet packages are installed. Visual Studio updates it automatically whenever a package is added.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
appsettings.json – Configuration File
Stores values that can change between environments — like connection strings, JWT secret keys, or API keys. These are read at runtime, not hardcoded in C# files.
{
"ConnectionStrings": {
"BookStoreDB": "Server=(localdb)\\mssqllocaldb;Database=BookStoreDB;Trusted_Connection=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}
launchSettings.json – Dev Server Configuration
Defines the URLs and ports the API runs on during development. This file is not deployed to production.
{
"profiles": {
"BookStoreAPI": {
"applicationUrl": "https://localhost:7001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
What Is a Namespace?
A namespace is like a folder address inside the code. It groups related classes together and prevents naming conflicts. Every class in the BookStore API uses a namespace based on its folder location.
// Controllers/BooksController.cs
namespace BookStoreAPI.Controllers { ... }
// Models/Book.cs
namespace BookStoreAPI.Models { ... }
// Repositories/BookRepository.cs
namespace BookStoreAPI.Repositories { ... }
Understanding Implicit Usings
In .NET 6 and later, common namespaces like System, System.Collections.Generic, and Microsoft.AspNetCore.Mvc are automatically included in every file. This is called implicit usings. It reduces the number of using statements at the top of each file.
// You do NOT need to write these in every file — they are automatic:
// using System;
// using System.Collections.Generic;
// using Microsoft.AspNetCore.Mvc;
// You still need to add non-standard namespaces manually:
using BookStoreAPI.Models;
using BookStoreAPI.Repositories;
BookStore API – Current File Setup
At this point in the course, the project should have these files:
BookStoreAPI/
├── Controllers/
│ └── BooksController.cs ← Created in Topic 3
├── Models/
│ └── Book.cs ← Created in Topic 3
├── appsettings.json
├── Program.cs
└── BookStoreAPI.csproj
Each subsequent topic adds a new file or folder to this structure. By the end of the course, all the folders listed in the recommended structure above will be filled in.
Key Points
- A layered architecture separates concerns: Controller → Service → Repository → Database.
Program.csis the entry point where services and middleware are configured.appsettings.jsonholds configuration values that change between environments.- Namespaces reflect the folder structure and group related classes together.
- Implicit usings in .NET 6+ reduce boilerplate at the top of each file.
