LESSON 2 OF 16

2. Creating the Solution Structure and Separating Layers

Build a .NET solution with Web, API, Business, Data Access, Entity, and Shared projects, then organize project references for a maintainable 3-tier architecture.

90 dk 84

Introduction

In the previous lesson, we examined why layered architecture is useful in .NET applications and separated the responsibilities of the user interface, business rules, and data access. In this lesson, we will turn that conceptual model into a working solution structure. Instead of placing every class in one project, we will create projects with clear responsibilities and controlled dependencies.

Throughout the course, we will use a sample product management system. The web application will handle user interaction, the API will receive HTTP requests, the Business project will apply business rules, the Data Access project will handle database operations, the Entity project will contain core models, and the Shared project will contain reusable contracts and common types.

The examples target the .NET 10 SDK and ASP.NET Core templates. If you are using another .NET version, change the target framework in the project files to match your installed SDK. The overall approach is also applicable to .NET 6 and later versions.

Understanding solutions, projects, and layers

A solution is a container used to manage multiple projects together. A project is a buildable application or class library. A layer describes a logical responsibility in the application. Creating a layer as a separate project provides more than folder organization; it also allows the compiler and build system to enforce dependency boundaries.

We will use the following structure:

ProjectResponsibilityProject type
Shop.WebWeb interface, pages, and user interactionASP.NET Core MVC
Shop.ApiREST API endpoints and HTTP responsesASP.NET Core Web API
Shop.BusinessBusiness rules, services, and application flowClass Library
Shop.DataAccessDatabase connection, repositories, and data accessClass Library
Shop.EntityEntities, DTOs, and core data modelsClass Library
Shop.SharedShared result models, constants, and reusable contractsClass Library

These names are not mandatory, but using the same root name makes the solution easier to understand. Larger systems may later include test projects, infrastructure projects, background workers, or shared packages.

1. Creating the solution directory

Open a terminal and create the directory that will contain the solution:

mkdir ShopSolution
cd ShopSolution
dotnet new sln --name ShopSolution

The dotnet new sln command creates the solution file. Depending on the SDK version, the file may use the .sln or .slnx extension. This difference does not change the project architecture.

Keeping production projects under a src directory also makes it easier to add tests under a separate tests directory later:

mkdir src

2. Creating the projects

Now create two executable ASP.NET Core projects and four class libraries:

dotnet new mvc --name Shop.Web --output src/Shop.Web
dotnet new webapi --name Shop.Api --output src/Shop.Api --use-controllers
dotnet new classlib --name Shop.Business --output src/Shop.Business
dotnet new classlib --name Shop.DataAccess --output src/Shop.DataAccess
dotnet new classlib --name Shop.Entity --output src/Shop.Entity
dotnet new classlib --name Shop.Shared --output src/Shop.Shared

These commands generate project files, default C# files, and the required startup configuration. The API command explicitly selects the controller-based model. In later lessons, we will create REST endpoints inside controller classes.

Add the generated projects to the solution:

dotnet sln ShopSolution.slnx add src/Shop.Web/Shop.Web.csproj
dotnet sln ShopSolution.slnx add src/Shop.Api/Shop.Api.csproj
dotnet sln ShopSolution.slnx add src/Shop.Business/Shop.Business.csproj
dotnet sln ShopSolution.slnx add src/Shop.DataAccess/Shop.DataAccess.csproj
dotnet sln ShopSolution.slnx add src/Shop.Entity/Shop.Entity.csproj
dotnet sln ShopSolution.slnx add src/Shop.Shared/Shop.Shared.csproj

If your SDK created a .sln file, use ShopSolution.sln in these commands instead. To list the projects currently registered in the solution, run:

dotnet sln list

3. Designing the dependency direction

One of the most important decisions in layered architecture is defining which projects may reference one another. A project should not reference every other project. Uncontrolled references create circular dependencies and make responsibilities difficult to understand.

For this course, use the following dependency direction:

Shop.Web        -> Shop.Api
Shop.Api        -> Shop.Business
Shop.Business   -> Shop.DataAccess
Shop.Business   -> Shop.Entity
Shop.DataAccess -> Shop.Entity
Shop.Api        -> Shop.Entity
Shop.Web        -> Shop.Shared
Shop.Api        -> Shop.Shared
Shop.Business   -> Shop.Shared

An arrow means that the project on the left depends on the project on the right. The Entity and Shared projects contain common contracts, so they should remain lightweight and avoid unnecessary dependencies. Data Access must not know about the Web or API projects. Similarly, Business should not depend directly on HTTP, Razor, or SQL Server implementation details.

Add the project references from the command line:

