How dependencies are resolved
June 10, 2015 ยท View on GitHub
Components in Windsor are seldom independent. After all, one of Windsor's main tasks is wiring and managing dependencies. When a component has some dependencies, Windsor goes through several steps in order to resolve them.
Dependency Resolver
Windsor uses dependency resolver (type implementing IDependencyResolver interface) to resolve your component's dependencies. Default dependency resolver (DefaultDependencyResolver class) looks in the following places in order to resolve the dependencies.
Creation Context
First of all dependency resolver asks CreationContext for the dependency. Creation context tries to resolve the dependency first by name, then by type using dependencies provided inline. That means, it comes from either of the following:
- Arguments passed to
Resolve:container.Resolve<IFoo>(new Arguments(new { inlineDependency = "its value" })); - Arguments passed via
DynamicParametersmethod of fluent registration API.
:information_source: Other sources: Notice that also Typed Factory Facility forwards the arguments passed to factory methods as inline parameters.
Handler
When no inline argument can satisfy the dependency the resolver asks handler if it can satisfy it. Handler tries to resolve the dependency first by name, then by type. The values come from ComponentModel's CustomDependencies collection, which usually means parameters passed to DependsOn method.
kernel.Register(Component.For<ClassWithArguments>()
.DependsOn(
Property.ForKey<string>().Eq("typed"),
Property.ForKey<int>().Eq(2)
)
);
Subresolvers
If previous places weren't able to resolve the dependency resolver will ask each of its sub resolvers (ISubDependencyResolver) if they can provide the dependency.
Kernel
When none of the above is able to resolve the dependency container will try to do it himself. Depending on the type of the dependency it will try to do the following:
- For parameter dependency it will inspect
ComponentModel'sParameterscollection for matching dependency. These include parameters passed from XML, or viaParametersmethod on fluent API. - For service override dependency, it will try to resolve component matching the key specified.
- For service dependency it will try to resolve any component matching the service specified.
If none of the above works, a DependencyResolverException will be thrown.