๐Ÿ”ง 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:

Code
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:

Code
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:

Code
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):

Code
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

Code
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:

Code
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:

Code
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

Code
MyApp.BackgroundServices/
โ”œโ”€โ”€ Jobs/
โ”‚   โ”œโ”€โ”€ OrderProcessingJob.cs
โ”‚   โ””โ”€โ”€ EmailSenderJob.cs
โ”œโ”€โ”€ Services/
โ”œโ”€โ”€ Configuration/
โ””โ”€โ”€ Extensions/

For Shared Libraries:

Code
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

Code
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.