首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Castle IOC 實踐:Startable Facility(二)原理篇

     編輯:關於.NET

IStartable 介面與生命週期關注點


public interface IStartable
{
    void Start();
    void Stop();
}

Windsor 中 ILifecycleConcern 提供特定生命週期管理:


public interface ILifecycleConcern
{
    void Apply(ComponentModel model, object component);
}

兩大核心 Concern 實作

StartConcern:啟動階段


public class StartConcern : ILifecycleConcern
{
    private static readonly StartConcern _instance = new StartConcern();
    public static StartConcern Instance { get { return _instance; } }
    public void Apply(ComponentModel model, object component)
    {
        if (component is IStartable)
            (component as IStartable).Start();
        else if (model.Configuration != null)
        {
            string startMethod = model.Configuration.Attributes["startMethod"];
            if (startMethod != null)
                model.Implementation.GetMethod(startMethod).Invoke(component, null);
        }
    }
}

StopConcern:停止階段


public class StopConcern : ILifecycleConcern
{
    private static readonly StopConcern _instance = new StopConcern();
    public static StopConcern Instance { get { return _instance; } }
    public void Apply(ComponentModel model, object component)
    {
        if (component is IStartable)
            (component as IStartable).Stop();
        else if (model.Configuration != null)
        {
            string stopMethod = model.Configuration.Attributes["stopMethod"];
            if (stopMethod != null)
                model.Implementation.GetMethod(stopMethod).Invoke(component, null);
        }
    }
}

StartableFacility 核心邏輯

繼承 AbstractFacility,實作 IFacility:


public interface IFacility
{
    void Init(IKernel kernel, IConfiguration facilityConfig);
    void Terminate();
}

初始化註冊兩個關鍵事件


protected override void Init()
{
    converter = (ITypeConverter)Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
    Kernel.ComponentModelCreated += new ComponentModelDelegate(OnComponentModelCreated);
    Kernel.ComponentRegistered += new ComponentDataDelegate(OnComponentRegistered);
}

OnComponentRegistered:依賴滿足檢查與等待列表


private void OnComponentRegistered(string key, IHandler handler)
{
    bool startable = (bool)handler.ComponentModel.ExtendedProperties["startable"];
    if (startable)
    {
        if (handler.CurrentState == HandlerState.WaitingDependency)
            waitList.Add(handler);
        else
            Start(key);
    }
    CheckWaitingList();
}

private void CheckWaitingList()
{
    IHandler[] handlers = (IHandler[])waitList.ToArray(typeof(IHandler));
    IList validList = new ArrayList();
    foreach (IHandler handler in handlers)
        if (handler.CurrentState == HandlerState.Valid)
        {
            validList.Add(handler);
            waitList.Remove(handler);
        }
    foreach (IHandler handler in validList)
        Start(handler.ComponentModel.Name);
}

Start 方法:觸發建立實例


private void Start(string key)
{
    object instance = Kernel[key]; // 觸發 ComponentModelCreated
}

OnComponentModelCreated:註冊生命週期 Concern


private void OnComponentModelCreated(ComponentModel model)
{
    bool startable = CheckIfComponentImplementsIStartable(model) || HasStartableAttributeSet(model);
    model.ExtendedProperties["startable"] = startable;
    if (startable)
    {
        model.LifecycleSteps.Add(LifecycleStepType.Commission, StartConcern.Instance);
        model.LifecycleSteps.Add(LifecycleStepType.Decommission, StopConcern.Instance);
    }
}

流程總結:組件註冊 → 檢查依賴 → 不滿足加入等待列表 → 滿足則 Start → 觸發建立實例 → OnComponentModelCreated 註冊 StartConcern/StopConcern → 進入 Commission 階段執行 Start() → 容器釋放時執行 Stop()。完整實現組件依賴就緒自動啟動與銷毀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved