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
    }}

質問:ティアダウンをフックして、それぞれの明示的なリリースを呼び出す方法は?

ありがとう

4

1 に答える 1

2

ComponentDestroyedのイベントを使用することも、コンポーネントIKernelに実装することもでき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はデモ目的のみです。

于 2009-10-07T22:36:14.843 に答える