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

LifestyleGroup<TService>

public class LifestyleGroup<TService> : RegistrationGroup<TService>
using Castle.Core; using Castle.Core.Internal; using Castle.MicroKernel.Lifestyle.Scoped; using Castle.MicroKernel.ModelBuilder.Descriptors; using System; namespace Castle.MicroKernel.Registration.Lifestyle { public class LifestyleGroup<TService> : RegistrationGroup<TService> where TService : class { public ComponentRegistration<TService> Transient => AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Transient)); public ComponentRegistration<TService> Singleton => AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Singleton)); public ComponentRegistration<TService> PerThread => AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Thread)); public ComponentRegistration<TService> Pooled => AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Pooled)); public LifestyleGroup(ComponentRegistration<TService> registration) : base(registration) { } public ComponentRegistration<TService> Is(LifestyleType type) { if (!Enum.IsDefined(typeof(LifestyleType), type)) throw InvalidValue(type, "Not a valid lifestyle"); if (type == LifestyleType.Undefined) throw InvalidValue(type, $"{LifestyleType.Undefined}"""); return AddDescriptor(new LifestyleDescriptor<TService>(type)); } private ArgumentOutOfRangeException InvalidValue(LifestyleType type, string message) { return new ArgumentOutOfRangeException("type", type, message); } public ComponentRegistration<TService> PooledWithSize(int? initialSize, int? maxSize) { ComponentRegistration<TService> componentRegistration = Pooled; if (initialSize.HasValue) componentRegistration = componentRegistration.Attribute("initialPoolSize").Eq(initialSize); if (maxSize.HasValue) componentRegistration = componentRegistration.Attribute("maxPoolSize").Eq(maxSize); return componentRegistration; } public ComponentRegistration<TService> Scoped<TScopeAccessor>() where TScopeAccessor : IScopeAccessor, new { return this.Scoped(typeof(TScopeAccessor)); } public ComponentRegistration<TService> Scoped(Type scopeAccessorType) { ComponentRegistration<TService> componentRegistration = AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Scoped)); if ((object)scopeAccessorType == null) return componentRegistration; ExtendedPropertiesDescriptor descriptor = new ExtendedPropertiesDescriptor(new Property(Constants.ScopeAccessorType, scopeAccessorType)); return componentRegistration.AddDescriptor(descriptor); } public ComponentRegistration<TService> Scoped() { return Scoped(null); } public ComponentRegistration<TService> BoundTo<TBaseForRoot>() where TBaseForRoot : class { return this.BoundTo((Func<IHandler[], IHandler>)CreationContextScopeAccessor.DefaultScopeRootSelector<TBaseForRoot>); } public ComponentRegistration<TService> BoundToNearest<TBaseForRoot>() where TBaseForRoot : class { return this.BoundTo((Func<IHandler[], IHandler>)CreationContextScopeAccessor.NearestScopeRootSelector<TBaseForRoot>); } public ComponentRegistration<TService> BoundTo(Func<IHandler[], IHandler> scopeRootBinder) { return AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Bound)).ExtendedProperties(new Property(Constants.ScopeRootSelector, scopeRootBinder)); } public ComponentRegistration<TService> Custom(Type customLifestyleType) { if (!ReflectionUtil.Is<ILifestyleManager>(customLifestyleType)) throw new ComponentRegistrationException($"""{customLifestyleType.FullName}""{typeof(ILifestyleManager).FullName}"""); return AddDescriptor(new LifestyleDescriptor<TService>(LifestyleType.Custom)).Attribute("customLifestyleType").Eq(customLifestyleType.AssemblyQualifiedName); } public ComponentRegistration<TService> Custom<TLifestyleManager>() where TLifestyleManager : ILifestyleManager, new { return this.Custom(typeof(TLifestyleManager)); } } }