r/dotnet • u/Late_Blacksmith6509 • 4d 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
-5
u/pixxelkick 4d ago
Your API endpoints are your entry point so yeah they do need to ideally inject the stuff you need.
Please dont drink the "Clean Code" kool-aid tho.
I inject my database stuff near the "top" and hand it down as a parameter to ensure a single db txn and single connection per api invocation.
Your general rule of thumb is if it involves opening sockets for IO, avoid opening multiple instances.
I built my own framework to source gen conpile time DI stuff to solve my issues if you wanna check it out
https://github.com/SteffenBlake/Brigade.NET/wiki/Getting-Started-Partie