In an ASP.NET Core clean-architecture solution split into Web, Application, Domain and Infrastructure projects, what belongs in each project and which project references which?
answer
- the compiler only sees referenced projects
- Domain references nothing
- Application owns the interfaces
- Infrastructure holds the DbContext
- Program.cs is the composition root
basics
~20 sDomain holds entities and rules and references nothing. Application holds use cases and the interfaces they need, referencing Domain. Infrastructure implements those interfaces with EF Core and references Application. Web hosts Program.cs and the endpoints, referencing Application and Infrastructure.
solid answer
~40 sEach ring becomes its own class library because a project is what the C# compiler enforces: code can only use types from its own project or a project it references. `Domain` holds entities, value objects and business rules and references nothing, not even EF Core. `Application` holds the use-case handlers, their request and response types, and the interfaces they need (a reminder sender, a data-access abstraction); it references `Domain`. `Infrastructure` holds the `DbContext`, the `IEntityTypeConfiguration<T>` classes, migrations and the classes that implement Application's interfaces; it references `Application`. `Web` is the `dotnet new webapi` project: `Program.cs`, endpoints or controllers, middleware and `appsettings.json`. It references `Application` for the use cases and `Infrastructure` only so `Program.cs` can register the concrete implementations, which makes it the composition root.
code
xml · 17 lines<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- inward only: Infrastructure implements Application's interfaces -->
<ProjectReference Include="..\Vet.Application\Vet.Application.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
</ItemGroup>
</Project>go deeper
Name the four projects, one sentence on what each holds, and draw the arrows: Domain to nothing, Application to Domain, Infrastructure to Application, Web to both.
Explain why projects rather than folders: the compiler enforces references. Say why Web references Infrastructure (composition root) and where migrations and entity configuration live.
Show you know where the rule leaks: the Web-to-Infrastructure reference, transitive references, and EF Core packages creeping into Domain, and how you would guard against each.
Weigh four projects against one project with feature folders for the team and system at hand, and say what evidence would make you split or merge.
## Why one web app becomes four projects A clean-architecture layout in .NET maps its rings onto separate **projects**, each compiling to its own assembly. The reason is enforcement: the C# compiler only lets code name a type that lives in the same project or in a project it **references** through a `ProjectReference` item in its `.csproj`. A folder is a convention; a project boundary is a compile error. Take a veterinary-practice back end with four projects: `Vet.Domain`, `Vet.Application`, `Vet.Infrastructure` and `Vet.Web`. ## What lives in each project | Project | Created from | Holds | References | |---|---|---|---| | `Vet.Domain` | `classlib` | `Patient`, `Owner`, `Appointment` entities, value objects, rules such as "no double-booked vet" | nothing | | `Vet.Application` | `classlib` | use-case handlers (`BookAppointmentHandler`), request and response records, interfaces such as `IReminderSender` | `Vet.Domain` | | `Vet.Infrastructure` | `classlib` | `VetDbContext`, `IEntityTypeConfiguration<T>` classes, migrations, `SmsReminderSender` | `Vet.Application` | | `Vet.Web` | `webapi` | `Program.cs`, Minimal API endpoints or controllers, middleware, `appsettings.json` | `Vet.Application`, `Vet.Infrastructure` | Key points about the contents: - **Domain** has no package reference to EF Core or ASP.NET Core. Mapping lives in Infrastructure as `IEntityTypeConfiguration<T>` classes picked up by `modelBuilder.ApplyConfigurationsFromAssembly(...)`, so entities stay plain classes. - **Application** describes what the system does. When it needs something only the outside world can provide (sending an SMS, reading the clock, persisting), it declares an interface and lets Infrastructure implement it. - **Infrastructure** is where EF Core, the database provider package and HTTP clients for outside services live. - **Web** translates HTTP into calls on Application and results back into HTTP: route, bind, call the handler, return `TypedResults`. ## The reference graph 1. `Vet.Domain` references nothing. 2. `Vet.Application` references `Vet.Domain`. 3. `Vet.Infrastructure` references `Vet.Application` (and so sees Domain too). 4. `Vet.Web` references `Vet.Application` and `Vet.Infrastructure`. Every arrow points inward except one: Web to Infrastructure. That reference exists so `Program.cs` can act as the **composition root**, the single place where `builder.Services` is told which concrete class backs each interface (`AddDbContext<VetDbContext>`, `AddScoped<IReminderSender, SmsReminderSender>`). Endpoints should still depend only on Application types; the reference is for wiring, not for injecting `VetDbContext` into an endpoint. If someone adds the reverse arrow, say Application referencing Infrastructure to reach the `DbContext` while Infrastructure already references Application, the graph has a cycle and `dotnet restore` fails with **NU1108** ("Cycle detected"). ## Where mapping and configuration go Two pieces of plumbing regularly end up in the wrong project: - **Entity mapping.** Column names, keys, owned types and indexes are written as `IEntityTypeConfiguration<Appointment>` classes in Infrastructure. `VetDbContext.OnModelCreating` calls `modelBuilder.ApplyConfigurationsFromAssembly(typeof(VetDbContext).Assembly)` once, and every configuration class in that assembly is applied. The `Appointment` class in Domain never learns that a database exists. - **Settings.** `appsettings.json`, user secrets and environment variables are read by the host that `Vet.Web` builds. Infrastructure receives what it needs as options or as values passed at registration time, so it never reads a configuration file on its own and never references the Web project. Keeping these two in Infrastructure is what lets Domain and Application compile, and be unit-tested, without a database provider or a web host anywhere in their reference graph. ## Creating it with the .NET 10 SDK ```bash dotnet new sln -n Vet dotnet new classlib -o src/Vet.Domain dotnet new classlib -o src/Vet.Application dotnet new classlib -o src/Vet.Infrastructure dotnet new webapi -o src/Vet.Web dotnet sln add src/Vet.Domain src/Vet.Application src/Vet.Infrastructure src/Vet.Web dotnet reference add src/Vet.Domain --project src/Vet.Application dotnet reference add src/Vet.Application --project src/Vet.Infrastructure dotnet reference add src/Vet.Application src/Vet.Infrastructure --project src/Vet.Web ``` Two .NET 10 SDK details show up here. `dotnet new sln` now writes an `.slnx` solution file instead of the old `.sln` format. And the SDK added noun-first command forms: `dotnet reference add` is the new spelling of `dotnet add reference`, which still works. ## Mistakes interviewers listen for - Putting `[Index]` or `[Owned]` attributes, or a `DbSet<T>`, inside Domain, which drags an EF Core package into the innermost project. - Letting Application reference Infrastructure "just to get the `DbContext`", which either creates a cycle or turns the layering upside down. - Business rules in endpoints or controllers, which leaves Application as a pass-through. - Believing Web must not reference Infrastructure at all. Something has to register the implementations, and in a single-host ASP.NET Core app that is usually `Program.cs`. - Treating four projects as mandatory. For a small service, one project with feature folders (vertical slices) is a legitimate alternative.
- Where do EF Core migrations live in this layout, and what does that mean for the dotnet ef command?Migrations live with the `DbContext` in Infrastructure, but the configuration and connection string come from the Web project that hosts the app. So `dotnet ef migrations add` is run with `--project` pointing at Infrastructure and `--startup-project` pointing at Web, which the tool builds and uses to create the context.
- Why does Application declare interfaces such as IReminderSender instead of calling an SMS client directly?Application references only Domain, so it cannot see an SMS client class that lives in Infrastructure. Declaring the interface in Application keeps the reference pointing inward: Infrastructure implements it, and `Program.cs` registers the implementation, so handlers receive it by constructor injection without knowing which class is behind it.
A hospital wing where the operating theatre (Domain) has no door to the loading dock; supplies reach it only through the corridor (Application), and only the front desk (Web) holds the master key list saying which contractor services which room.
- Domain should reference EF Core so entities can carry mapping attributes.
- Application references Infrastructure because handlers need the concrete DbContext.
- Web must never reference Infrastructure in a clean-architecture solution.
- The four projects are just folders; references between them do not matter.
- Business rules can live in the endpoints because Web is the outermost project.
Assumes the .NET 10 SDK; dotnet new sln writes .slnx and dotnet reference add is the noun-first form new in 10.