How dependencies are resolved

总结:

DependencyResovler先后通过以下几个步骤解析依赖:

    • CreateContext解析
    • Handler解析
    • SubResolver解析
    • Kernel解析

 

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.

Windsor 使用 实现IdependencyResolver接口的类解析组件依赖,默认是DefaultDependencyResolver

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:

首先从CreationContext中解析依赖。CreationContext 先通过名字解析 在通过内联依赖项使用type解析,如下:

1.Arguments passed to Resolve: container.Resolve<IFoo>(new Arguments(new { inlineDependency = "its value" }));

2.Arguments passed via DynamicParameters method of fluent registration API.(这里DynamicParameters 待续

源码解析:

 

public virtual bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver,
                                       ComponentModel model,
                                       DependencyModel dependency)
        {
            return HasAdditionalArguments && (CanResolveByKey(dependency) || CanResolveByType(dependency));
        }

additionalArguments ?? CreationContext怎么初始化的,根据什么初始化的?

调用两个方法分别是,通过key获取,通过Type获取

 

Other sources: Notice that also Typed Factory Facility forwards (forward发送)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获取参数) 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)
)
);

Subresovlers

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.(如果以上都没有解析出依赖参数,接下来会访问所有实现ISubDependencyResolver接口的 resovler来解析依赖)

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's Parameters collection for matching dependency. These include parameters passed from XML, or via Parameters method on fluent API.
  • For service override dependency, it will try to resolve component matching the key specified.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.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.

posted @ 2019-08-07 03:07  vvf  阅读(189)  评论(0编辑  收藏  举报