[Composite UI][OB之BuilderStrategy扩展(一)]CAB之BuilderStrategy概览
********************************************************************
* 版权声明
*
* 本文以Creative Commons的发布,请严格遵循该授权协议。
* 本文首发于博客园, 此声明为本文章中不可或缺的一部分。
* 作者网名: 浪子
* 作者EMAIL:dayichen (at)163.com
* 作者BLOG: Http://Www.Cnblogs.Com/Walkingboy
*
********************************************************************
[CAB][OB之BuilderStrategy扩展(一)]CAB之BuilderStrategy概览
-Written by 浪子@cnblogs.com (06-09-21)
摘要:
有看过CAB的人一定非常赞叹于CAB中对Attribute的灵活应用程度,它使得开发者只需要轻轻松松的贴上相关的Atrribute就可以从CAB中获得想要的XX。而如此便捷的开发方法,背后是以IoC/DI思想为指导,依托OB(关于OB的相关知识请参考:http://www.cnblogs.com/walkingboy/category/56479.html)来实现的。本系列文章试图从CAB如何应用OB的角度来深入了解CAB到底是如何产生这种灵活的开发机制的。
CAB到底扩展了哪些BuilderStrategy,并且应用了哪些BuilderStrategy,我们从public abstract class CabApplication<TWorkItem>where TWorkItem : WorkItem, new()中可以看到,CAB到底应用了多少个策略,以及他们是如何协同工作的。private Builder CreateBuilder()
{
Builder builder = new Builder();
builder.Strategies.AddNew<EventBrokerStrategy>(BuilderStage.Initialization);
builder.Strategies.AddNew<CommandStrategy>(BuilderStage.Initialization);
builder.Strategies.Add(new RootWorkItemInitializationStrategy(this.OnRootWorkItemInitialized), BuilderStage.Initialization);
builder.Strategies.AddNew<ObjectBuiltNotificationStrategy>(BuilderStage.PostInitialization);
builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
builder.Policies.SetDefault<IBuilderTracePolicy>(new BuilderTraceSourcePolicy(new TraceSource("Microsoft.Practices.ObjectBuilder")));
builder.Policies.SetDefault<ObjectBuiltNotificationPolicy>(new ObjectBuiltNotificationPolicy());
return builder;
}
可见CAB对于PreCreation,Creation并没有进行任何的扩展,关键在于对象创建后的Initialization和PostInitialization阶段。
在Initialization扩展了
- EventBrokerStrategy:用来处理事件的发布与订阅
- CommandStrategy:用来处理Command与事件的关联
- RootWorkItemInitializationStrategy:用来处理RootWorkItem的其他初始化工作
在PostInitialization则扩展了
- ObjectBuiltNotificationStrategy:对象创建通知服务
虽然有4个扩展策略,但是CAB默认只启用了SingletonPolicy,BuilderTraceSourcePolicy,ObjectBuiltNotificationPolicy。哪是这些策略在哪里启用呢?搜索一下整个源码,发现只有WorkItem里面有设置Policy的地方
protected internal void BuildUp() { // We use Guid.NewGuid() to generate a dummy ID, so that the WorkItem buildup sequence can // run (the WorkItem is already located with the null ID, which marks it as a service, so // the SingletonStrategy would short circuit and not do the build-up). Type type = GetType(); string temporaryID = Guid.NewGuid().ToString(); PropertySetterPolicy propPolicy = new PropertySetterPolicy(); propPolicy.Properties.Add("Parent", new PropertySetterInfo("Parent", new ValueParameter(typeof(WorkItem), null))); PolicyList policies = new PolicyList(); policies.Set<ISingletonPolicy>(new SingletonPolicy(false), type, temporaryID); policies.Set<IPropertySetterPolicy>(propPolicy, type, temporaryID); builder.BuildUp(locator, type, temporaryID, this, policies); }是不是有点纳闷?其实我走入了一个误区,在我的OB相关文章中下过这样一个定论:OB的BuilderStrategy如何起作用,都是通过相关的BuilderPolicy 来决定。而CAB中的BuilderStrategy却没有通过BuilderPolicy来约束BuilderStrategy的应用。
那CAB是不是就只有这些BuilderStrategy呢?
这些策略当然不够用的。这些只是核心的BuilderStrategy,或者叫"无关UI的BuilderStrategy"。
相反,CAB中还有些与UI有关的BuilderStrategy,看一下WinForm应用程序的基类CabApplication
public abstract class CabApplication<TWorkItem>where TWorkItem : WorkItem, new()中的/// <summary> /// May be overridden in a derived class to add strategies to the <see cref="Builder"/>. /// </summary> protected virtual void AddBuilderStrategies(Builder builder){}这个方法就是为子类提供新增自定义BuilderStrategy的一种途径。比如子类WindowsFormsApplication中的
/// <summary> /// Adds Windows Forms specific strategies to the builder. /// </summary> protected override void AddBuilderStrategies(Builder builder) { builder.Strategies.AddNew<WinFormServiceStrategy>(BuilderStage.Initialization); builder.Strategies.AddNew<ControlActivationStrategy>(BuilderStage.Initialization); builder.Strategies.AddNew<ControlSmartPartStrategy>(BuilderStage.Initialization); }
可见从CabApplication继承出来的子类,还存在继续扩展OB创建策略的可能性。在WindowsFormsApplication中就扩展了适用于WinForm的三个创建策略。假设我们新实现了一个WebFormsApplication,很有可能就需要创建跟Web UI比较密切相关的BuilderStrategy。
小结:可见CAB中的BuilderStrategy可分为两大类:无关UI和UI相关。后续我按自己的研读步骤,逐一剖析每一个BuilderStrategy的内部机制以及它在CAB中所起的作用。
注:-------------------------------------------------
- CAB:Composite UI Application Block 简称, 站点:patterns & practices- Composite UI Application Block
- OB:ObjectBuilder简称,站点:patterns & practices- Object Builder