Sure:

using System;
using System.Collections.Generic;
using Castle.Core.Logging;
using Castle.Facilities.Logging;
using NUnit.Framework;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace TypedFactoryDemo
{
    public class MyComponent:IDisposable
    {
        public bool IsDisposed { get; private set; }

        public MyComponent(ILogger logger,string name)
        {
        }

        public void Dispose()
        {
            IsDisposed = true;
        }
    }
    
   public  interface IMyComponentFactory
    {
        MyComponent Create(string name);
        void Release(MyComponent component);
    }

    class Manager:IDisposable
    {
        private readonly IMyComponentFactory m_Factory;
        private readonly List<MyComponent> m_Components=new 
List<MyComponent>();

        public Manager(IMyComponentFactory factory)
        {
            m_Factory = factory;
        }

        public void InitializeComponent(string name)
        {
            m_Components.Add(m_Factory.Create(name));
        }

        public void Dispose()
        {
            foreach (var component in m_Components)
            {
                m_Factory.Release(component);
            }
        }
    }

    [TestFixture]
    public class Investigation
    {
        [Test]
        [Ignore]
        public void Test()
        {
            using (var container=new WindsorContainer())
            {
                container
                    .AddFacility<TypedFactoryFacility>()
                    .AddFacility<LoggingFacility>()
                    .Register(
                        Component.For<IMyComponentFactory>().AsFactory(),
                        Component.For<MyComponent>().LifestyleTransient(),
                        Component.For<Manager>()
                        );

                var manager = container.Resolve<Manager>();

                manager.InitializeComponent("component1");
                manager.InitializeComponent("component2");
            }
        }

    }
}

On Saturday, September 21, 2013 3:27:35 PM UTC+4, Krzysztof Koźmic wrote:
>
> can you reproduce that in a test?
>
> -- 
> Krzysztof Kozmic
>
> On Saturday, 21 September 2013 at 8:50 PM, Konstantin wrote:
>
> I have a manager that creates component instanses. To instantiated 
> componenets the typed factory is used, but once component is instanciated 
> manager is responsible for it's further setup and configuration.
> I'd like to controll the process of components disposing within manager. 
> Something like this in manager.Dispose method:
>         public void Dispose()
>         {
>             m_Logger.Info("Manager is disposing");
>             m_Logger.Info("Stopping components");
>             foreach (var component in m_Components)
>             {
>                 VerifyState(component);
>                 m_Factory.Release(component);
>             }
>             m_Logger.Info("Manager is disposed");
>         }
>
> But I get "System.ObjectDisposedException : The factory was disposed and 
> can no longer be used." when I try to use factory in Dispose.
> Looks like a bug to me - manager depends on factory thus container should 
> dispose manager before the factory...
>
>  -- 
> You received this message because you are subscribed to the Google Groups 
> "Castle Project Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To post to this group, send email to 
> [email protected]<javascript:>
> .
> Visit this group at http://groups.google.com/group/castle-project-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to