π§ 1. Use a Clean Architecture (Onion or Hexagonal)
Separate concerns clearly with layered architecture:
Layers:
- Presentation/UI (ASP.NET MVC/Razor Pages/API Controllers)
- Application (Use cases, commands, queries, interfaces)
- Domain (Entities, value objects, business rules)
- Infrastructure (Database, file system, email, external APIs)
Example Folder Structure:
/src
/Web (Presentation Layer)
/Application
/Domain
/Infrastructure
/tests
/UnitTests
/IntegrationTests
π§± 2. Modularize by Feature, Not Type
Avoid grouping files by technical type (e.g., Controllers, Models, Views). Instead, use feature folders:
/Features
/Orders
OrderController.cs
OrderService.cs
Order.cs
/Products
ProductController.cs
ProductService.cs
Product.cs
This improves encapsulation and makes features easier to test and modify.
π¦ 3. Use Dependency Injection Everywhere
Rely on ASP.NET Core’s built-in DI system. Avoid static classes or service locators.
Register interfaces and implementations in Startup.cs
or via extension methods in Infrastructure
.
π§ͺ 4. Separate Test Projects
Maintain dedicated test projects for unit and integration tests, mirroring the structure of the main app.
/tests
/App.UnitTests
/App.IntegrationTests
π 5. Use Projects for Separation
Split into multiple class libraries:
MyApp.Web
MyApp.Application
MyApp.Domain
MyApp.Infrastructure
This enforces compile-time separation and better boundaries.
π 6. Follow SOLID Principles
- Single Responsibility: One class = one job.
- Open/Closed: Code open for extension, closed for modification.
- Liskov: Use base classes/interfaces safely.
- Interface Segregation: Prefer small interfaces.
- Dependency Inversion: Depend on abstractions.
Here are the essential folder, solution, and project structures for large ASP.NET applications:
Solution Structure
Multi-Project Solution Layout:
MySolution.sln
βββ src/
β βββ MyApp.Domain/
β βββ MyApp.Application/
β βββ MyApp.Infrastructure/
β βββ MyApp.Web.API/
β βββ MyApp.Web.MVC/
β βββ MyApp.Shared/
βββ tests/
β βββ MyApp.Domain.Tests/
β βββ MyApp.Application.Tests/
β βββ MyApp.Infrastructure.Tests/
β βββ MyApp.Integration.Tests/
βββ docs/
βββ scripts/
βββ tools/
For Microservices/Multiple Bounded Contexts:
MySolution.sln
βββ src/
β βββ Services/
β β βββ MyApp.Orders/
β β β βββ MyApp.Orders.API/
β β β βββ MyApp.Orders.Domain/
β β β βββ MyApp.Orders.Infrastructure/
β β βββ MyApp.Users/
β β βββ MyApp.Inventory/
β βββ Shared/
β β βββ MyApp.Shared.Kernel/
β β βββ MyApp.Shared.Infrastructure/
β β βββ MyApp.Shared.Contracts/
β βββ Gateways/
β βββ MyApp.API.Gateway/
Project Structure Patterns
Clean Architecture Project Layout:
MyApp.Domain/
βββ Entities/
βββ ValueObjects/
βββ Enums/
βββ Interfaces/
βββ Events/
βββ Exceptions/
βββ Specifications/
MyApp.Application/
βββ Commands/
βββ Queries/
βββ Handlers/
βββ Services/
βββ Interfaces/
βββ DTOs/
βββ Mappings/
βββ Validators/
MyApp.Infrastructure/
βββ Data/
β βββ Configurations/
β βββ Migrations/
β βββ Repositories/
βββ Services/
βββ External/
βββ Caching/
MyApp.Web.API/
βββ Controllers/
βββ Middleware/
βββ Filters/
βββ Models/
β βββ Requests/
β βββ Responses/
βββ Configuration/
βββ Extensions/
Feature-Based Structure (Alternative):
MyApp.Web.API/
βββ Features/
β βββ Orders/
β β βββ GetOrder/
β β β βββ GetOrderQuery.cs
β β β βββ GetOrderHandler.cs
β β β βββ GetOrderValidator.cs
β β βββ CreateOrder/
β β βββ OrdersController.cs
β βββ Users/
β βββ Products/
βββ Common/
β βββ Behaviors/
β βββ Exceptions/
β βββ Extensions/
βββ Infrastructure/
Folder Organization Within Projects
MyApp.Domain/
βββ Aggregates/
β βββ Order/
β β βββ Order.cs
β β βββ OrderItem.cs
β β βββ IOrderRepository.cs
β βββ User/
βββ Common/
β βββ BaseEntity.cs
β βββ IAggregateRoot.cs
β βββ DomainEvent.cs
βββ ValueObjects/
β βββ Money.cs
β βββ Address.cs
β βββ Email.cs
βββ Exceptions/
βββ DomainException.cs
Infrastructure Project Structure:
MyApp.Infrastructure/
βββ Data/
β βββ Context/
β β βββ ApplicationDbContext.cs
β βββ Configurations/
β β βββ OrderConfiguration.cs
β β βββ UserConfiguration.cs
β βββ Repositories/
β β βββ OrderRepository.cs
β β βββ BaseRepository.cs
β βββ Migrations/
βββ Services/
β βββ EmailService.cs
β βββ FileStorageService.cs
βββ External/
β βββ PaymentGateway/
β βββ NotificationService/
βββ Caching/
βββ RedisCacheService.cs
API Project Structure:
MyApp.Web.API/
βββ Controllers/
β βββ V1/
β β βββ OrdersController.cs
β β βββ UsersController.cs
β βββ V2/
βββ Models/
β βββ Requests/
β β βββ CreateOrderRequest.cs
β β βββ UpdateOrderRequest.cs
β βββ Responses/
β β βββ OrderResponse.cs
β βββ Common/
β βββ PagedResponse.cs
βββ Middleware/
β βββ ExceptionHandlingMiddleware.cs
β βββ RequestLoggingMiddleware.cs
βββ Filters/
β βββ ValidateModelFilter.cs
βββ Configuration/
β βββ DependencyInjection.cs
β βββ SwaggerConfiguration.cs
βββ Extensions/
βββ ServiceCollectionExtensions.cs
Specialized Structures
MyApp.BackgroundServices/
βββ Jobs/
β βββ OrderProcessingJob.cs
β βββ EmailSenderJob.cs
βββ Services/
βββ Configuration/
βββ Extensions/
For Shared Libraries:
MyApp.Shared.Kernel/
βββ Extensions/
βββ Utilities/
βββ Constants/
βββ Attributes/
βββ Interfaces/
MyApp.Shared.Contracts/
βββ Events/
βββ Commands/
βββ Queries/
βββ DTOs/
Project Dependencies
Dependency Flow:
- Web.API → Application + Infrastructure
- Application → Domain only
- Infrastructure → Domain + Application (for interfaces)
- Domain → No dependencies
- Shared.Kernel → No dependencies
- Shared.Contracts → Shared.Kernel only
File Naming Conventions
Controllers: OrdersController.cs
, UsersController.cs
Services: IOrderService.cs
, OrderService.cs
Repositories: IOrderRepository.cs
, OrderRepository.cs
DTOs: CreateOrderDto.cs
, OrderResponseDto.cs
Commands/Queries: CreateOrderCommand.cs
, GetOrderQuery.cs
Handlers: CreateOrderCommandHandler.cs
, GetOrderQueryHandler.cs
Configuration Files Organization
MyApp.Web.API/
βββ appsettings.json
βββ appsettings.Development.json
βββ appsettings.Production.json
βββ appsettings.Staging.json
βββ Configuration/
βββ DatabaseSettings.cs
βββ JwtSettings.cs
βββ ExternalServiceSettings.cs
This structure provides clear separation of concerns, makes navigation intuitive for large teams, and scales well as your application grows. The key is consistency across all projects and establishing clear naming conventions that everyone follows.