Skip to content

Commit 1dd2d76

Browse files
committed
Fixing #7
1 parent 6d34a95 commit 1dd2d76

9 files changed

Lines changed: 40 additions & 55 deletions

src/ContainerIntegration/AdditionalInterface.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public AdditionalInterface(Type additionalInterface)
5151
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
5252
{
5353
AdditionalInterfacesPolicy policy =
54-
AdditionalInterfacesPolicy.GetOrCreate(policies, implementationType, name);
54+
AdditionalInterfacesPolicy.GetOrCreate(policies, serviceType, name);
5555
policy.AddAdditionalInterface(_additionalInterface);
5656
}
5757
}

src/ContainerIntegration/DefaultInterceptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public override void AddPolicies(Type serviceType, Type implementationType, stri
7474
{
7575
if (IsInstanceInterceptor)
7676
{
77-
AddDefaultInstanceInterceptor(implementationType, policies);
77+
AddDefaultInstanceInterceptor(serviceType, policies);
7878
}
7979
else
8080
{
81-
AddDefaultTypeInterceptor(implementationType, policies);
81+
AddDefaultTypeInterceptor(serviceType, policies);
8282
}
8383
}
8484

src/ContainerIntegration/InterceptionBehaviorBase.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,33 +64,23 @@ public override void AddPolicies(Type serviceType, Type implementationType, stri
6464
{
6565
if (_explicitBehavior != null)
6666
{
67-
AddExplicitBehaviorPolicies(implementationType, name, policies);
67+
var lifetimeManager = new ContainerControlledLifetimeManager();
68+
lifetimeManager.SetValue(_explicitBehavior);
69+
var behaviorName = Guid.NewGuid().ToString();
70+
var newBehaviorKey = new NamedTypeBuildKey(_explicitBehavior.GetType(), behaviorName);
71+
72+
policies.Set<ILifetimePolicy>(lifetimeManager, newBehaviorKey);
73+
74+
InterceptionBehaviorsPolicy behaviorsPolicy = GetBehaviorsPolicy(policies, serviceType, name);
75+
behaviorsPolicy.AddBehaviorKey(newBehaviorKey);
6876
}
6977
else
7078
{
71-
AddKeyedPolicies(implementationType, name, policies);
79+
var behaviorsPolicy = GetBehaviorsPolicy(policies, serviceType, name);
80+
behaviorsPolicy.AddBehaviorKey(_behaviorKey);
7281
}
7382
}
7483

75-
private void AddExplicitBehaviorPolicies(Type implementationType, string name, IPolicyList policies)
76-
{
77-
var lifetimeManager = new ContainerControlledLifetimeManager();
78-
lifetimeManager.SetValue(_explicitBehavior);
79-
var behaviorName = Guid.NewGuid().ToString();
80-
var newBehaviorKey = new NamedTypeBuildKey(_explicitBehavior.GetType(), behaviorName);
81-
82-
policies.Set<ILifetimePolicy>(lifetimeManager, newBehaviorKey);
83-
84-
InterceptionBehaviorsPolicy behaviorsPolicy = GetBehaviorsPolicy(policies, implementationType, name);
85-
behaviorsPolicy.AddBehaviorKey(newBehaviorKey);
86-
}
87-
88-
private void AddKeyedPolicies(Type implementationType, string name, IPolicyList policies)
89-
{
90-
var behaviorsPolicy = GetBehaviorsPolicy(policies, implementationType, name);
91-
behaviorsPolicy.AddBehaviorKey(_behaviorKey);
92-
}
93-
9484
/// <summary>
9585
/// GetOrDefault the list of behaviors for the current type so that it can be added to.
9686
/// </summary>

src/ContainerIntegration/Interceptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Interceptor(Type interceptorType)
6666
/// <param name="policies">Policy list to add policies to.</param>
6767
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
6868
{
69-
var key = new NamedTypeBuildKey(implementationType, name);
69+
var key = new NamedTypeBuildKey(serviceType, name);
7070
if (IsInstanceInterceptor)
7171
{
7272
var policy = CreateInstanceInterceptionPolicy();

src/ContainerIntegration/ObjectBuilder/InstanceInterceptionStrategy.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ public override void PostBuildUp(IBuilderContext context)
7575
private static T FindInterceptionPolicy<T>(IBuilderContext context, bool probeOriginalKey)
7676
where T : class, IBuilderPolicy
7777
{
78-
// First, try for a match against the current build key
79-
Type currentType = context.BuildKey.Type;
80-
var policy = context.Policies.Get<T>(context.BuildKey) ??
81-
context.Policies.Get<T>(currentType);
78+
// First, try for an original build key
79+
var policy = context.Policies.Get<T>(context.OriginalBuildKey) ??
80+
context.Policies.Get<T>(context.OriginalBuildKey.Type);
8281

8382
if (policy != null)
8483
{
@@ -90,10 +89,9 @@ private static T FindInterceptionPolicy<T>(IBuilderContext context, bool probeOr
9089
return null;
9190
}
9291

93-
// Next, try the original build key
94-
Type originalType = context.OriginalBuildKey.Type;
95-
policy = context.Policies.Get<T>(context.OriginalBuildKey) ??
96-
context.Policies.Get<T>(originalType);
92+
// Next, try the build type
93+
policy = context.Policies.Get<T>(context.BuildKey) ??
94+
context.Policies.Get<T>(context.BuildKey.Type);
9795

9896
return policy;
9997
}

src/ContainerIntegration/ObjectBuilder/TypeInterceptionStrategy.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public override void PreBuildUp(IBuilderContext context)
7070
var enumerable = interceptionBehaviors as IInterceptionBehavior[] ?? interceptionBehaviors.ToArray();
7171
context.Policies.Set(
7272
new EffectiveInterceptionBehaviorsPolicy { Behaviors = enumerable },
73-
context.BuildKey);
73+
context.OriginalBuildKey);
7474

7575
Type[] allAdditionalInterfaces =
7676
Intercept.GetAllAdditionalInterfaces(enumerable, additionalInterfaces);
@@ -98,7 +98,7 @@ public override void PostBuildUp(IBuilderContext context)
9898
}
9999

100100
EffectiveInterceptionBehaviorsPolicy effectiveInterceptionBehaviorsPolicy =
101-
context.Policies.GetNoDefault<EffectiveInterceptionBehaviorsPolicy>(context.BuildKey);
101+
context.Policies.GetNoDefault<EffectiveInterceptionBehaviorsPolicy>(context.OriginalBuildKey);
102102
if (effectiveInterceptionBehaviorsPolicy == null)
103103
{
104104
return;
@@ -113,8 +113,8 @@ public override void PostBuildUp(IBuilderContext context)
113113
private static TPolicy FindInterceptionPolicy<TPolicy>(IBuilderContext context)
114114
where TPolicy : class, IBuilderPolicy
115115
{
116-
return context.Policies.Get<TPolicy>(context.BuildKey) ??
117-
context.Policies.Get<TPolicy>(context.BuildKey.Type);
116+
return context.Policies.Get<TPolicy>(context.OriginalBuildKey) ??
117+
context.Policies.Get<TPolicy>(context.OriginalBuildKey.Type);
118118
}
119119

120120
private class EffectiveInterceptionBehaviorsPolicy : IBuilderPolicy
@@ -167,25 +167,23 @@ private static SelectedConstructor FindNewConstructor(SelectedConstructor origin
167167

168168
public static void SetPolicyForInterceptingType(IBuilderContext context, Type interceptingType)
169169
{
170-
IPolicyList selectorPolicyDestination;
171-
var currentSelectorPolicy = context.Policies.Get<IConstructorSelectorPolicy>(context.BuildKey, out selectorPolicyDestination);
172-
var currentDerivedTypeSelectorPolicy = currentSelectorPolicy as DerivedTypeConstructorSelectorPolicy;
170+
var currentSelectorPolicy = context.Policies.Get<IConstructorSelectorPolicy>(context.OriginalBuildKey, out var selectorPolicyDestination);
173171

174-
if (currentDerivedTypeSelectorPolicy == null)
172+
if (!(currentSelectorPolicy is DerivedTypeConstructorSelectorPolicy currentDerivedTypeSelectorPolicy))
175173
{
176174
selectorPolicyDestination.Set<IConstructorSelectorPolicy>(
177175
new DerivedTypeConstructorSelectorPolicy(
178176
interceptingType,
179177
currentSelectorPolicy),
180-
context.BuildKey);
178+
context.OriginalBuildKey);
181179
}
182180
else if (currentDerivedTypeSelectorPolicy._interceptingType != interceptingType)
183181
{
184182
selectorPolicyDestination.Set<IConstructorSelectorPolicy>(
185183
new DerivedTypeConstructorSelectorPolicy(
186184
interceptingType,
187185
currentDerivedTypeSelectorPolicy._originalConstructorSelectorPolicy),
188-
context.BuildKey);
186+
context.OriginalBuildKey);
189187
}
190188
}
191189
}

tests/InterceptionConfigurationFixture.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ public void CanSetUpAdditionalInterfaceThroughInjectionMemberForInstanceIntercep
6666

6767
int invokeCount = 0;
6868

69-
container.RegisterType<IInterface, BaseClass>(
70-
"test",
71-
new Interceptor<InterfaceInterceptor>(),
72-
new AdditionalInterface(typeof(IOtherInterface)),
73-
new InterceptionBehavior(
74-
new DelegateInterceptionBehavior(
75-
(mi, gn) => { invokeCount++; return mi.CreateMethodReturn(0); })));
69+
container.RegisterType<IInterface, BaseClass>("test",
70+
new Interceptor<InterfaceInterceptor>(),
71+
new AdditionalInterface(typeof(IOtherInterface)),
72+
new InterceptionBehavior(
73+
new DelegateInterceptionBehavior(
74+
(mi, gn) => { invokeCount++; return mi.CreateMethodReturn(0); })));
7675

7776
IInterface instance = container.Resolve<IInterface>("test");
7877

tests/PolicyInjection/PolicyInjectionWithGenericMethodsFixture.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ private static IUnityContainer CreateContainer<TInterceptor>()
5252
{
5353
return new UnityContainer()
5454
.AddNewExtension<Interception>()
55-
.RegisterType<IInterfaceWithGenericMethod, MyClass>(
56-
new Interceptor<TInterceptor>(),
57-
new InterceptionBehavior<PolicyInjectionBehavior>());
55+
.RegisterType<IInterfaceWithGenericMethod, MyClass>(new Interceptor<TInterceptor>(),
56+
new InterceptionBehavior<PolicyInjectionBehavior>());
5857
}
5958

6059
private static void CanInterceptNonGenericMethod<TInterceptor>()

tests/VirtualMethodInterception/ContainerVirtualMethodInterceptionFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ public void ResolvingKeyForTheSecondTimeAfterAddingBehaviorWithRequiredInterface
166166
Assert.IsFalse(container.Resolve<ClassWithVirtualProperty>() is INotifyPropertyChanged);
167167

168168
container
169-
.RegisterType<ClassWithVirtualProperty>(
170-
new InterceptionBehavior(new NaiveINotifyPropertyChangedInterceptionBehavior()));
169+
.RegisterType<ClassWithVirtualProperty>(new Interceptor<VirtualMethodInterceptor>(),
170+
new InterceptionBehavior(
171+
new NaiveINotifyPropertyChangedInterceptionBehavior()));
171172

172173
Assert.IsTrue(container.Resolve<ClassWithVirtualProperty>() is INotifyPropertyChanged);
173174
}

0 commit comments

Comments
 (0)