Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="5.2.0-preview-743" />

 ResolutionContext

public class ResolutionContext
Represents information about the actual resolution flow.
using Stashbox.Registration; using Stashbox.Utils; using Stashbox.Utils.Data; using Stashbox.Utils.Data.Immutable; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace Stashbox.Resolution { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class ResolutionContext { [System.Runtime.CompilerServices.NullableContext(0)] internal class PerRequestConfiguration { public bool RequiresRequestContext { get; set; } public bool FactoryDelegateCacheEnabled { get; set; } } private readonly Tree<Expression> expressionCache; internal readonly Stashbox.Utils.Data.Stack<object> ScopeNames; internal readonly PerRequestConfiguration RequestConfiguration; internal readonly IContainerContext RequestInitiatorContainerContext; internal readonly Stashbox.Utils.Data.Stack<int> CircularDependencyBarrier; internal readonly Tree<Func<IResolutionScope, IRequestContext, object>> FactoryCache; [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] internal readonly HashTree<object, ConstantExpression> ExpressionOverrides; internal readonly ExpandableArray<Expression> SingleInstructions; internal readonly Tree<ParameterExpression> DefinedVariables; internal readonly ExpandableArray<Pair<bool, ParameterExpression>[]> ParameterExpressions; internal readonly ExpandableArray<Type, Stashbox.Utils.Data.Stack<ServiceRegistration>> RemainingDecorators; internal readonly ExpandableArray<ServiceRegistration> CurrentDecorators; internal readonly int CurrentLifeSpan; [System.Runtime.CompilerServices.Nullable(2)] internal readonly string NameOfServiceLifeSpanValidatingAgainst; internal readonly bool PerResolutionRequestCacheEnabled; internal readonly bool UnknownTypeCheckDisabled; internal readonly bool ShouldFallBackToRequestInitiatorContext; internal readonly bool IsTopRequest; public readonly bool NullResultAllowed; public readonly bool IsRequestedFromRoot; public readonly ParameterExpression RequestContextParameter; public readonly ParameterExpression CurrentScopeParameter; public readonly IContainerContext CurrentContainerContext; private ResolutionContext(IEnumerable<object> initialScopeNames, IContainerContext currentContainerContext, bool isTopLevel, bool isRequestedFromRoot, bool nullResultAllowed, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] object[] dependencyOverrides, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] ImmutableTree<object, object> knownInstances, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] ParameterExpression[] initialParameters) { RequestConfiguration = new PerRequestConfiguration(); DefinedVariables = new Tree<ParameterExpression>(); SingleInstructions = new ExpandableArray<Expression>(); RemainingDecorators = new ExpandableArray<Type, Stashbox.Utils.Data.Stack<ServiceRegistration>>(); CurrentDecorators = new ExpandableArray<ServiceRegistration>(); CircularDependencyBarrier = new Stashbox.Utils.Data.Stack<int>(); expressionCache = new Tree<Expression>(); FactoryCache = new Tree<Func<IResolutionScope, IRequestContext, object>>(); NullResultAllowed = nullResultAllowed; IsRequestedFromRoot = isRequestedFromRoot; IsTopRequest = isTopLevel; ScopeNames = initialScopeNames.AsStack(); CurrentScopeParameter = Constants.ResolutionScopeParameter; RequestContextParameter = Constants.RequestContextParameter; CurrentContainerContext = (RequestInitiatorContainerContext = currentContainerContext); RequestConfiguration.FactoryDelegateCacheEnabled = (PerResolutionRequestCacheEnabled = (dependencyOverrides == null)); ExpressionOverrides = ((dependencyOverrides == null && (knownInstances == null || knownInstances.IsEmpty)) ? null : ProcessDependencyOverrides(dependencyOverrides, knownInstances)); ParameterExpressions = ((initialParameters != null) ? new ExpandableArray<Pair<bool, ParameterExpression>[]> { initialParameters.AsParameterPairs() } : new ExpandableArray<Pair<bool, ParameterExpression>[]>()); } private ResolutionContext(PerRequestConfiguration perRequestConfiguration, Tree<ParameterExpression> definedVariables, ExpandableArray<Expression> singleInstructions, ExpandableArray<Type, Stashbox.Utils.Data.Stack<ServiceRegistration>> remainingDecorators, ExpandableArray<ServiceRegistration> currentDecorators, Stashbox.Utils.Data.Stack<int> circularDependencyBarrier, Tree<Expression> cachedExpressions, Tree<Func<IResolutionScope, IRequestContext, object>> factoryCache, Stashbox.Utils.Data.Stack<object> scopeNames, ParameterExpression currentScopeParameter, ParameterExpression requestContextParameter, IContainerContext currentContainerContext, IContainerContext requestInitiatorContainerContext, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] HashTree<object, ConstantExpression> expressionOverrides, ExpandableArray<Pair<bool, ParameterExpression>[]> parameterExpressions, bool nullResultAllowed, bool isRequestedFromRoot, bool isTopLevel, [System.Runtime.CompilerServices.Nullable(2)] string nameOfServiceLifeSpanValidatingAgainst, int currentLifeSpan, bool perResolutionRequestCacheEnabled, bool unknownTypeCheckDisabled, bool shouldFallBackToRequestInitiatorContext) { RequestConfiguration = perRequestConfiguration; DefinedVariables = definedVariables; SingleInstructions = singleInstructions; RemainingDecorators = remainingDecorators; CurrentDecorators = currentDecorators; CircularDependencyBarrier = circularDependencyBarrier; expressionCache = cachedExpressions; FactoryCache = factoryCache; NullResultAllowed = nullResultAllowed; IsRequestedFromRoot = isRequestedFromRoot; IsTopRequest = isTopLevel; ScopeNames = scopeNames; CurrentScopeParameter = currentScopeParameter; RequestContextParameter = requestContextParameter; CurrentContainerContext = currentContainerContext; RequestInitiatorContainerContext = requestInitiatorContainerContext; ExpressionOverrides = expressionOverrides; ParameterExpressions = parameterExpressions; NameOfServiceLifeSpanValidatingAgainst = nameOfServiceLifeSpanValidatingAgainst; CurrentLifeSpan = currentLifeSpan; PerResolutionRequestCacheEnabled = perResolutionRequestCacheEnabled; UnknownTypeCheckDisabled = unknownTypeCheckDisabled; ShouldFallBackToRequestInitiatorContext = shouldFallBackToRequestInitiatorContext; } public void AddInstruction(Expression instruction) { SingleInstructions.Add(instruction); } public void AddDefinedVariable(int key, ParameterExpression parameter) { DefinedVariables.Add(key, parameter); } public void AddDefinedVariable(ParameterExpression parameter) { DefinedVariables.Add(RuntimeHelpers.GetHashCode(parameter), parameter); } [System.Runtime.CompilerServices.NullableContext(2)] public ParameterExpression GetKnownVariableOrDefault(int key) { return DefinedVariables.GetOrDefault(key); } internal void CacheExpression(int key, Expression expression) { expressionCache.Add(key, expression); } [System.Runtime.CompilerServices.NullableContext(2)] internal Expression GetCachedExpression(int key) { return expressionCache.GetOrDefault(key); } internal static ResolutionContext BeginTopLevelContext(IEnumerable<object> initialScopeNames, IContainerContext currentContainerContext, bool isRequestedFromRoot, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] object[] dependencyOverrides = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] ImmutableTree<object, object> knownInstances = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] ParameterExpression[] initialParameters = null) { return new ResolutionContext(initialScopeNames, currentContainerContext, true, isRequestedFromRoot, false, dependencyOverrides, knownInstances, initialParameters); } internal static ResolutionContext BeginNullableTopLevelContext(IEnumerable<object> initialScopeNames, IContainerContext currentContainerContext, bool isRequestedFromRoot, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] object[] dependencyOverrides = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] ImmutableTree<object, object> knownInstances = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] ParameterExpression[] initialParameters = null) { return new ResolutionContext(initialScopeNames, currentContainerContext, true, isRequestedFromRoot, true, dependencyOverrides, knownInstances, initialParameters); } internal static ResolutionContext BeginValidationContext(IContainerContext currentContainerContext) { return new ResolutionContext(Constants.EmptyArray<object>(), currentContainerContext, true, false, false, null, null, null); } internal ResolutionContext BeginSubDependencyContext() { if (IsTopRequest) { bool? isTopRequest = false; return Clone(null, null, null, null, null, null, null, null, null, null, null, isTopRequest, null, null, null); } return this; } internal ResolutionContext BeginCrossContainerContext(IContainerContext currentContainerContext) { bool? shouldFallBackToRequestInitiatorContext = RequestInitiatorContainerContext != currentContainerContext; return Clone(null, null, null, null, null, null, null, currentContainerContext, null, null, null, null, null, null, shouldFallBackToRequestInitiatorContext); } internal ResolutionContext BeginNewScopeContext([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 1 })] ReadOnlyKeyValue<object, ParameterExpression> scopeParameter) { ScopeNames.PushBack(scopeParameter.Key); return Clone(new Tree<ParameterExpression>(), new ExpandableArray<Expression>(), null, null, new Tree<Expression>(), ScopeNames, scopeParameter.Value, null, null, null, null, null, null, null, null); } internal ResolutionContext BeginSubGraph() { return Clone(new Tree<ParameterExpression>(), new ExpandableArray<Expression>(), null, null, new Tree<Expression>(), null, null, null, null, null, null, null, null, null, null); } internal ResolutionContext BeginUnknownTypeCheckDisabledContext() { if (!UnknownTypeCheckDisabled) { bool? unknownTypeCheckDisabled = true; return Clone(null, null, null, null, null, null, null, null, null, null, null, null, null, unknownTypeCheckDisabled, null); } return this; } internal ResolutionContext BeginContextWithFunctionParameters(ParameterExpression[] parameterExpressions) { ExpandableArray<Pair<bool, ParameterExpression>[]> parameterExpressions2 = new ExpandableArray<Pair<bool, ParameterExpression>[]>(ParameterExpressions) { parameterExpressions.AsParameterPairs() }; bool? perResolutionRequestCacheEnabled = false; return Clone(null, null, null, null, null, null, null, null, parameterExpressions2, null, null, null, perResolutionRequestCacheEnabled, null, null); } internal ResolutionContext BeginDecoratingContext(Type decoratingType, IEnumerable<ServiceRegistration> serviceRegistrations) { Stashbox.Utils.Data.Stack<ServiceRegistration> stack = new Stashbox.Utils.Data.Stack<ServiceRegistration>(serviceRegistrations); ServiceRegistration item = stack.Pop(); ExpandableArray<Type, Stashbox.Utils.Data.Stack<ServiceRegistration>> expandableArray = new ExpandableArray<Type, Stashbox.Utils.Data.Stack<ServiceRegistration>>(RemainingDecorators); expandableArray.AddOrUpdate(decoratingType, stack); ExpandableArray<ServiceRegistration> currentDecorators = new ExpandableArray<ServiceRegistration>(CurrentDecorators) { item }; return Clone(null, null, expandableArray, currentDecorators, null, null, null, null, null, null, null, null, null, null, null); } internal ResolutionContext BeginLifetimeValidationContext(int lifeSpan, string currentlyLifeSpanValidatingService) { int? currentLifeSpan = lifeSpan; return Clone(null, null, null, null, null, null, null, null, null, currentlyLifeSpanValidatingService, currentLifeSpan, null, null, null, null); } private static HashTree<object, ConstantExpression> ProcessDependencyOverrides([System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] object[] dependencyOverrides, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1 })] ImmutableTree<object, object> knownInstances) { HashTree<object, ConstantExpression> hashTree = new HashTree<object, ConstantExpression>(); if (knownInstances != null && !knownInstances.IsEmpty) { foreach (ReadOnlyKeyValue<object, object> item in knownInstances.Walk()) { hashTree.Add(item.Key, item.Value.AsConstant(), false); } } if (dependencyOverrides == null) return hashTree; foreach (object obj in dependencyOverrides) { Type type = obj.GetType(); ConstantExpression value = obj.AsConstant(); hashTree.Add(type, value, false); foreach (Type item2 in type.GetRegisterableInterfaceTypes().Concat(type.GetRegisterableBaseTypes())) { hashTree.Add(item2, value, false); } } return hashTree; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] private ResolutionContext Clone([System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] Tree<ParameterExpression> definedVariables = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] ExpandableArray<Expression> singleInstructions = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1, 1 })] ExpandableArray<Type, Stashbox.Utils.Data.Stack<ServiceRegistration>> remainingDecorators = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] ExpandableArray<ServiceRegistration> currentDecorators = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] Tree<Expression> cachedExpressions = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1 })] Stashbox.Utils.Data.Stack<object> scopeNames = null, ParameterExpression currentScopeParameter = null, IContainerContext currentContainerContext = null, [System.Runtime.CompilerServices.Nullable(new byte[] { 2, 1, 1, 1 })] ExpandableArray<Pair<bool, ParameterExpression>[]> parameterExpressions = null, string nameOfServiceLifeSpanValidatingAgainst = null, int? currentLifeSpan = default(int?), bool? isTopRequest = default(bool?), bool? perResolutionRequestCacheEnabled = default(bool?), bool? unknownTypeCheckDisabled = default(bool?), bool? shouldFallBackToRequestInitiatorContext = default(bool?)) { return new ResolutionContext(RequestConfiguration, definedVariables ?? DefinedVariables, singleInstructions ?? SingleInstructions, remainingDecorators ?? RemainingDecorators, currentDecorators ?? CurrentDecorators, CircularDependencyBarrier, cachedExpressions ?? expressionCache, FactoryCache, scopeNames ?? ScopeNames, currentScopeParameter ?? CurrentScopeParameter, RequestContextParameter, currentContainerContext ?? CurrentContainerContext, RequestInitiatorContainerContext, ExpressionOverrides, parameterExpressions ?? ParameterExpressions, NullResultAllowed, IsRequestedFromRoot, isTopRequest ?? IsTopRequest, nameOfServiceLifeSpanValidatingAgainst ?? NameOfServiceLifeSpanValidatingAgainst, currentLifeSpan ?? CurrentLifeSpan, perResolutionRequestCacheEnabled ?? PerResolutionRequestCacheEnabled, unknownTypeCheckDisabled ?? UnknownTypeCheckDisabled, shouldFallBackToRequestInitiatorContext ?? ShouldFallBackToRequestInitiatorContext); } } }