1

I run this at the application Start Up

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

Question: How to hook tear down and to call explicit release for each ?

Thanks

ruslander
  • 3,815
  • 4
  • 32
  • 34
  • I hoped that overriding the dispose will help but it's not so :( – ruslander Oct 06 '09 at 13:54
  • please explain a bit more: what do you call "tear down", what do you want to release and why. – Mauricio Scheffer Oct 07 '09 at 01:06
  • At the application start up I'm doing environment validation and if something is not configured like external hardware connectivity ... I do it ... when the container (application) is stopped I'd like to uninitailze and do cleanup ... I know there are other ways to do this but I'd like something clean and simple – ruslander Oct 07 '09 at 13:54

1 Answers1

2

You can use either the ComponentDestroyed event of IKernel or just implement IDisposable in your components. Here's a little sample code:

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

The static ArrayList is only for demo purposes, of course.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275