Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="3.6.4-preview-652" />

 BaseFluentConfigurator<TConfigurator>

public class BaseFluentConfigurator<TConfigurator> : RegistrationConfiguration where TConfigurator : BaseFluentConfigurator<TConfigurator>
Represents the base of the fluent registration api.
using Stashbox.Configuration; using Stashbox.Exceptions; using Stashbox.Lifetime; using Stashbox.Resolution; using Stashbox.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Stashbox.Registration.Fluent { public class BaseFluentConfigurator<TConfigurator> : RegistrationConfiguration where TConfigurator : BaseFluentConfigurator<TConfigurator> { internal BaseFluentConfigurator(Type serviceType, Type implementationType) : base(serviceType, implementationType) { } internal BaseFluentConfigurator(Type serviceType, Type implementationType, RegistrationContext registrationContext) : base(serviceType, implementationType, registrationContext) { } public TConfigurator WithLifetime(LifetimeDescriptor lifetime) { base.Context.Lifetime = lifetime; return (TConfigurator)this; } public TConfigurator WithScopedLifetime() { return WithLifetime(Lifetimes.Scoped); } public TConfigurator WithSingletonLifetime() { return WithLifetime(Lifetimes.Singleton); } public TConfigurator WithTransientLifetime() { return WithLifetime(Lifetimes.Transient); } public TConfigurator WithPerScopedRequestLifetime() { return WithLifetime(Lifetimes.PerScopedRequest); } public TConfigurator InNamedScope(object scopeName) { return WithLifetime(Lifetimes.NamedScope(scopeName)); } public TConfigurator InScopeDefinedBy(Type type) { return WithLifetime(Lifetimes.NamedScope(type)); } public TConfigurator InScopeDefinedBy<TScopeDefiner>() { return this.WithLifetime(Lifetimes.NamedScope(typeof(TScopeDefiner))); } public TConfigurator WithDependencyBinding<TDependency>(object dependencyName = null) { return this.WithDependencyBinding(typeof(TDependency), dependencyName); } public TConfigurator WithDependencyBinding(Type dependencyType, object dependencyName = null) { Shield.EnsureNotNull<Type>(dependencyType, "dependencyType"); base.Context.DependencyBindings.Add(dependencyType, dependencyName); return (TConfigurator)this; } public TConfigurator WithDependencyBinding(string parameterName, object dependencyName = null) { Shield.EnsureNotNull<string>(parameterName, "parameterName"); base.Context.DependencyBindings.Add(parameterName, dependencyName); return (TConfigurator)this; } public TConfigurator WhenDependantIs<TTarget>() where TTarget : class { return this.WhenDependantIs(typeof(TTarget)); } public TConfigurator WhenDependantIs(Type targetType) { Shield.EnsureNotNull<Type>(targetType, "targetType"); base.Context.TargetTypeConditions.Add(targetType); return (TConfigurator)this; } public TConfigurator WhenHas<TAttribute>() where TAttribute : Attribute { return this.WhenHas(typeof(TAttribute)); } public TConfigurator WhenHas(Type attributeType) { base.Context.AttributeConditions.Add(attributeType); return (TConfigurator)this; } public TConfigurator When(Func<TypeInformation, bool> resolutionCondition) { base.Context.ResolutionConditions.Add(resolutionCondition); return (TConfigurator)this; } public TConfigurator WithInjectionParameters(params KeyValuePair<string, object>[] injectionParameters) { base.Context.InjectionParameters.AddRange(injectionParameters); return (TConfigurator)this; } public TConfigurator WithInjectionParameter(string name, object value) { base.Context.InjectionParameters.Add(new KeyValuePair<string, object>(name, value)); return (TConfigurator)this; } public TConfigurator WithAutoMemberInjection(Rules.AutoMemberInjectionRules rule = Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter, Func<MemberInfo, bool> filter = null) { base.Context.AutoMemberInjectionEnabled = true; base.Context.AutoMemberInjectionRule = rule; base.Context.AutoMemberInjectionFilter = filter; return (TConfigurator)this; } public TConfigurator WithConstructorSelectionRule(Func<IEnumerable<ConstructorInfo>, IEnumerable<ConstructorInfo>> rule) { base.Context.ConstructorSelectionRule = rule; return (TConfigurator)this; } public TConfigurator WithConstructorByArgumentTypes(params Type[] argumentTypes) { ConstructorInfo constructorByArguments = base.ImplementationType.GetConstructorByArguments(argumentTypes); if (constructorByArguments == (ConstructorInfo)null) ThrowConstructorNotFoundException(base.ImplementationType, argumentTypes); base.Context.SelectedConstructor = constructorByArguments; return (TConfigurator)this; } public TConfigurator WithConstructorByArguments(params object[] arguments) { Type[] array = Enumerable.ToArray<Type>(Enumerable.Select<object, Type>((IEnumerable<object>)arguments, (Func<object, Type>)((object arg) => arg.GetType()))); ConstructorInfo constructorByArguments = base.ImplementationType.GetConstructorByArguments(array); if (constructorByArguments == (ConstructorInfo)null) ThrowConstructorNotFoundException(base.ImplementationType, array); base.Context.SelectedConstructor = constructorByArguments; base.Context.ConstructorArguments = arguments; return (TConfigurator)this; } [Obsolete("Use WithDependencyBinding() instead.")] public TConfigurator InjectMember(string memberName, object dependencyName = null) { return WithDependencyBinding(memberName, dependencyName); } public TConfigurator WithoutDisposalTracking() { base.Context.IsLifetimeExternallyOwned = true; return (TConfigurator)this; } public TConfigurator ReplaceExisting() { base.Context.ReplaceExistingRegistration = true; return (TConfigurator)this; } public TConfigurator ReplaceOnlyIfExists() { base.Context.ReplaceExistingRegistrationOnlyIfExists = true; return (TConfigurator)this; } public TConfigurator AsImplementedTypes() { base.Context.AdditionalServiceTypes.AddRange(Enumerable.Concat<Type>(base.ImplementationType.GetRegisterableInterfaceTypes(), base.ImplementationType.GetRegisterableBaseTypes())); return (TConfigurator)this; } public TConfigurator AsServiceAlso<TAdditionalService>() { return this.AsServiceAlso(typeof(TAdditionalService)); } public TConfigurator AsServiceAlso(Type serviceType) { if (!base.ImplementationType.Implements(serviceType)) throw new ArgumentException($"""{base.ImplementationType}""{serviceType}"""); base.Context.AdditionalServiceTypes.Add(serviceType); return (TConfigurator)this; } private protected void SetFactory(Delegate factory, bool isCompiledLambda, params Type[] parameterTypes) { base.Context.Factory = factory; base.Context.FactoryParameters = parameterTypes; base.Context.IsFactoryDelegateACompiledLambda = isCompiledLambda; } private static void ThrowConstructorNotFoundException(Type type, params Type[] argTypes) { ConstructorNotFoundException ex; switch (argTypes.LongLength) { case 0: ex = new ConstructorNotFoundException(type, (Exception)null); break; case 1: ex = new ConstructorNotFoundException(type, argTypes[0], null); break; default: ex = new ConstructorNotFoundException(type, argTypes, null); break; } throw ex; } } }