Introduction
In real-world projects, it is not enough for code to simply work. Code should also be understandable, testable, replaceable, and easy to extend when new features are introduced. As an application grows, developers therefore separate responsibilities into architectural layers instead of keeping all code in one project or one undifferentiated folder structure.
In this lesson, we will examine the basic building blocks of modern .NET applications and establish the architectural foundation for the application developed throughout this course. We will use a product catalog as an example. Requests arriving through a web client or REST API will execute business rules, while the data access layer will communicate with persistent sources such as SQL Server.
Layered architecture does not merely mean dividing files into folders. Its main purpose is to make each component’s responsibility explicit and to keep dependencies between components under control. Microsoft distinguishes logical layers from physical tiers: layers describe separation within an application, while tiers describe deployment across different processes or servers. ([learn.microsoft.com](https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures?utm_source=openai))
Core components of a .NET application
.NET and the runtime
.NET is a platform for building applications with languages such as C#. Compilation, execution, memory management, file operations, networking, and the base class libraries are all parts of the .NET ecosystem. ASP.NET Core runs on .NET and provides the framework for building web applications and HTTP-based APIs.
In an ASP.NET Core application, Program.cs is commonly the entry point. It creates the application, registers services in the dependency injection container, and configures the HTTP request pipeline. Modern ASP.NET Core templates generally use the minimal hosting model, where startup and service configuration are expressed in a compact form.
Solution and project
A solution is a container used to manage one or more projects together. A project is a buildable application or class library. A typical solution for this course might contain the following projects:
| Project | Type | Primary responsibility |
|---|---|---|
Catalog.Api | ASP.NET Core Web API | Receive HTTP requests and produce responses |
Catalog.Business | Class Library | Execute business rules and application services |
Catalog.DataAccess | Class Library | Communicate with databases and other data sources |
Catalog.Tests | Test project | Verify the behavior of the layers |
The important point is that project boundaries are stronger than folder boundaries. Separate projects make incorrect direct usage of another layer more visible through compile-time references.
Responsibilities of the three layers
1. Presentation layer
The Presentation layer is the public face of the application. In a REST API, it commonly contains controllers, request models, response models, HTTP status-code decisions, and API-specific validation.
Its job is to translate an HTTP request into an application call, not to make business decisions. For example, ProductsController may receive a request to create a product, but it should not decide whether a product price can be negative. That rule belongs in a Business-layer service.
2. Business layer
The Business layer is where the application makes decisions. It contains business rules, use cases, services, interfaces, and, when appropriate, data-transfer objects.
Rules such as “two products cannot have the same SKU,” “inactive products must not appear in the sales list,” or “a discount must be between zero and fifty percent” belong here. As a result, the same rules can be applied consistently not only to API requests, but also to a future web interface, background job, or another client.
3. Data Access layer
The Data Access layer focuses on how data is stored and retrieved. SQL queries, Entity Framework Core configuration, repository implementations, connection settings, and persistence-specific models may be placed here.
This layer should not decide whether a product is eligible for sale. Its responsibility is to retrieve or persist the data requested by the Business layer. If SQL Server is replaced later, the scope of the change can remain limited.
Designing the dependency direction
In a simple three-layer model, the flow is usually Presentation → Business → Data Access. A request travels downward and the result returns upward. The important design rule is to create only the dependencies that are actually needed.
For example, the Business layer can define an abstraction for the data it needs:
namespace Catalog.Business.Products;
public interface IProductRepository
{
Task<IReadOnlyList<Product>> GetActiveAsync(
CancellationToken cancellationToken = default);
}The Data Access layer can implement that contract:
using Catalog.Business.Products;
public sealed class SqlProductRepository : IProductRepository
{
public Task<IReadOnlyList<Product>> GetActiveAsync(
CancellationToken cancellationToken = default)
{
// SQL Server access will be added in later lessons.
return Task.FromResult<IReadOnlyList<Product>>(
Array.Empty<Product>());
}
}With this approach, the Business layer defines the contract it needs, while the Data Access layer decides how to implement that contract with SQL Server. The service consuming the interface does not need to know the concrete SQL implementation. ASP.NET Core’s built-in dependency injection support is designed to support this kind of loosely coupled structure. ([learn.microsoft.com](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0&utm_source=openai))
Practical start: creating the solution and projects
The following commands create the basic structure with the .NET CLI. The target framework may differ according to the SDK installed on your machine. Use one consistent target framework throughout the course.
mkdir CatalogApp
cd CatalogApp
dotnet new sln -n CatalogApp
dotnet new webapi -n Catalog.Api
dotnet new classlib -n Catalog.Business
dotnet new classlib -n Catalog.DataAccess
dotnet sln add Catalog.Api/Catalog.Api.csproj
dotnet sln add Catalog.Business/Catalog.Business.csproj
dotnet sln add Catalog.DataAccess/Catalog.DataAccess.csprojNow add the project references:
dotnet add Catalog.Api reference Catalog.Business
dotnet add Catalog.DataAccess reference Catalog.Business
dotnet add Catalog.Api reference Catalog.DataAccessThe last reference allows the application’s composition root to register the concrete Data Access implementation with dependency injection. However, the API project should not use Data Access classes directly throughout its code. Keep this dependency as close as possible to Program.cs.
Defining the first application service
We can create an API-independent service contract and implementation in the Business layer:
public sealed record Product(int Id, string Name, decimal Price);
public interface IProductService
{
Task<IReadOnlyList<Product>> GetActiveAsync(
CancellationToken cancellationToken = default);
}
public sealed class ProductService : IProductService
{
private readonly IProductRepository _repository;
public ProductService(IProductRepository repository)
{
_repository = repository;
}
public Task<IReadOnlyList<Product>> GetActiveAsync(
CancellationToken cancellationToken = default)
{
return _repository.GetActiveAsync(cancellationToken);
}
}The registrations can be added in the API startup:
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IProductRepository, SqlProductRepository>();AddScoped is commonly used for services whose lifetime should be tied to an HTTP request. Lifetime selection should consider whether a service holds state and how resources such as database contexts are managed. ([learn.microsoft.com](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0&utm_source=openai))
Visualizing the request flow
- The client sends
GET /api/products. - A controller in the Presentation layer receives the request.
- The controller calls the Business layer through
IProductService. - The Business service requests data through
IProductRepository. - The Data Access implementation reads data from SQL Server.
- The result returns from Data Access to Business and then to Presentation.
- The controller converts the result into an appropriate HTTP response.
In this flow, the controller does not contain SQL queries, the Business service does not produce HTTP status codes, and the Data Access layer does not return JSON directly to the client. Each component stays within its responsibility.
Common mistakes
- Creating layers only with folders: Folder organization inside one project does not create a strong compile-time boundary.
- Putting business rules in controllers: Controllers become difficult to test, reuse, and maintain.
- Creating
SqlConnectiondirectly in Business: This couples business logic to a specific database technology. - Allowing every layer to reference every other layer: This creates cycles and an architecture that is difficult to understand.
- Using
neweverywhere instead of DI: Concrete implementations become tightly coupled, making test doubles harder to use. - Choosing project names without considering responsibility: A project named “Repository” should not automatically contain every business rule.
Short practice exercise
Task: Create the basic layer boundaries for the CatalogApp solution.
- Create
Product,IProductRepository,IProductService, andProductServiceinCatalog.Business. - Create
SqlProductRepositoryinCatalog.DataAccess. - Register the services with dependency injection in
Catalog.Api. - Build the API project and inspect the project references.
- Draw the request flow as Presentation → Business → Data Access.
As an additional check, verify that the Business project does not reference either the API or Data Access project. This is an important exercise for understanding dependency direction.
Conclusion
In this lesson, you learned the difference between solutions and projects, the responsibilities of the three primary layers, and how dependencies should be directed. The Presentation layer communicates with the outside world, the Business layer manages decisions and rules, and the Data Access layer works with persistent data sources.
Good layered design is not simply about making every class small. Its real purpose is to limit the impact of change and make different parts of the application easier to understand and test independently. In the next lessons, we will extend this foundation and gradually build the API’s HTTP behavior and the real data flow between the layers.