📖 Business
Composition Root Pattern
The Composition Root is the single location in an application — as close to the entry point as possible — where the entire object graph is constructed and wired together. Rather than scattering `new` calls and dependency resolution throughout controllers, services, and repositories, the Composition Root centralizes all object creation into one place. This pattern ensures that no class in your codebase is responsible for creating its own dependencies, which is the foundation of truly loosely coupled code. The Composition Root is not a class or a library — it is a concept: the place where you compose your application's object graph before handing control to the framework.
2
Minutes
2
Concepts
+45
XP
1
How It Works
  1. Single entry point composition — Every application type has a natural Composition Root location. For ASP.NET Core it's Startup.ConfigureServices(), for console apps it's Main(), for WPF it's App.OnStartup(). All object graph construction happens here and nowhere else.
  1. The Three Calls pattern — A well-structured Composition Root follows a predictable sequence:
  • Register — declare which abstractions map to which implementations
  • Resolve — build the root object graph (typically one call)
  • Release — dispose of the graph when the application shuts down
  1. No dependency resolution outside the root — Classes throughout the application receive dependencies through their constructors. They never call new on service classes, never use a Service Locator, and never reference the DI container. The container (if used) is referenced only in the Composition Root.
  1. One per application, not per library — Class libraries should never have a Composition Root. Only the executable — the thing that starts — should compose the graph. Libraries expose abstractions and implementations; the host application decides how to wire them.
  1. Object graph depth vs. width — A typical Composition Root constructs a deep object graph: a controller depends on a service, which depends on a repository, which depends on a database context. The Composition Root sees the full depth and wires every level in one place, making the architecture visible at a glance.