ShopApI/
├── Controllers/ # API Controllers (HTTP endpoints)
│ └── ProductsController.cs
├── Models/ # Domain/Entity models
│ └── Product.cs
├── DTOs/ # Data Transfer Objects (API contracts)
│ └── ProductDto.cs
├── Services/ # Business logic layer
│ ├── IProductService.cs
│ └── ProductService.cs
├── Repositories/ # Data access layer
│ ├── IProductRepository.cs
│ └── ProductRepository.cs
├── Data/ # Database context and configurations
│ └── ApplicationDbContext.cs
├── Middleware/ # Custom middleware
│ └── ExceptionHandlingMiddleware.cs
├── Helpers/ # Utility classes and helpers
├── Properties/ # Launch settings
├── Program.cs # Application entry point
└── appsettings.json # Configuration files
Contains API controllers that handle HTTP requests and responses.
- Define your API endpoints here
- Controllers should be lightweight and delegate business logic to services
- Use attribute routing:
[Route("api/[controller]")]
Example:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
// Your endpoints here
}Contains domain entities that represent your database tables.
- These are your core business objects
- Map directly to database tables when using Entity Framework
Example:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
// Other properties...
}Contains objects used for API requests and responses.
- Separate from domain models for security and flexibility
- Include: CreateDto, UpdateDto, ResponseDto
- Helps prevent over-posting attacks
Example:
public class CreateProductDto { /* properties */ }
public class ProductDto { /* properties */ }Contains business logic and orchestrates operations.
- Implements interfaces for dependency injection
- Handles complex business rules
- Coordinates between repositories
Example:
public interface IProductService { /* methods */ }
public class ProductService : IProductService { /* implementation */ }Contains data access logic.
- Abstracts database operations
- Implements the Repository pattern
- Works with DbContext (Entity Framework)
Example:
public interface IProductRepository { /* methods */ }
public class ProductRepository : IProductRepository { /* implementation */ }Contains database context and configurations.
- ApplicationDbContext for Entity Framework
- Entity configurations
- Database migrations
Contains custom middleware components.
- Exception handling
- Authentication/Authorization
- Logging
- Request/Response modification
Contains utility classes and helper methods.
- Extension methods
- Mapping helpers
- Validation utilities
- Common functions
- .NET 8 SDK
- Your preferred IDE (Visual Studio, VS Code, Rider)
dotnet restore
dotnet build
dotnet runThe API will be available at:
- HTTPS: https://localhost:7xxx
- HTTP: http://localhost:5xxx
- Swagger UI: https://localhost:7xxx/swagger
To add Entity Framework Core with SQL Server:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.ToolsUpdate appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=ShopDB;Trusted_Connection=true;TrustServerCertificate=true;"
}
}Uncomment the DbContext registration in Program.cs and the context in Data/ApplicationDbContext.cs.
dotnet ef migrations add InitialCreate
dotnet ef database update- Separation of Concerns: Keep controllers thin, move logic to services
- Dependency Injection: Use interfaces and inject dependencies
- DTOs: Always use DTOs for API contracts, never expose domain models directly
- Async/Await: Use async methods for I/O operations
- Error Handling: Use middleware for global exception handling
- Logging: Inject ILogger and log important operations
- Validation: Use Data Annotations or FluentValidation
- Implement your business logic in Services
- Add database configuration and Entity Framework
- Create additional controllers for other resources
- Add authentication and authorization
- Implement input validation
- Add unit tests
- Configure environment-specific settings