Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="5.5.2-preview-768" />

 BaseFluentConfigurator<TConfigurator>

public class BaseFluentConfigurator<TConfigurator> : ServiceRegistration 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 Stashbox.Utils.Data; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; namespace Stashbox.Registration.Fluent { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class BaseFluentConfigurator<[System.Runtime.CompilerServices.Nullable(0)] TConfigurator> : ServiceRegistration where TConfigurator : BaseFluentConfigurator<TConfigurator> { public Type ServiceType { get; } internal BaseFluentConfigurator(Type serviceType, Type implementationType, [System.Runtime.CompilerServices.Nullable(2)] object name, LifetimeDescriptor lifetimeDescriptor, bool isDecorator) : base(implementationType, name, lifetimeDescriptor, isDecorator, null, null, null) { ServiceType = serviceType; } public TConfigurator WithLifetime(LifetimeDescriptor lifetime) { base.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 WithPerRequestLifetime() { return WithLifetime(Lifetimes.PerRequest); } public TConfigurator InNamedScope(object scopeName) { return WithLifetime(Lifetimes.NamedScope(scopeName)); } public TConfigurator InScopeDefinedBy(Type type) { return WithLifetime(Lifetimes.NamedScope(type)); } public TConfigurator InScopeDefinedBy<[System.Runtime.CompilerServices.Nullable(2)] TScopeDefiner>() { return this.WithLifetime(Lifetimes.NamedScope(typeof(TScopeDefiner))); } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public TConfigurator WithDependencyBinding<TDependency>(object dependencyName = null) { return this.WithDependencyBinding(typeof(TDependency), dependencyName); } public TConfigurator WithDependencyBinding(Type dependencyType, [System.Runtime.CompilerServices.Nullable(2)] object dependencyName = null) { Shield.EnsureNotNull<Type>(dependencyType, "dependencyType"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.DependencyBindings, out object value)) { Dictionary<object, object> dictionary = value as Dictionary<object, object>; if (dictionary != null) { dictionary.Add(dependencyType, dependencyName); goto IL_005b; } } Options[RegistrationOption.DependencyBindings] = new Dictionary<object, object> { { dependencyType, dependencyName } }; goto IL_005b; IL_005b: return (TConfigurator)this; } public TConfigurator WithDependencyBinding(string parameterName, [System.Runtime.CompilerServices.Nullable(2)] object dependencyName = null) { Shield.EnsureNotNull<string>(parameterName, "parameterName"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.DependencyBindings, out object value)) { Dictionary<object, object> dictionary = value as Dictionary<object, object>; if (dictionary != null) { dictionary.Add(parameterName, dependencyName); goto IL_005b; } } Options[RegistrationOption.DependencyBindings] = new Dictionary<object, object> { { parameterName, dependencyName } }; goto IL_005b; IL_005b: 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"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.ConditionOptions, out object value)) { ConditionOptions conditionOptions = value as ConditionOptions; if (conditionOptions != null) { ConditionOptions conditionOptions2 = conditionOptions; if (conditionOptions2.TargetTypeConditions == null) { ExpandableArray<Type> expandableArray2 = conditionOptions2.TargetTypeConditions = new ExpandableArray<Type>(); } conditionOptions.TargetTypeConditions.Add(targetType); goto IL_0082; } } Options[RegistrationOption.ConditionOptions] = new ConditionOptions { TargetTypeConditions = new ExpandableArray<Type> { targetType } }; goto IL_0082; IL_0082: return (TConfigurator)this; } public TConfigurator WhenHas<[System.Runtime.CompilerServices.Nullable(0)] TAttribute>() where TAttribute : Attribute { return this.WhenHas(typeof(TAttribute)); } public TConfigurator WhenHas(Type attributeType) { Shield.EnsureNotNull<Type>(attributeType, "attributeType"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.ConditionOptions, out object value)) { ConditionOptions conditionOptions = value as ConditionOptions; if (conditionOptions != null) { ConditionOptions conditionOptions2 = conditionOptions; if (conditionOptions2.AttributeConditions == null) { ExpandableArray<Type> expandableArray2 = conditionOptions2.AttributeConditions = new ExpandableArray<Type>(); } conditionOptions.AttributeConditions.Add(attributeType); goto IL_0082; } } Options[RegistrationOption.ConditionOptions] = new ConditionOptions { AttributeConditions = new ExpandableArray<Type> { attributeType } }; goto IL_0082; IL_0082: return (TConfigurator)this; } public TConfigurator When(Func<TypeInformation, bool> resolutionCondition) { Shield.EnsureNotNull<Func<TypeInformation, bool>>(resolutionCondition, "resolutionCondition"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.ConditionOptions, out object value)) { ConditionOptions conditionOptions = value as ConditionOptions; if (conditionOptions != null) { ConditionOptions conditionOptions2 = conditionOptions; if (conditionOptions2.ResolutionConditions == null) { ExpandableArray<Func<TypeInformation, bool>> expandableArray2 = conditionOptions2.ResolutionConditions = new ExpandableArray<Func<TypeInformation, bool>>(); } conditionOptions.ResolutionConditions.Add(resolutionCondition); goto IL_0082; } } Options[RegistrationOption.ConditionOptions] = new ConditionOptions { ResolutionConditions = new ExpandableArray<Func<TypeInformation, bool>> { resolutionCondition } }; goto IL_0082; IL_0082: return (TConfigurator)this; } public TConfigurator WithInjectionParameters([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1, 2 })] params KeyValuePair<string, object>[] injectionParameters) { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.InjectionParameters, out object value)) { ExpandableArray<KeyValuePair<string, object>> expandableArray = value as ExpandableArray<KeyValuePair<string, object>>; if (expandableArray != null) { expandableArray.AddRange(injectionParameters); goto IL_004a; } } Options[RegistrationOption.InjectionParameters] = new ExpandableArray<KeyValuePair<string, object>>(injectionParameters); goto IL_004a; IL_004a: return (TConfigurator)this; } public TConfigurator WithInjectionParameter(string name, [System.Runtime.CompilerServices.Nullable(2)] object value) { Shield.EnsureNotNull<string>(name, "name"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.InjectionParameters, out object value2)) { ExpandableArray<KeyValuePair<string, object>> expandableArray = value2 as ExpandableArray<KeyValuePair<string, object>>; if (expandableArray != null) { expandableArray.Add(new KeyValuePair<string, object>(name, value)); goto IL_0067; } } Options[RegistrationOption.InjectionParameters] = new ExpandableArray<KeyValuePair<string, object>> { new KeyValuePair<string, object>(name, value) }; goto IL_0067; IL_0067: return (TConfigurator)this; } public TConfigurator WithAutoMemberInjection(Rules.AutoMemberInjectionRules rule = Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] Func<MemberInfo, bool> filter = null) { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.AutoMemberOptions] = new AutoMemberOptions(rule, filter); return (TConfigurator)this; } public TConfigurator WithConstructorSelectionRule(Func<IEnumerable<ConstructorInfo>, IEnumerable<ConstructorInfo>> rule) { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.ConstructorSelectionRule] = rule; return (TConfigurator)this; } public TConfigurator WithConstructorByArgumentTypes(params Type[] argumentTypes) { ConstructorInfo constructor = base.ImplementationType.GetConstructor(argumentTypes); if (constructor == (ConstructorInfo)null) { ThrowConstructorNotFoundException(base.ImplementationType, argumentTypes); return (TConfigurator)this; } if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.ConstructorOptions] = new ConstructorOptions(constructor, null); 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 constructor = base.ImplementationType.GetConstructor(array); if (constructor == (ConstructorInfo)null) { ThrowConstructorNotFoundException(base.ImplementationType, array); return (TConfigurator)this; } if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.ConstructorOptions] = new ConstructorOptions(constructor, arguments); return (TConfigurator)this; } public TConfigurator WithoutDisposalTracking() { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.IsLifetimeExternallyOwned] = true; return (TConfigurator)this; } public TConfigurator ReplaceExisting() { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.ReplaceExistingRegistration] = true; return (TConfigurator)this; } public TConfigurator ReplaceOnlyIfExists() { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.ReplaceExistingRegistrationOnlyIfExists] = true; return (TConfigurator)this; } public TConfigurator AsImplementedTypes() { if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.AdditionalServiceTypes] = new ExpandableArray<Type>(Enumerable.Concat<Type>(base.ImplementationType.GetRegisterableInterfaceTypes(), base.ImplementationType.GetRegisterableBaseTypes())); return (TConfigurator)this; } public TConfigurator AsServiceAlso<[System.Runtime.CompilerServices.Nullable(2)] TAdditionalService>() { return this.AsServiceAlso(typeof(TAdditionalService)); } public TConfigurator AsServiceAlso(Type serviceType) { if (!base.ImplementationType.Implements(serviceType)) { DefaultInterpolatedStringHandler val = default(DefaultInterpolatedStringHandler); val..ctor(68, 2); val.AppendLiteral("The implementation type "); val.AppendFormatted<Type>(base.ImplementationType); val.AppendLiteral(" does not implement the given service type "); val.AppendFormatted<Type>(serviceType); val.AppendLiteral("."); throw new ArgumentException(val.ToStringAndClear()); } if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.AdditionalServiceTypes] = new ExpandableArray<Type> { serviceType }; return (TConfigurator)this; } internal void ValidateTypeMap() { if (!IsFactory()) Shield.EnsureTypeMapIsValid(ServiceType, base.ImplementationType); } internal void ValidateImplementationIsResolvable() { if (!IsFactory()) Shield.EnsureIsResolvable(base.ImplementationType); } private protected TConfigurator SetFactory(Delegate factory, bool isCompiledLambda, params Type[] parameterTypes) { Shield.EnsureNotNull<Delegate>(factory, "factory"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.RegistrationTypeOptions] = new FactoryOptions(factory, parameterTypes, isCompiledLambda); return (TConfigurator)this; } 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; } } }