How componets are created

How componets are created 组件怎么创建的

 

 

  • Locating Handler,确定HandlerContainer通过 naming subsystem检查组件是否注册了
            如果没有找到,容器将会尝试懒加载,如果加载失败,抛出ComponentNotFoundException
            如果组件注册了,容器会成功发现组件,容器会轮训处理器( poll its handler)并解析出组件对象
            IHandler: Contract for the IHandler, which manages an component state and coordinates its creation and destruction (dispatching to activators, lifestyle managers) IHandler用来控制组件状态,协          调组建的创建和销毁(传递给activators,控制生命方式)
  • What handler does ,Handler做了什么

  • What lifestyle manager does
            不同的lifestyle表现不同,会根据情况从缓存中获取或者通过componentactivator创建新实例
  • What component activitor does
            1.Instantiates the component by invoking its constructor 通过构造方法创建组件实例
            Select eligible constructor 选择合适的构造函数,Crate construcor's arguments 创建构造方法的参数,反射创建未初始化实例,用参数初始化对象
            选择构造方法策略:1.参数都能解析出来 2.参数最多 3.参数数量相同每次不确定选择 4.忽略Castle.Core.DoNotSelectAttribute标记的构造方法
          
      2. it then resolves component's property dependencies 解析组件的属性 
            
    3.When the component is fully created it invokes all commissino concern of the componet 组件被完全创建后执行所有commission concern(一些操作,可自定义 link
            4.Triggers ComponentCreated event on the kernel 执行注册的ComponentCreated事件
      5.把创建的实例返回给style manager
  • What handler ,release policy and container do

 

 总结

DefaultKernel LoadHandlerByType() => GetHandler(ServiceType) => NamingSubSystem.GetHandler(ServiceType)   默认得到DefaultHandler(DefaultHandler:AbstractHandler:IHandler:ISubDependencyResolver) DefaultHandler 使用handlerByServiceCache字典(这是一个Dic<Type,HandlerType>  )获取Handler 
DefaultHandler => LifestyleManager.Resolve() (DefaultLifestyleManager:AbstractLifestyleManager:ILifestyleManager)
AbstractLifestyleManager => ComponentActivator Create Instance (DefaultComponentActivator:AbstractComponentActivator:IComponentActivator)
DefaultComponetActivator 检查构造方法 解析最复杂的构造方法的参数(循环解析Component,是否所有的Dependency都可以解析)
反射构造未初始化的Instance,再使用以上解析出来的参数调用构造方法初始化反射构造的Instance

var instance = FormatterServices.GetUninitializedObject(implType);   constructor.Constructor.Invoke(instance, arguments);

LifeCycleComponentsReleasePolicy Track Instance (将实例放到一个 Dictionary<object, Burden>中 在OnRelease事件中添加事件,ControllerFactory调用Release时调用该事件后删除该事件)

总结流程处理类:
Container --- DefaultKernel --- NamingSubSystem --- Handler --- LiftStyleManager --- ComponentActivator (Create) --- LifeCycleComponentReleasePolicy (Track Instance)
构造方法选择策略:
1.所有的参数都能提供
2.优先选择参数多的构造方法
3.有多个参数最多的构造方法每次的选择不固定

构造方法参数选择策略 link

类关系:
WindsorContainer:IWindsorContainer

IKernelInternal : IKernel  // Extended contract of kernel, used internally.
DefaultKernel : MarshalByRefObject, IKernel, IKernelEvents, IKernelInternal

DefaultNamingSubSystem : AbstractSubSystem, INamingSubSystem
INamingSubSystem : ISubSystem
AbstractSubSystem : MarshalByRefObject, ISubSystem

DefaultHandler : AbstractHandler:IHandler: ISubDependencyResolver

AbstractLifestyleManager : ILifestyleManager
TransientLifestyleManager:AbstractLifestyleManager : ILifestyleManager (TransientLifestyleManager 是空实现,只有一个空的Dispose方法,所以AbstractLifestyleManager也是默认的)
SingletonLifestyleManager:AbstractLifestyleManager : ILifestyleManager (SingletonLifestyleManager中包含一个引用是Component实例的字段,可直接返回,不需要重新解析)
ScopedLifestyleManager : AbstractLifestyleManager ()
PoolableLifestyleManager : AbstractLifestyleManager (使用Stack<Burden>保存实例)


DefaultComponentActivator:AbstractComponentActivator : IComponentActivator 检查选择构造方法,初始化依赖参数(使用resolver获取Handler获取参数实例),反射构造实例

 

DefaultDependencyResolver : IDependencyResolver()

 

LifecycledComponentsReleasePolicy : IReleasePolicy

 

 

DefaultKernel 初始化

    1. RegisterSubSystems 注册子系统 :configuration store subsystem、 conversion manager、 diagnostics subsystem、 naming subsystem、 resource subsystem
posted @ 2019-08-09 22:31  vvf  阅读(212)  评论(0编辑  收藏  举报