>From the documentation, it states: Transient lifestyle is a good choice 
when you want to be in control of instance's lifetime of the instances. 
When you need new instance, with new state every time.

When I run the following test, though, it would seem this is not the case. 
  If I run the "Program" console app below, all JP instances returned have 
an ID of 10 (see attached JpOutput.gif).  If I pause for 500 milliseconds 
in between Resolve requests (attached JpOutputWithDelay.gif), then the IDs 
are incremented properly.  So, it would seem the objects being returned 
from the container and not a new instance with a new state every time?  Is 
this reason for concern?  Or maybe I'm doing something wrong with this test?

public class Program
    {

        public static void Main(string[] args)
        {
            var windsor = new WindsorContainer();
            windsor.Install(new WindsorInstaller());
            
            Console.WriteLine(String.Empty);
            Console.WriteLine(String.Empty);

            for (var i = 0; i < 10; i++)
            {
                new Thread(()=> {
                    var jp = windsor.Resolve<IJP>();
                    jp.ID = i;

                    jp.Extract();
                    windsor.Release(jp);
                }
                ).Start();
                //Thread.Sleep(500);
            }
            
            Console.WriteLine("Hit ENTER to exit.");
            Console.ReadLine();
        }
    }

    public class WindsorInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, 
IConfigurationStore store)
        {
            container.AddFacility<TypedFactoryFacility>();

            container.Register(
                Component.For<IJP>().ImplementedBy<JP>().Named("IJP")
.LifeStyle.Transient.OnDestroy(myInstance => myInstance.ByeBye()));

            
container.Register(Component.For<IExtractionJob>().ImplementedBy<ExtractionJob>().Named("IExtractionJob")
.LifeStyle.Transient);

            
container.Register(Component.For<IWindsorFactory>().AsFactory().Named("IWindsorFactory"));
        }
    }

public interface IJP
    {
        IWindsorFactory Factory { get; set; }
        int ID { get; set; }
        void Extract();
        void ByeBye();
        void Dispose();
    }

    public class JP : IJP, IDisposable
    {
        public IWindsorFactory Factory { get; set; }
        public int ID { get; set; }

        public JP(IWindsorFactory factory)
        {
            Factory = factory;
        }

        public void Extract()
        {
            Console.WriteLine("Extracting in JP with ID {0}.", ID);
            Thread.Sleep(5000);
        }

        public void ByeBye()
        {
            Console.WriteLine("JP with ID {0} is in ByeBye: {1}", ID, 
DateTime.Now);
        }

        bool disposed = false;
        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
                Console.WriteLine(string.Format("JP with ID {0} is being 
disposed: {1}", ID, DateTime.Now));

            disposed = true;
        }
    }


-- 
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/d/optout.

Reply via email to