<PackageReference Include="Castle.Windsor" Version="2.5.3" />

PoolableLifestyleManager

Implements a Poolable Lifestyle Manager.
using Castle.MicroKernel.Context; using Castle.MicroKernel.Lifestyle.Pool; using Castle.MicroKernel.Registration; using System; namespace Castle.MicroKernel.Lifestyle { [Serializable] public class PoolableLifestyleManager : AbstractLifestyleManager { private IPool pool; private int initialSize; private int maxSize; public PoolableLifestyleManager(int initialSize, int maxSize) { this.initialSize = initialSize; this.maxSize = maxSize; } public override object Resolve(CreationContext context) { if (pool == null) { lock (base.ComponentActivator) { if (pool == null) pool = CreatePool(initialSize, maxSize); } } return pool.Request(context); } public override bool Release(object instance) { if (pool != null) return pool.Release(instance); return false; } public override void Dispose() { if (pool != null) pool.Dispose(); } protected IPool CreatePool(int initialSize, int maxSize) { if (!base.Kernel.HasComponent(typeof(IPoolFactory))) base.Kernel.Register(Component.For<IPoolFactory>().ImplementedBy<DefaultPoolFactory>().Named("castle.internal.poolfactory")); IPoolFactory poolFactory = base.Kernel.Resolve<IPoolFactory>(); return poolFactory.Create(initialSize, maxSize, base.ComponentActivator); } } }