Removing references to BuilderContext via interface casting

This commit is contained in:
Eugene Sadovoi 2018-12-20 23:46:30 -05:00
Родитель 26b7142399
Коммит fda51f94ac
3 изменённых файлов: 16 добавлений и 112 удалений

Просмотреть файл

@ -49,7 +49,7 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
IEnumerable<Type> additionalInterfaces =
additionalInterfacesPolicy != null ? additionalInterfacesPolicy.AdditionalInterfaces : Type.EmptyTypes;
Type typeToIntercept = context.Registration.Type;
Type typeToIntercept = context.RegistrationType;
Type implementationType = context.Existing.GetType();
IInterceptionBehavior[] interceptionBehaviors =
@ -73,24 +73,19 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
where T : class
{
// First, try for an original build key
var policy = (T)context.GetOrDefault(typeof(T), context.Registration) ??
(T)context.GetOrDefault(typeof(T), context.Registration.Type);
var policy = GetPolicyOrDefault<T>(ref context, context.RegistrationType, context.RegistrationName);
if (policy != null)
{
return policy;
}
if (policy != null) return policy;
if (!probeOriginalKey)
{
return null;
}
if (!probeOriginalKey) return null;
// Next, try the build type
policy = (T)context.GetOrDefault(typeof(T), context.Type, context.Name) ??
(T)context.GetOrDefault(typeof(T), context.Type);
policy = GetPolicyOrDefault<T>(ref context, context.Type, context.Name);
return policy;
}
}
}

Просмотреть файл

@ -68,7 +68,7 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
additionalInterfacesPolicy != null ? additionalInterfacesPolicy.AdditionalInterfaces : Type.EmptyTypes;
var enumerable = interceptionBehaviors as IInterceptionBehavior[] ?? interceptionBehaviors.ToArray();
((IPolicySet)context.Registration).Set(typeof(EffectiveInterceptionBehaviorsPolicy),
context.Registration.Set(typeof(EffectiveInterceptionBehaviorsPolicy),
new EffectiveInterceptionBehaviorsPolicy { Behaviors = enumerable });
Type[] allAdditionalInterfaces =
@ -97,8 +97,8 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
return;
}
var effectiveInterceptionBehaviorsPolicy = context.Get<EffectiveInterceptionBehaviorsPolicy>(
context.Registration.Type, context.Registration.Name);
var effectiveInterceptionBehaviorsPolicy = (EffectiveInterceptionBehaviorsPolicy)context.Get(
context.RegistrationType, context.RegistrationName, typeof(EffectiveInterceptionBehaviorsPolicy));
if (effectiveInterceptionBehaviorsPolicy == null)
{
@ -114,8 +114,8 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
private static TPolicy FindInterceptionPolicy<TPolicy>(ref BuilderContext context)
{
return (TPolicy)(context.GetOrDefault(typeof(TPolicy), context.Registration) ??
(TPolicy) context.GetOrDefault(typeof(TPolicy), context.Registration.Type));
return GetPolicyOrDefault<TPolicy>(ref context, context.RegistrationType, context.RegistrationName);
}
#endregion
@ -198,17 +198,16 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
public static void SetPolicyForInterceptingType(ref BuilderContext context, Type interceptingType)
{
var currentSelectorPolicy =
(IConstructorSelectorPolicy)context.GetOrDefault(
typeof(IConstructorSelectorPolicy), context.Registration);
GetPolicy<IConstructorSelectorPolicy>(ref context, context.RegistrationType, context.RegistrationName);
if (!(currentSelectorPolicy is DerivedTypeConstructorSelectorPolicy currentDerivedTypeSelectorPolicy))
{
((IPolicySet)context.Registration).Set(typeof(IConstructorSelectorPolicy),
context.Registration.Set(typeof(IConstructorSelectorPolicy),
new DerivedTypeConstructorSelectorPolicy(interceptingType, currentSelectorPolicy));
}
else if (currentDerivedTypeSelectorPolicy.InterceptingType != interceptingType)
{
((IPolicySet)context.Registration).Set(typeof(IConstructorSelectorPolicy),
context.Registration.Set(typeof(IConstructorSelectorPolicy),
new DerivedTypeConstructorSelectorPolicy(interceptingType,
currentDerivedTypeSelectorPolicy.OriginalConstructorSelectorPolicy));
}

Просмотреть файл

@ -1,90 +0,0 @@
using System;
using System.Globalization;
using System.Reflection;
using Unity.Builder;
// ReSharper disable once CheckNamespace
namespace Unity.Policy
{
public static class LegacyPolicyListUtilityExtensions
{
#region Get
/// <summary>
/// Default resolution/search algorithm for retrieving requested policy
/// </summary>
/// <param name="policies"><see cref="IPolicyList"/> to search.</param>
/// <param name="policyInterface">The interface the policy is registered under.</param>
/// <param name="buildKey">The key the policy applies.</param>
/// <returns>The policy in the list, if present; returns null otherwise.</returns>
public static object GetOrDefault(this IPolicyList policies, Type policyInterface, object buildKey)
{
var tuple = ParseBuildKey(buildKey);
return (buildKey != null ? policies.Get(tuple.Item1, tuple.Item2, policyInterface) : null) ??
(tuple.Item1 != null && tuple.Item1.GetTypeInfo().IsGenericType
? Get(policies, policyInterface, ReplaceType(tuple.Item1.GetGenericTypeDefinition())) : null) ??
(tuple.Item1 != null ? policies.Get(tuple.Item1, string.Empty, policyInterface) : null) ??
(tuple.Item1 != null && tuple.Item1.GetTypeInfo().IsGenericType
? policies.Get(tuple.Item1.GetGenericTypeDefinition(), string.Empty, policyInterface) : null) ??
policies.Get(null, null, policyInterface);
object ReplaceType(Type newType)
{
switch (buildKey)
{
case Type _:
return newType;
case INamedType originalKey:
return new NamedTypeBuildKey(newType, originalKey.Name);
default:
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
"Cannot extract type from build key {0}.", buildKey), nameof(buildKey));
}
}
object Get(IPolicyList list, Type policy, object key)
{
var tupleKey = ParseBuildKey(key);
return list.Get(tupleKey.Item1, tupleKey.Item2, policy);
}
Tuple<Type, string> ParseBuildKey(object key)
{
switch (key)
{
case INamedType namedBuildKey:
return new Tuple<Type, string>(namedBuildKey.Type, namedBuildKey.Name);
case Type typeBuildKey:
return new Tuple<Type, string>(typeBuildKey, string.Empty);
case string name:
return new Tuple<Type, string>(null, name);
case null:
return new Tuple<Type, string>(null, null);
default:
return new Tuple<Type, string>(key.GetType(), null);
}
}
}
public static object GetOrDefault(this IPolicyList policies, Type policyInterface, Type type, string name)
{
return policies.Get(type, name, policyInterface) ??
(type != null && type.GetTypeInfo().IsGenericType
? policies.Get(type.GetGenericTypeDefinition(), name, policyInterface) : null) ??
(type != null ? policies.Get(type, string.Empty, policyInterface) : null) ??
(type != null && type.GetTypeInfo().IsGenericType
? policies.Get(type.GetGenericTypeDefinition(), string.Empty, policyInterface) : null) ??
policies.Get(null, null, policyInterface);
}
#endregion
}
}