Castle IOC容器实践之Startable Facility(九)

摘要:在Castle IOC容器实践之Startable Facility(一)中我们已经学会了如何去使用Startable Facility,本文将在此基础进一步对它的原理做一些分析。

 

主要内容

Startable Facility原理分析

……

 

Castle IOC容器实践之Startable Facility(一)中我们已经看到了如何去使用Startable Facility,本文将对它的原理做一些分析。先看一下接口IStartable,它的实现代码如下:

public   interface  IStartable
ExpandedBlockStart.gifContractedBlock.gif
{
    
void Start();

    
void Stop();
}


代码是相当的简单,只有两个方法,分别在组件创建的时候和销毁的时候执行,这就涉及到了组件的生命周期管理。在Windsor中,接口ILifecycleConcern提供特定的组件生命周期管理:

public   interface  ILifecycleConcern
ExpandedBlockStart.gifContractedBlock.gif
{
    
void Apply( ComponentModel model, object component );

}


现在我们要实现组件的自动创建和销毁,就需要实现接口ILifecycleConcern,在Startable Facility中分别用两个类来实现,第一个类StartConcern,它判断如果组件实现了接口IStartable,则直接调用它的Start()方法;如果组件是用特性startMethod,则获取并调用具有startMethod特性的方法:

public   class  StartConcern : ILifecycleConcern
ExpandedBlockStart.gifContractedBlock.gif
{
    
private static readonly StartConcern _instance = new StartConcern();

    
protected StartConcern()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public static StartConcern Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _instance; }
    }


    
public void Apply(ComponentModel model, object component)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (component is IStartable)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            (component 
as IStartable).Start();
        }

        
else if (model.Configuration != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            String startMethod 
= model.Configuration.Attributes["startMethod"];
 
            
if (startMethod != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{

                MethodInfo method 
= model.Implementation.GetMethod(startMethod);

                method.Invoke(component, 
null);
            }

        }

    }

}


第二个类是StopConcern,它判断如果组件实现了接口IStartable,则直接调用它的Stop()方法;如果组件是用特性stopMethod,则获取并调用具有stopMethod特性的方法:

public   class  StopConcern : ILifecycleConcern
ExpandedBlockStart.gifContractedBlock.gif
{
    
private static readonly StopConcern _instance = new StopConcern();

    
protected StopConcern()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

    }


    
public static StopConcern Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get return _instance; }
    }


    
public void Apply(ComponentModel model, object component)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(component is IStartable)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            (component 
as IStartable).Stop();
        }

        
else if (model.Configuration != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            String stopMethod 
= model.Configuration.Attributes["stopMethod"];

            
if (stopMethod != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                MethodInfo method 
= model.Implementation.GetMethod(stopMethod);

                method.Invoke(component, 
null);
            }

        }

    }

}


好了,知道了Startable Facility如何管理组件的生命周期,我们就来看看真正的Startable Facility是如何实现的。每一个Facility都是满足这样的一个继承关系:

1 Facility继承关系图

其中的Abstract Facility提供了一些默认的实现,Facility可以直接实现IFacility,也可以继承于Abstract FacilityIFacility的实现如下:

public   interface  IFacility
ExpandedBlockStart.gifContractedBlock.gif
{
    
void Init(IKernel kernel, IConfiguration facilityConfig);


    
void Terminate();

}


那么到底如何让组件满足依赖性后就自动执行呢?注意到再Startable FacilityInit()注册了这样的两个事件:

protected   override   void  Init()
ExpandedBlockStart.gifContractedBlock.gif
{
    converter 
= (ITypeConverter) Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);


    Kernel.ComponentModelCreated 
+= 

        
new ComponentModelDelegate(OnComponentModelCreated);

    Kernel.ComponentRegistered 
+= 

        
new ComponentDataDelegate(OnComponentRegistered);

}

 

分别为OnComponentModelCreatedOnComponentRegistered。当我们注册一个组件时首先会出发OnComponentRegistered事件,在它里面判断组件是否满足依赖性,如果不满足,则添加到一个等待列表中,否则就直接启动,然后再对这个等待列表进行检测,看添加改组件后,列表中是否有组件满足了依赖性:

private   void  OnComponentRegistered(String key, IHandler handler)
ExpandedBlockStart.gifContractedBlock.gif
{
    
bool startable = (bool) handler.ComponentModel.ExtendedProperties["startable"];

    
if (startable)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (handler.CurrentState == HandlerState.WaitingDependency)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            waitList.Add( handler );
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Start( key );
        }

    }


    CheckWaitingList();
}


private   void  CheckWaitingList()
ExpandedBlockStart.gifContractedBlock.gif
{
    IHandler[] handlers 
= (IHandler[]) waitList.ToArray( typeof(IHandler) );

    IList validList 
= new ArrayList();

    
foreach(IHandler handler in handlers)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if (handler.CurrentState == HandlerState.Valid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            validList.Add(handler);

            waitList.Remove(handler);
        }

    }


    
foreach(IHandler handler in validList)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        Start( handler.ComponentModel.Name );
    }

}


刚才说到,如果满足了依赖性,则会请求创建这个组件:

private   void  Start(String key)
ExpandedBlockStart.gifContractedBlock.gif
{
    
object instance = Kernel[key];
}


这时就触发了OnComponentModelCreated事件,这时就该用到开始我们所讲的那两生命周期处理的类了:

private   void  OnComponentModelCreated(ComponentModel model)
ExpandedBlockStart.gifContractedBlock.gif
{
    
bool startable = 

        CheckIfComponentImplementsIStartable(model) 
|| HasStartableAttributeSet(model);

    model.ExtendedProperties[
"startable"= startable;

    
if (startable)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        model.LifecycleSteps.Add( 

            LifecycleStepType.Commission, StartConcern.Instance );

        model.LifecycleSteps.Add( 

            LifecycleStepType.Decommission, StopConcern.Instance );

    }

}


首先还是先判断组件是否实现了IStartable接口或这时候有特性startable,如果没有那也就不用自动启动了,否则就把StartConcernStopConcern分别注册为组件的生命周期开始行为和生命周期结束行为,(关于组件的生命周期的详细内容可以参考我前面写的Castle IOC容器组件生命周期管理)。此时组件进入生命周期开始,会调用StartConcernApply()方法,这时就触发组件的Start()方法,同样在组件销毁时调用StopConcernApply()方法,这时就会调用组件的Stop()方法。这样就完成整个了组件的自动执行与销毁的全过程。

转载于:https://www.cnblogs.com/Madream/articles/1680456.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值