ResolutionStrategy
using Stashbox.Expressions;
using Stashbox.Lifetime;
using Stashbox.Registration;
using Stashbox.Resolution.Resolvers;
using Stashbox.Resolution.Wrappers;
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.Reflection;
using System.Runtime.CompilerServices;
namespace Stashbox.Resolution
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal class ResolutionStrategy : IResolutionStrategy
{
private readonly ParentContainerResolver parentContainerResolver;
private ImmutableBucket<IResolver> resolverRepository;
public ResolutionStrategy()
{
parentContainerResolver = new ParentContainerResolver();
resolverRepository = new ImmutableBucket<IResolver>(new IResolver[10] {
new EnumerableWrapper(),
new LazyWrapper(),
new FuncWrapper(),
new MetadataWrapper(),
new KeyValueWrapper(),
new ServiceProviderResolver(),
new OptionalValueResolver(),
new DefaultValueResolver(),
parentContainerResolver,
new UnknownTypeResolver()
});
}
public ServiceContext BuildExpressionForType(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
if (typeInformation.Type == TypeCache<IDependencyResolver>.Type)
return resolutionContext.CurrentScopeParameter.AsServiceContext(null);
if (typeInformation.Type == TypeCache<IRequestContext>.Type) {
resolutionContext.RequestConfiguration.RequiresRequestContext = true;
return resolutionContext.RequestContextParameter.AsServiceContext(null);
}
if (typeInformation.IsDependency) {
if (resolutionContext.ParameterExpressions.Length > 0) {
Type type = typeInformation.Type;
int num = resolutionContext.ParameterExpressions.Length;
while (num-- > 0) {
Pair<bool, ParameterExpression>[] array = resolutionContext.ParameterExpressions[num].Where(delegate(Pair<bool, ParameterExpression> p) {
if (!(p.I2.Type == type))
return p.I2.Type.Implements(type);
return true;
}).CastToArray();
if (array.Length != 0) {
object obj = Array.Find(array, (Pair<bool, ParameterExpression> parameter) => !parameter.I1);
if (obj == null) {
Pair<bool, ParameterExpression>[] array2 = array;
obj = array2[array2.Length - 1];
}
((Pair<bool, ParameterExpression>)obj).I1 = true;
return ((Pair<bool, ParameterExpression>)obj).I2.AsServiceContext(null);
}
}
}
Stashbox.Utils.Data.Stack<ServiceRegistration> orDefaultByRef = resolutionContext.RemainingDecorators.GetOrDefaultByRef(typeInformation.Type);
if (orDefaultByRef != null && orDefaultByRef.Length > 0)
return BuildExpressionForDecorator(orDefaultByRef.Front(), resolutionContext.BeginDecoratingContext(typeInformation.Type, orDefaultByRef), typeInformation, orDefaultByRef).AsServiceContext(null);
}
ConstantExpression constantExpression = resolutionContext.ExpressionOverrides?.GetOrDefaultByValue(typeInformation.DependencyName ?? typeInformation.Type);
if (constantExpression != null)
return constantExpression.AsServiceContext(null);
ServiceRegistration serviceRegistration = resolutionContext.ResolutionBehavior.Has(ResolutionBehavior.Current) ? resolutionContext.CurrentContainerContext.RegistrationRepository.GetRegistrationOrDefault(typeInformation, resolutionContext) : null;
bool flag = serviceRegistration != null && serviceRegistration.Options.IsOn(RegistrationOption.IsResolutionCallRequired);
if ((typeInformation.IsDependency && serviceRegistration != null) & flag) {
Expression target = resolutionContext.CurrentScopeParameter.ConvertTo(TypeCache<IDependencyResolver>.Type);
MethodInfo resolveMethod = Constants.ResolveMethod;
Expression[] obj2 = new Expression[4] {
typeInformation.Type.AsConstant(),
typeInformation.DependencyName.AsConstant(),
null,
null
};
HashTree<object, ConstantExpression> expressionOverrides = resolutionContext.ExpressionOverrides;
obj2[2] = (((expressionOverrides != null) ? (from c in expressionOverrides.Walk()
select c.Value).ToArray().AsConstant() : null) ?? TypeCache.EmptyArray<object>().AsConstant());
obj2[3] = resolutionContext.ResolutionBehavior.AsConstant();
return target.CallMethod(resolveMethod, obj2).ConvertTo(typeInformation.Type).AsServiceContext(serviceRegistration);
}
if (serviceRegistration == null)
return BuildExpressionUsingWrappersOrResolvers(resolutionContext, typeInformation);
return BuildExpressionForRegistration(serviceRegistration, resolutionContext, typeInformation);
}
public IEnumerable<ServiceContext> BuildExpressionsForEnumerableRequest(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
IEnumerable<ServiceRegistration> enumerable = resolutionContext.ResolutionBehavior.Has(ResolutionBehavior.Current) ? resolutionContext.CurrentContainerContext.RegistrationRepository.GetRegistrationsOrDefault(typeInformation, resolutionContext) : null;
if (enumerable == null)
return BuildEnumerableExpressionUsingWrappersOrResolvers(resolutionContext, typeInformation);
IEnumerable<ServiceContext> enumerable2 = enumerable.Select(delegate(ServiceRegistration registration) {
Stashbox.Utils.Data.Stack<ServiceRegistration> orDefaultByRef = resolutionContext.RemainingDecorators.GetOrDefaultByRef(typeInformation.Type);
if (orDefaultByRef == null || orDefaultByRef.Length == 0)
return BuildExpressionForRegistration(registration, resolutionContext, typeInformation);
orDefaultByRef.ReplaceBack(registration);
return BuildExpressionForDecorator(orDefaultByRef.Front(), resolutionContext.BeginDecoratingContext(typeInformation.Type, orDefaultByRef), typeInformation, orDefaultByRef).AsServiceContext(registration);
});
if (!parentContainerResolver.CanUseForResolution(typeInformation, resolutionContext))
return enumerable2;
IEnumerable<ServiceContext> expressionsForEnumerableRequest = parentContainerResolver.GetExpressionsForEnumerableRequest(this, typeInformation, resolutionContext);
return enumerable2.Concat(expressionsForEnumerableRequest);
}
public ServiceContext BuildExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
{
OpenGenericRegistration openGenericRegistration = serviceRegistration as OpenGenericRegistration;
if (openGenericRegistration != null) {
Type requestedType = serviceRegistration.ImplementationType.MakeGenericType(typeInformation.Type.GetGenericArguments());
serviceRegistration = openGenericRegistration.ProduceClosedRegistration(requestedType);
}
IContainerContext containerContext = resolutionContext.RequestInitiatorResolutionBehavior.Has(ResolutionBehavior.Current) ? resolutionContext.RequestInitiatorContainerContext : resolutionContext.RequestInitiatorContainerContext.ParentContext;
IEnumerable<ServiceRegistration> enumerable = (resolutionContext.RequestInitiatorResolutionBehavior.Has(ResolutionBehavior.Parent) || resolutionContext.RequestInitiatorResolutionBehavior.Has(ResolutionBehavior.ParentDependency)) ? CollectDecorators(serviceRegistration.ImplementationType, typeInformation, resolutionContext, containerContext) : containerContext?.DecoratorRepository.GetDecoratorsOrDefault(serviceRegistration.ImplementationType, typeInformation, resolutionContext);
if (enumerable == null)
return BuildExpressionAndApplyLifetime(serviceRegistration, resolutionContext, typeInformation, null).AsServiceContext(serviceRegistration);
Stashbox.Utils.Data.Stack<ServiceRegistration> stack = enumerable.AsStack();
stack.PushBack(serviceRegistration);
return BuildExpressionForDecorator(stack.Front(), resolutionContext.BeginDecoratingContext(typeInformation.Type, stack), typeInformation, stack).AsServiceContext(serviceRegistration);
}
public bool IsTypeResolvable(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
if (typeInformation.Type.IsGenericTypeDefinition)
return false;
if (typeInformation.Type == TypeCache<IDependencyResolver>.Type || typeInformation.Type == TypeCache<IServiceProvider>.Type || typeInformation.Type == TypeCache<IRequestContext>.Type)
return true;
if (resolutionContext.CurrentContainerContext.RegistrationRepository.ContainsRegistration(typeInformation.Type, typeInformation.DependencyName, true) || IsWrappedTypeRegistered(typeInformation, resolutionContext))
return true;
if (resolutionContext.ExpressionOverrides?.GetOrDefaultByValue(typeInformation.DependencyName ?? typeInformation.Type) == null)
return CanLookupService(typeInformation, resolutionContext);
return true;
}
public void RegisterResolver(IResolver resolver)
{
Swap.SwapValue<IResolver, byte, byte, byte, ImmutableBucket<IResolver>>(ref resolverRepository, (IResolver t1, byte _, byte _, byte _, ImmutableBucket<IResolver> repo) => repo.Insert(repo.Length - 4, t1), resolver, 0, 0, 0);
}
private ServiceContext BuildExpressionUsingWrappersOrResolvers(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
int length = resolverRepository.Length;
for (int i = 0; i < length; i++) {
IResolver resolver = resolverRepository[i];
IEnumerableWrapper enumerableWrapper = resolver as IEnumerableWrapper;
if (enumerableWrapper != null) {
IEnumerableWrapper enumerableWrapper2 = enumerableWrapper;
if (enumerableWrapper2.TryUnWrap(typeInformation, out TypeInformation unWrappedType))
return enumerableWrapper2.WrapExpression(typeInformation, unWrappedType, BuildExpressionsForEnumerableRequest(resolutionContext, unWrappedType)).AsServiceContext(null);
}
IServiceWrapper serviceWrapper = resolver as IServiceWrapper;
if (serviceWrapper != null) {
IServiceWrapper serviceWrapper2 = serviceWrapper;
if (serviceWrapper2.TryUnWrap(typeInformation, out TypeInformation unWrappedType2)) {
ServiceContext serviceContext = BuildExpressionForType(resolutionContext, unWrappedType2);
if (!serviceContext.IsEmpty())
return serviceWrapper2.WrapExpression(typeInformation, unWrappedType2, serviceContext).AsServiceContext(serviceContext.ServiceRegistration);
return ServiceContext.Empty;
}
}
IParameterizedWrapper parameterizedWrapper = resolver as IParameterizedWrapper;
if (parameterizedWrapper != null) {
IParameterizedWrapper parameterizedWrapper2 = parameterizedWrapper;
if (parameterizedWrapper2.TryUnWrap(typeInformation, out TypeInformation unWrappedType3, out IEnumerable<Type> parameterTypes)) {
ParameterExpression[] parameterExpressions = (from p in parameterTypes
select p.AsParameter(null)).CastToArray();
ServiceContext serviceContext2 = BuildExpressionForType(resolutionContext.BeginContextWithFunctionParameters(parameterExpressions), unWrappedType3);
if (!serviceContext2.IsEmpty())
return parameterizedWrapper2.WrapExpression(typeInformation, unWrappedType3, serviceContext2, parameterExpressions).AsServiceContext(serviceContext2.ServiceRegistration);
return ServiceContext.Empty;
}
}
}
return BuildExpressionUsingResolvers(resolutionContext, typeInformation);
}
private IEnumerable<ServiceContext> BuildEnumerableExpressionUsingWrappersOrResolvers(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
int length = resolverRepository.Length;
for (int i = 0; i < length; i++) {
IResolver resolver = resolverRepository[i];
IResolver resolver2 = resolver;
IServiceWrapper serviceWrapper = resolver2 as IServiceWrapper;
if (serviceWrapper != null) {
IServiceWrapper serviceWrapper2 = serviceWrapper;
if (serviceWrapper2.TryUnWrap(typeInformation, out TypeInformation unWrappedType))
return from serviceContext in BuildExpressionsForEnumerableRequest(resolutionContext, unWrappedType)
select serviceWrapper2.WrapExpression(typeInformation, unWrappedType, serviceContext).AsServiceContext(serviceContext.ServiceRegistration);
}
IParameterizedWrapper parameterizedWrapper = resolver2 as IParameterizedWrapper;
if (parameterizedWrapper != null) {
IParameterizedWrapper parameterizedWrapper2 = parameterizedWrapper;
if (parameterizedWrapper2.TryUnWrap(typeInformation, out TypeInformation unWrappedType2, out IEnumerable<Type> parameterTypes)) {
ParameterExpression[] parameterExpressions = (from p in parameterTypes
select p.AsParameter(null)).CastToArray();
return from serviceContext in BuildExpressionsForEnumerableRequest(resolutionContext.BeginContextWithFunctionParameters(parameterExpressions), unWrappedType2)
select parameterizedWrapper2.WrapExpression(typeInformation, unWrappedType2, serviceContext, parameterExpressions).AsServiceContext(serviceContext.ServiceRegistration);
}
}
}
return BuildEnumerableExpressionsUsingResolvers(resolutionContext, typeInformation);
}
private ServiceContext BuildExpressionUsingResolvers(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
int length = resolverRepository.Length;
for (int i = 0; i < length; i++) {
IServiceResolver serviceResolver = resolverRepository[i] as IServiceResolver;
if (serviceResolver != null && serviceResolver.CanUseForResolution(typeInformation, resolutionContext))
return serviceResolver.GetExpression(this, typeInformation, resolutionContext);
}
return ServiceContext.Empty;
}
private IEnumerable<ServiceContext> BuildEnumerableExpressionsUsingResolvers(ResolutionContext resolutionContext, TypeInformation typeInformation)
{
int length = resolverRepository.Length;
for (int i = 0; i < length; i++) {
IEnumerableSupportedResolver enumerableSupportedResolver = resolverRepository[i] as IEnumerableSupportedResolver;
if (enumerableSupportedResolver != null && enumerableSupportedResolver.CanUseForResolution(typeInformation, resolutionContext))
return enumerableSupportedResolver.GetExpressionsForEnumerableRequest(this, typeInformation, resolutionContext);
}
return TypeCache.EmptyArray<ServiceContext>();
}
private bool IsWrappedTypeRegistered(TypeInformation typeInformation, ResolutionContext resolutionContext)
{
int length = resolverRepository.Length;
int num = 0;
while (num < length) {
IResolver resolver = resolverRepository[num];
IEnumerableWrapper enumerableWrapper = resolver as IEnumerableWrapper;
if (enumerableWrapper == null || !enumerableWrapper.TryUnWrap(typeInformation, out TypeInformation _)) {
IServiceWrapper serviceWrapper = resolver as IServiceWrapper;
TypeInformation unWrappedType2;
if (serviceWrapper == null || !serviceWrapper.TryUnWrap(typeInformation, out unWrappedType2) || !resolutionContext.CurrentContainerContext.RegistrationRepository.ContainsRegistration(unWrappedType2.Type, typeInformation.DependencyName, true)) {
IParameterizedWrapper parameterizedWrapper = resolver as IParameterizedWrapper;
TypeInformation unWrappedType3;
if (parameterizedWrapper == null || !parameterizedWrapper.TryUnWrap(typeInformation, out unWrappedType3, out IEnumerable<Type> _) || !resolutionContext.CurrentContainerContext.RegistrationRepository.ContainsRegistration(unWrappedType3.Type, typeInformation.DependencyName, true)) {
num++;
continue;
}
}
}
return true;
}
return false;
}
private bool CanLookupService(TypeInformation typeInfo, ResolutionContext resolutionContext)
{
int length = resolverRepository.Length;
for (int i = 0; i < length; i++) {
ILookup lookup = resolverRepository[i] as ILookup;
if (lookup != null && lookup.CanLookupService(typeInfo, resolutionContext))
return true;
}
return false;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private static IEnumerable<ServiceRegistration> CollectDecorators(Type implementationType, TypeInformation typeInformation, ResolutionContext resolutionContext, [System.Runtime.CompilerServices.Nullable(2)] IContainerContext decoratorContext)
{
if (decoratorContext == null)
return null;
IEnumerable<ServiceRegistration> enumerable = CollectDecorators(implementationType, typeInformation, resolutionContext, decoratorContext.ParentContext);
if (enumerable == null)
return decoratorContext.DecoratorRepository.GetDecoratorsOrDefault(implementationType, typeInformation, resolutionContext);
IEnumerable<ServiceRegistration> decoratorsOrDefault = decoratorContext.DecoratorRepository.GetDecoratorsOrDefault(implementationType, typeInformation, resolutionContext);
if (decoratorsOrDefault != null)
return enumerable.Concat(decoratorsOrDefault);
return enumerable;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
private static Expression BuildExpressionForDecorator(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, Stashbox.Utils.Data.Stack<ServiceRegistration> decorators)
{
OpenGenericRegistration openGenericRegistration = serviceRegistration as OpenGenericRegistration;
if (openGenericRegistration != null) {
Type requestedType = serviceRegistration.ImplementationType.MakeGenericType(typeInformation.Type.GetGenericArguments());
serviceRegistration = openGenericRegistration.ProduceClosedRegistration(requestedType);
}
return BuildExpressionAndApplyLifetime(serviceRegistration, resolutionContext, typeInformation, decorators.PeekBack()?.Lifetime);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
private static Expression BuildExpressionAndApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, [System.Runtime.CompilerServices.Nullable(2)] LifetimeDescriptor secondaryLifetimeDescriptor = null)
{
LifetimeDescriptor lifetimeDescriptor = (secondaryLifetimeDescriptor != null && serviceRegistration.Lifetime is EmptyLifetime) ? secondaryLifetimeDescriptor : serviceRegistration.Lifetime;
Expression expression = (!IsOutputLifetimeManageable(serviceRegistration)) ? ExpressionBuilder.BuildExpressionForRegistration(serviceRegistration, resolutionContext, typeInformation) : lifetimeDescriptor.ApplyLifetime(serviceRegistration, resolutionContext, typeInformation);
if (!(typeInformation.Type != expression?.Type))
return expression;
if (expression == null)
return null;
return expression.ConvertTo(typeInformation.Type);
}
private static bool IsOutputLifetimeManageable(ServiceRegistration serviceRegistration)
{
if (!(serviceRegistration is OpenGenericRegistration))
return !serviceRegistration.IsInstance();
return false;
}
}
}