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.
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.
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
DynamicParameters
method of fluent registration API.
ℹ️ Other sources: Notice that also Typed Factory Facility forwards the arguments passed to factory methods as inline parameters.
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)
)
);
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.
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
'sParameters
collection for matching dependency. These include parameters passed from XML, or viaParameters
method 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.