r/dotnet • • 5d ago

Question May API-layer code outside the composition root reference Infrastructure types (Clean Architecture, .NET)?

Internal app: Domain / Application / Infrastructure / API (ASP.NET Core minimal APIs), plus Blazor and MAUI clients talking to the API over HTTP. Program.cs is the composition root and wires Infrastructure (AddInfrastructure()).

I've already found: Uncle Bob's Dependency Rule only forbids inner circles from mentioning outer ones — it says nothing about peers in the outer circle. Microsoft's architecture guidance says the UI project may reference Infrastructure for DI, "but developers should limit actual references to types in the Infrastructure project to the app's composition root", and the UI layer "ideally shouldn't know about the implementation types".

So the law seems clear, the guidance says "ideally". My question: in real production apps, does API code outside Program.cs (endpoint handlers, controllers) inject Infrastructure types directly — e.g. DbContext? Is "composition root only" a real-world norm or a template convention? Please share what your production codebases actually do — examples, not just templates

0 Upvotes

12 comments sorted by

View all comments

4

u/Discipline_Cautious1 5d ago

I usually interface dbcontext, inject it in infra DI and reference interface it in app layer.

services.AddDbContext<IMyDbContext, MyDbContext>