dotnet reference add src/Shop.Api/Shop.Api.csproj --project src/Shop.Web/Shop.Web.csproj
dotnet reference add src/Shop.Business/Shop.Business.csproj --project src/Shop.Api/Shop.Api.csproj
dotnet reference add src/Shop.Entity/Shop.Entity.csproj --project src/Shop.Api/Shop.Api.csproj
dotnet reference add src/Shop.Shared/Shop.Shared.csproj --project src/Shop.Api/Shop.Api.csproj
dotnet reference add src/Shop.DataAccess/Shop.DataAccess.csproj --project src/Shop.Business/Shop.Business.csproj
dotnet reference add src/Shop.Entity/Shop.Entity.csproj --project src/Shop.Business/Shop.Business.csproj
dotnet reference add src/Shop.Shared/Shop.Shared.csproj --project src/Shop.Business/Shop.Business.csproj
dotnet reference add src/Shop.Entity/Shop.Entity.csproj --project src/Shop.DataAccess/Shop.DataAccess.csproj
dotnet reference add src/Shop.Shared/Shop.Shared.csproj --project src/Shop.Web/Shop.Web.csproj

With older SDK versions, you may use the equivalent dotnet add reference syntax. In Visual Studio, right-click the Dependencies node of the target project, select Add Project Reference, and choose the required projects.

4. Inspecting project files

When a project reference is added, a ProjectReference entry is written to the target .csproj file. For example, Shop.Api.csproj should contain a section similar to this:

<ItemGroup>
  <ProjectReference Include="..\Shop.Business\Shop.Business.csproj" />
  <ProjectReference Include="..\Shop.Entity\Shop.Entity.csproj" />
  <ProjectReference Include="..\Shop.Shared\Shop.Shared.csproj" />
</ItemGroup>

Project-to-project references allow the build system to understand dependencies within the solution. When a referenced project changes, it can be rebuilt as part of the dependency graph. Manually referencing a compiled DLL can cause an outdated assembly to be used or create conflicts between different versions.

5. Defining responsibility boundaries

Shop.Entity

The Entity project contains classes that represent the business domain. A Product class may contain an identifier, name, and price. It should not contain SQL connection code, HTTP request logic, or user-interface code.

namespace Shop.Entity.Products;

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
}

Shop.Shared

The Shared project is intended for general structures used across multiple layers. For example, a standard service result or pagination model can be defined here. Do not turn this project into an unstructured dumping ground for unrelated code.

namespace Shop.Shared.Results;

public record ServiceResult<T>(bool Success, T? Data, string? Message);

Shop.DataAccess

This project will later contain the SQL Server connection, Entity Framework Core, the DbContext, and repository implementations. For now, create folders that express the future organization:

mkdir src/Shop.DataAccess/Contexts
mkdir src/Shop.DataAccess/Repositories

Shop.Business

The Business project will manage rules such as product creation, price validation, and handling a missing product. It should preferably work through interfaces rather than knowing the details of a specific database provider.

Shop.Api

The API project is the HTTP boundary of the application. Controllers receive requests, call Business services, and return suitable HTTP results. Writing SQL queries directly inside a controller bypasses the intended layer separation.

Shop.Web

The Web project contains the user-facing screens. It will communicate with the API over HTTP and prepare data for presentation. Avoiding direct references from Web to Business or Data Access preserves the real communication path between the web client and the API.

6. Building and validating the solution

From the solution root, restore dependencies and build every project:

dotnet restore
dotnet build

If the build succeeds, the initial project relationships are valid. If one project cannot find another, first verify that the reference was added to the correct target project and that the relative path and project name are correct.

You can also run the Web and API projects in separate terminals to verify the generated applications:

dotnet run --project src/Shop.Api
dotnet run --project src/Shop.Web

At this point, we have not created a real endpoint or database connection. However, the solution now has two application entry points and four supporting class libraries.

Common mistakes

  • Referencing every project from every other project: This may appear convenient at first, but it creates circular dependencies and weakens architectural boundaries.
  • Connecting the API directly to Data Access: This allows the API to bypass business rules. The API should use the Business layer.
  • Accessing the database from the Web project: This creates security, testing, and deployment problems. The Web project should consume the API.
  • Putting SQL or ASP.NET code in Entity: Core models become coupled to implementation details.
  • Referencing a DLL manually: Prefer a project reference for projects in the same solution.
  • Using inconsistent names: Clear names such as Shop.Business and Shop.DataAccess improve team communication.
  • Forgetting to add projects to the solution: A project may exist on disk but still be excluded from solution-level restore and build operations.

Short practice exercise

Complete the following exercise:

  1. Create an xUnit test project named Shop.Tests under a tests directory.
  2. Add the test project to the solution.
  3. Make the test project reference only Shop.Business, Shop.Entity, and, if necessary, Shop.Shared.
  4. Explain whether the test project should reference Shop.Web or Shop.DataAccess, and justify your decision.
  5. Run dotnet build and inspect the generated project references in the relevant .csproj files.

Conclusion

In this lesson, we created a solution and separated Web, API, Business, Data Access, Entity, and Shared projects. We then designed the dependency direction, added project references, and defined the responsibility of each project.

The basic application skeleton is now ready. In the next lessons, we will expand the models in the Entity project, define contracts between Business and Data Access, and prepare the solution for SQL Server integration. A well-designed reference structure makes future features easier to add, easier to test, and safer to maintain.

Sources