技术开发 频道

Castle IOC容器内幕故事(下)


【IT168技术文档】

  摘要:上一篇文章我们了解了Castle IOC中注册组件的流程,为了更好的使用Castle IOC,本篇文章我们介绍一下Castle IOC中的几个重要的角色,开始Castle IOC容器内幕故事下角色介绍。



  主要内容

  1.ComponentModelBuilder 和 Contributors

  2.Contributors分析

  3.Handles分析

  4.ComponentActivator分析



 一.ComponentModelBuilder 和 Contributors

  在前一篇中介绍组件的注册流程时说到了,创建ComponentModel的过程就是调用contributor来对组件进行处理的过程。Contributor就是我们这个内幕故事的第一个主角,在DefaultComponentModelBuilder一共注册了八个Contributor,每一个Contributor都专门负责处理某一方面的事情。如下代码所示:
protected virtual void InitializeContributors() { AddContributor( new ConfigurationModelInspector() ); AddContributor( new LifestyleModelInspector() ); AddContributor( new ConstructorDependenciesModelInspector() ); AddContributor( new PropertiesDependenciesModelInspector() ); AddContributor( new MethodMetaInspector() ); AddContributor( new LifecycleModelInspector() ); AddContributor( new ConfigurationParametersInspector() ); AddContributor( new InterceptorInspector() ); }
  1.ConfigurationModelInspector:用来处理配置,它使用ConfigurationStore在Kernel中注册来保持一个组件的连接。

  2.LifestyleModelInspector:生命周期处理方式,主要有Singleton、Thread、Transient、Pooled、Custom这些都可以在配置文件中指定,后续文章会讲到。

  3.ConstructorDependenciesModelInspector:处理构造函数依赖,收集所有的Public构造函数,并它们交给ComponentModel的Constructors集合。

  4.PropertiesDependenciesModelInspector:处理属性依赖,收集所有的可写的属性,Kernel也许不会在组件注册时就设置所有的属性,也有可能在请求获取组件时来设置。

  5.MethodMetaInspector:检测组件配置中的Method节点,每一个节点都将添加到ComponentModel的Method集合中。

  6.LifecycleModelInspector:处理组件生命周期,即在组件装载,初始化,销毁所出发的行为,分别对应三个接口:IInitializable,ISupportInitialize,IDisposable,如果组件实现了这些接口,容器会自动在不同的生命周期调用它们。

  7.ConfigurationParametersInspector:处理配置文件中的parameters元素内容,每一个parameters都将创建一个ParameterModel,并添加到ComponentModel的Parameters集合中。

  8.InterceptorInspector:处理InterceptorAttribute或者配置文件中的interceptors元素的信息。

  在有些情况下,我们可能并不需要这么多的Contributors,或者说我们想添加自定义的Contributors,可以用ComponentModelBuilder的如下两个方法来实现对Contributors的管理:
public void AddContributor(IContributeComponentModelConstruction contributor) { contributors.Add(contributor); } public void RemoveContributor(IContributeComponentModelConstruction contributor) { contributors.Remove(contributor); }
0
相关文章