2009-10-06 12 views
1

내가성 윈저,

public class ConfigurationFacility : AbstractFacility { 
    private readonly List<string> configuredComponents = new List<string>(); 

    protected override void Init() { 
     Kernel.ComponentRegistered += OnComponentRegistered; 
     // add environment configurators 
    } 
    private void OnComponentRegistered(string key, IHandler handler) { 
     // if the component is a configurator then run conf settings and add it to configuredComponents 
    }} 

질문 시작 응용 프로그램에서이 작업을 실행 눌렀다 명시 적 구성 요소를 호출하기 위해 후크 continer 해제 방법 : 아래로 눈물을 훅하고 각각에 대해 명시 적으로 해제 전화를?

감사

+0

가 나는 처분을 무시하는 것이 도움이 될 것으로 기대하지만 그렇게 :( – ruslander

+0

이 좀 더 설명해주십시오 아니다 : 당신은, "해체"무엇이라고 부릅니까 당신이 해제 무엇을 원하는가 왜 –

+0

를 응용 프로그램에서 내가 '시작합니다.이 환경 유효성 검사를하고 뭔가가 외부 하드웨어 연결처럼 구성되지 않은 경우 ... 할 수 있습니다 ... 컨테이너 (응용 프로그램)가 중지되었을 때 uninitailize하고 정리를하고 싶습니다 ... 다른 방법이 있습니다. 이 일을하지만 깨끗하고 간단한 것을 원합니다. – ruslander

답변

2

당신은 ComponentDestroyedIKernel의 이벤트 또는 당신의 구성 요소에 IDisposable을 구현을 사용할 수 있습니다. 약간의 샘플 코드는 다음과 같습니다.

namespace WindsorInitConfig { 
    [TestFixture] 
    public class ConfigurationFacilityTests { 
     [Test] 
     public void tt() { 
      OneDisposableComponent component = null; 
      using (var container = new WindsorContainer()) { 
       container.AddFacility<ConfigurationFacility>(); 
       container.AddComponent<OneDisposableComponent>(); 
       component = container.Resolve<OneDisposableComponent>(); 
      } 
      Assert.IsTrue(component.Disposed); 
      Assert.Contains(component, ConfigurationFacility.DestroyedComponents); 
     } 

     public class OneDisposableComponent : IDisposable { 
      public bool Disposed { get; private set; } 

      public void Dispose() { 
       Disposed = true; 
      } 
     } 

     public class ConfigurationFacility : AbstractFacility { 
      private readonly List<string> configuredComponents = new List<string>(); 
      public static readonly ArrayList DestroyedComponents = new ArrayList(); 

      protected override void Init() { 
       Kernel.ComponentRegistered += OnComponentRegistered; 
       Kernel.ComponentDestroyed += Kernel_ComponentDestroyed; 
       // add environment configurators 
      } 

      private void Kernel_ComponentDestroyed(ComponentModel model, object instance) { 
       DestroyedComponents.Add(instance); 
       // uninitialization, cleanup 
      } 

      private void OnComponentRegistered(string key, IHandler handler) { 
       // if the component is a configurator then run conf settings and add it to configuredComponents 
       configuredComponents.Add(key);} 
     } 
    } 
} 

정적 ArrayList는 물론 데모 용입니다.