skip to content

Solution Layout & Layers

How ASP.NET Core solutions are split into projects and features: layered, clean and vertical-slice layouts, and modules wired into one host. Lead and senior interviews probe these trade-offs.

part ofASP.NET Coreread it →
on this pageshow

explore

questions

11

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?

level: juniorimportance: must knowfreq 62%

answer

  1. the compiler only sees referenced projects
  2. Domain references nothing
  3. Application owns the interfaces
  4. Infrastructure holds the DbContext
  5. Program.cs is the composition root

basics

~20 s

Domain 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 s

Each 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
xml
<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

for a junior

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.

for a middle

Explain why projects rather than folders: the compiler enforces references. Say why Web references Infrastructure (composition root) and where migrations and entity configuration live.

for a senior

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.

for a principal

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.

open as a page

In an ASP.NET Core modular monolith, what do a module's AddX and MapX extension methods look like, and why should Program.cs call only those?

level: middleimportance: must knowfreq 45%

basics

~20 s

AddX is a public static extension on IServiceCollection that registers the module's internal services, options and DbContext and returns the collection; MapX extends IEndpointRouteBuilder and maps its endpoints. Program.cs calls only these, so module internals stay hidden and movable.

open as a page

In an ASP.NET Core vertical-slice layout, what does one feature folder contain, and what makes a Minimal API endpoint over its handler thin?

level: middleimportance: must knowfreq 50%

basics

~20 s

A feature folder holds everything for one use case: the request and response types, the handler with its data access, validation and the endpoint mapping. A thin endpoint only binds input, calls the handler and turns its outcome into TypedResults.

open as a page

In an ASP.NET Core modular monolith on EF Core 10, why give each module its own DbContext and schema, and how do modules then read each other's data?

level: seniorimportance: must knowfreq 40%

basics

~20 s

A DbContext per module, with HasDefaultSchema and its own migrations history table, keeps each module's tables and migrations private and extractable. Other modules never query those tables; they call the owning module's public contract or keep their own copy updated by events.

open as a page

In an ASP.NET Core solution, how do ProjectReference items enforce clean-architecture dependency direction, and where can that enforcement still leak?

level: middleimportance: should knowfreq 38%

basics

~20 s

A project can only compile against projects it references, so inward-only ProjectReference items stop Domain from naming a DbContext, and a reverse reference fails restore with NU1108. It leaks through transitive references, public Infrastructure types reachable from Web, and package references.

open as a page

In an ASP.NET Core modular monolith, how would Admissions publish an ApplicantAdmitted event that Fees handles in-process, and what can go wrong?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Admissions defines ApplicantAdmitted in its Contracts project and publishes it through a dispatcher that resolves registered handlers from DI; Fees registers one. Dispatch runs inside the request, so a failing handler hits the caller and the modules commit separately.

open as a page

In an ASP.NET Core modular monolith, how do internal types and InternalsVisibleTo keep a module's implementation private while its tests can still reach it?

level: seniorimportance: should knowfreq 35%

basics

~20 s

Mark everything in a module internal except its AddX and MapX methods and a small Contracts project of interfaces, DTOs and events. Other modules reference only Contracts. An InternalsVisibleTo item opens the internals to the module's own test project, nothing else.

open as a page

In an ASP.NET Core app on EF Core 10, when does wrapping the DbContext in a repository add nothing, and when does it earn its place?

level: seniorimportance: should knowfreq 55%

basics

~20 s

A generic IRepository<T> over EF Core adds nothing when it forwards to DbSet<T>, returns IQueryable or calls SaveChanges itself. It earns its place when it hides specific, named queries for stubbing in tests or guards how an aggregate is loaded and saved.

open as a page

When would you extract the Timetabling module of a university's ASP.NET Core modular monolith into its own service, and what changes in its registration, data and cross-module calls?

level: principalimportance: should knowfreq 27%

basics

~20 s

Extract only for a concrete reason: scaling, release cadence, failure isolation or ownership. Then its AddX and MapX calls move into a new host, its schema becomes its own database, and callers keep its contract interface, now backed by a typed HttpClient.

open as a page

A veterinary-practice ASP.NET Core back end is growing from four developers to three teams; would you keep clean-architecture layer projects, move to vertical slices, or combine them?

level: principalimportance: should knowfreq 28%

basics

~20 s

Combine them in most cases: keep a Domain project the compiler protects, and organise Application and Web by feature so each team's change lands in one folder. Layer-only projects scatter a typical feature across four projects; slices alone lose compiler-enforced direction.

open as a page

In an ASP.NET Core modular monolith, how can each module's Minimal API endpoints be discovered by assembly scanning, and what does scanning cost?

level: middleimportance: nice to knowfreq 22%

basics

~20 s

Each endpoint class implements a small IEndpoint interface with a MapEndpoint(IEndpointRouteBuilder) method; the module's AddX scans its assembly for those types and registers them, and MapX resolves them and calls MapEndpoint. Scanning trades explicit routes for reflection, trimming warnings and harder navigation.

open as a page