In an ASP.NET Core solution, how do ProjectReference items enforce clean-architecture dependency direction, and where can that enforcement still leak?
answer
- compile-time visibility, not runtime
- cycle: restore error NU1108
- transitive references on by default
- DisableTransitiveProjectReferences or PrivateAssets
- packages bypass the project graph
basics
~20 sA 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.
solid answer
~50 s`ProjectReference` decides what the compiler can see: if `Vet.Domain` references nothing, no code there can name `VetDbContext`, and if someone points Application at Infrastructure while Infrastructure points at Application, `dotnet restore` reports a cycle as **NU1108** and the build stops. The enforcement has holes, though. SDK-style projects flow references **transitively**, so a project referencing Infrastructure also compiles against Application and Domain; `DisableTransitiveProjectReferences` or `PrivateAssets="all"` on a reference turns that off. Web references Infrastructure for wiring, so every **public** Infrastructure type, including the `DbContext`, is injectable into an endpoint unless you make those types `internal` behind a public registration method. And a **package** reference is invisible to the project graph: Domain adding an EF Core package, or Application adding `<FrameworkReference Include="Microsoft.AspNetCore.App" />` to accept `HttpContext`, breaks the layering with every arrow still pointing inward. Architecture tests in the test project catch what references cannot.
code
xml · 15 lines<!-- Vet.Web.csproj -->
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<!-- only projects listed below are visible to Web's code -->
<DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Vet.Application\Vet.Application.csproj" />
<ProjectReference Include="..\Vet.Infrastructure\Vet.Infrastructure.csproj" />
</ItemGroup>
</Project>go deeper
Remember that a ProjectReference is what lets one project use another's types, and that the arrows in a clean solution point inward toward Domain.
Explain compile-time visibility, the NU1108 cycle error, and transitive references being on by default, with the two switches that turn them off.
Show the leaks you have seen in real diffs: endpoints injecting the DbContext, EF Core or ASP.NET Core packages in inner projects, and the architecture test that guards them.
Decide how much enforcement a codebase deserves: compiler-only, internal types, or architecture tests in CI, weighed against team size and how often boundaries erode.
## What a ProjectReference actually enforces In an SDK-style `.csproj`, a `<ProjectReference Include="..\Vet.Application\Vet.Application.csproj" />` item tells MSBuild to build that project first and pass its assembly to the compiler. That is the whole mechanism: **compile-time visibility**. If a type's assembly is not referenced, any code naming that type fails with a compile error. A clean-architecture solution exploits this by making the arrows point inward only: 1. `Vet.Domain` references nothing. 2. `Vet.Application` references `Vet.Domain`. 3. `Vet.Infrastructure` references `Vet.Application`. 4. `Vet.Web` references `Vet.Application` and `Vet.Infrastructure`. ## The cycle guard The most common attempted shortcut is Application referencing Infrastructure so a handler can take `VetDbContext`. Because Infrastructure already references Application, that closes a loop. NuGet restore walks the project graph before compilation and reports **NU1108** ("Cycle detected"), and the build cannot proceed. The fix is the usual inversion: Application declares the abstraction, Infrastructure implements it. ## Where it leaks ### Transitive project references SDK-style projects treat project references as **transitive** by default: the SDK's `IncludeTransitiveProjectReferences` target adds every project your references depend on as an implicit reference. So a test project that references only `Vet.Infrastructure` can still use `Vet.Domain` types directly. Two switches change that: - `<DisableTransitiveProjectReferences>true</DisableTransitiveProjectReferences>` in the consuming project turns off the implicit references, so it must list every project it uses. - `PrivateAssets="all"` on a single `ProjectReference` keeps that reference from flowing on to projects that reference this one. ### The composition-root reference `Vet.Web` must reference `Vet.Infrastructure` so `Program.cs` can register implementations. The side effect is that **every public type** in Infrastructure, including `VetDbContext` and `SmsReminderSender`, is visible to endpoints. Nothing stops a Minimal API handler from taking `VetDbContext db` as a parameter. The usual guards are making implementation classes `internal` and exposing only the registration method, or an architecture test that fails when an endpoint type depends on an Infrastructure namespace. ### Package and framework references The project graph says nothing about NuGet packages or shared frameworks. These leaks keep every arrow pointing inward: - **Domain** adds `Microsoft.EntityFrameworkCore.Abstractions` to use `[Index]` or `[Owned]`, or the full EF Core package to expose a `DbSet<T>`. - **Application** adds `<FrameworkReference Include="Microsoft.AspNetCore.App" />` so a handler can accept `IFormFile` or `HttpContext`, coupling use cases to HTTP. - **Application** defines an `IVetDbContext` exposing `DbSet<Appointment>`, which needs the EF Core package. Some teams accept this trade deliberately; it should be a decision, not an accident. ## A leak as it appears in review A developer on the billing feature needs the unpaid invoices for an owner and writes the endpoint the quick way: `app.MapGet("/owners/{id}/unpaid", (int id, VetDbContext db) => db.Invoices.Where(...).ToListAsync())`. It compiles, because `Vet.Web` references `Vet.Infrastructure` and `VetDbContext` is public. Every arrow in the solution still points the right way, yet the use case now lives in the web project and Application never sees it. Two changes stop it recurring: 1. Declare `VetDbContext` and the other implementation classes `internal` in Infrastructure, and expose one public method that registers them. Web can still wire everything up but can no longer name the context. 2. Add an architecture test asserting that no type in the Web assembly depends on an Infrastructure namespace, so the next attempt fails in CI rather than in review. ## Guarding what references cannot | Leak | Reference graph catches it? | Guard | |---|---|---| | Application to Infrastructure | yes, NU1108 cycle | none needed | | Implicit transitive access | no | `DisableTransitiveProjectReferences`, `PrivateAssets="all"` | | Endpoint injecting `VetDbContext` | no | `internal` implementations, architecture test | | EF Core or ASP.NET Core package in an inner project | no | architecture test, code review of `.csproj` changes | An **architecture test** is an ordinary test in the test project that loads the layer assemblies and asserts on their dependencies. The plainest version uses reflection: `typeof(Appointment).Assembly.GetReferencedAssemblies()` must not contain `Microsoft.EntityFrameworkCore` or `Microsoft.AspNetCore.*`. Because it runs in CI with every other test, it turns a convention into a failing build. ## Why interviewers ask this The question separates candidates who drew the onion diagram from those who have kept one intact for a year. The arrows in the diagram are easy; knowing that transitive references, public implementation types and package references each silently erode it is what a reviewer of `.csproj` diffs actually needs.
- Why might an architecture test based on GetReferencedAssemblies miss a dependency someone added?The compiler only records an assembly reference when the code actually uses a type from it. A `PackageReference` added to Domain but not yet used leaves no trace in the compiled assembly, so the test passes until the first type is used. Reviewing `.csproj` changes, or checking the project file in the test, covers that gap.
- If Vet.Web sets DisableTransitiveProjectReferences to true and uses a Domain type directly, what happens?Compilation fails for that usage, because Domain is no longer an implicit reference of Web. You either add an explicit `ProjectReference` to Domain, which documents the dependency, or keep Web talking only to Application's request and response types.
- Project references are checked at run time, so a wrong reference shows up as a crash.
- If no ProjectReference points outward, Domain cannot depend on EF Core.
- A project only sees the projects listed in its own .csproj file.
- Adding the reverse reference just produces a warning the build ignores.
- Making Infrastructure classes public is required for Program.cs to register them.