Skip to content

Commit c86f27c

Browse files
committed
Added restriction on adding policies to Container
1 parent e55b8c9 commit c86f27c

10 files changed

+267
-35
lines changed

src/DefaultElement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
using Unity;
4+
using Unity.Interception;
45
using Unity.Interception.ContainerIntegration;
56
using Unity.Interception.Interceptors;
67
using Unity.Interception.Interceptors.InstanceInterceptors;

src/DefaultInterceptionBehavior.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using Unity.Configuration;
3+
using Unity.Interception.ContainerIntegration.ObjectBuilder;
4+
using Unity.Interception.InterceptionBehaviors;
5+
6+
namespace Unity.Interception.ContainerIntegration
7+
{
8+
/// <summary>
9+
/// An injection member that lets you specify behaviors that should
10+
/// apply to all instances of a type in the container regardless
11+
/// of what name it's resolved under.
12+
/// </summary>
13+
public class DefaultInterceptionBehavior : InterceptionBehaviorBase, IDefaultPolicy
14+
{
15+
/// <summary>
16+
/// Create a new <see cref="DefaultInterceptionBehavior"/> that will
17+
/// supply the given interception behavior to the container.
18+
/// </summary>
19+
/// <param name="interceptionBehavior">Behavior to apply to this type.</param>
20+
public DefaultInterceptionBehavior(IInterceptionBehavior interceptionBehavior)
21+
: base(interceptionBehavior)
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Create a new <see cref="DefaultInterceptionBehavior"/> that will
27+
/// resolve the given type/name pair to get the behavior.
28+
/// </summary>
29+
/// <param name="behaviorType">Type of behavior.</param>
30+
/// <param name="name">Name for behavior registration.</param>
31+
public DefaultInterceptionBehavior(Type behaviorType, string name)
32+
: base(behaviorType, name)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Create a new <see cref="DefaultInterceptionBehavior"/> that will
38+
/// resolve the given type to get the behavior.
39+
/// </summary>
40+
/// <param name="behaviorType">Type of behavior.</param>
41+
public DefaultInterceptionBehavior(Type behaviorType)
42+
: base(behaviorType)
43+
{
44+
}
45+
46+
/// <summary>
47+
/// GetOrDefault the list of behaviors for the current type so that it can be added to.
48+
/// </summary>
49+
/// <param name="policies">Policy list.</param>
50+
/// <returns>An instance of <see cref="InterceptionBehaviorsPolicy"/>.</returns>
51+
protected override InterceptionBehaviorsPolicy GetBehaviorsPolicy<TPolicySet>(ref TPolicySet policies)
52+
{
53+
var policy = policies.Get(typeof(IInterceptionBehaviorsPolicy));
54+
if (!(policy is InterceptionBehaviorsPolicy))
55+
{
56+
policy = new InterceptionBehaviorsPolicy();
57+
policies.Set(typeof(IInterceptionBehaviorsPolicy), policy);
58+
}
59+
return (InterceptionBehaviorsPolicy)policy;
60+
}
61+
}
62+
63+
/// <summary>
64+
/// A generic version of <see cref="DefaultInterceptionBehavior"/> so you
65+
/// can give the behavior type using generic syntax.
66+
/// </summary>
67+
/// <typeparam name="TBehavior">Type of the behavior object to apply.</typeparam>
68+
public class DefaultInterceptionBehavior<TBehavior> : DefaultInterceptionBehavior
69+
where TBehavior : IInterceptionBehavior
70+
{
71+
/// <summary>
72+
/// Construct a new <see cref="DefaultInterceptionBehavior{TBehavior}"/> instance
73+
/// that use the given type and name to resolve the behavior object.
74+
/// </summary>
75+
/// <param name="name">Name of the registration.</param>
76+
public DefaultInterceptionBehavior(string name)
77+
: base(typeof(TBehavior), name)
78+
{
79+
}
80+
81+
/// <summary>
82+
/// Construct a new <see cref="DefaultInterceptionBehavior{TBehavior}"/> instance
83+
/// that uses the given type to resolve the behavior object.
84+
/// </summary>
85+
public DefaultInterceptionBehavior()
86+
: base(typeof(TBehavior))
87+
{
88+
}
89+
}
90+
}

src/DefaultInterceptor.cs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using Unity.Builder;
3+
using Unity.Configuration;
4+
using Unity.Injection;
5+
using Unity.Interception.ContainerIntegration.ObjectBuilder;
6+
using Unity.Interception.Interceptors;
7+
using Unity.Interception.Interceptors.InstanceInterceptors;
8+
using Unity.Interception.Interceptors.TypeInterceptors;
9+
using Unity.Policy;
10+
11+
namespace Unity.Interception.Configuration
12+
{
13+
/// <summary>
14+
/// A <see cref="InjectionMember"/> that can be passed to the
15+
/// <see cref="IUnityContainer.RegisterType"/> method to specify
16+
/// which interceptor to use. This member sets up the default
17+
/// interceptor for a type - this will be used regardless of which
18+
/// name is used to resolve the type.
19+
/// </summary>
20+
internal class DefaultInterceptor : InjectionMember, IDefaultPolicy
21+
{
22+
private readonly IInterceptor _interceptor;
23+
private readonly NamedTypeBuildKey _interceptorKey;
24+
25+
/// <summary>
26+
/// Construct a new <see cref="DefaultInterceptor"/> instance that,
27+
/// when applied to a container, will register the given
28+
/// interceptor as the default one.
29+
/// </summary>
30+
/// <param name="interceptor">Interceptor to use.</param>
31+
public DefaultInterceptor(IInterceptor interceptor)
32+
{
33+
_interceptor = interceptor ?? throw new ArgumentNullException(nameof(interceptor));
34+
}
35+
36+
/// <summary>
37+
/// Construct a new <see cref="DefaultInterceptor"/> that, when
38+
/// applied to a container, will register the given type as
39+
/// the default interceptor.
40+
/// </summary>
41+
/// <param name="interceptorType">Type of interceptor.</param>
42+
/// <param name="name">Name to use to resolve the interceptor.</param>
43+
public DefaultInterceptor(Type interceptorType, string name)
44+
{
45+
if (null == interceptorType) throw new ArgumentNullException(nameof(interceptorType));
46+
//Guard.TypeIsAssignable(typeof(IInterceptor), interceptorType, "interceptorType");
47+
48+
_interceptorKey = new NamedTypeBuildKey(interceptorType, name);
49+
}
50+
51+
/// <summary>
52+
/// Construct a new <see cref="DefaultInterceptor"/> that, when
53+
/// applied to a container, will register the given type as
54+
/// the default interceptor.
55+
/// </summary>
56+
/// <param name="interceptorType">Type of interceptor.</param>
57+
public DefaultInterceptor(Type interceptorType)
58+
: this(interceptorType, null)
59+
{
60+
}
61+
62+
/// <summary>
63+
/// Add policies to the <paramref name="policies"/> to configure the
64+
/// container to call this constructor with the appropriate parameter values.
65+
/// </summary>
66+
/// <param name="registeredType">Type of interface being registered. If no interface,
67+
/// this will be null.</param>
68+
/// <param name="mappedToType">Type of concrete type being registered.</param>
69+
/// <param name="name">Name used to resolve the type object.</param>
70+
/// <param name="policies">Policy list to add policies to.</param>
71+
public override void AddPolicies<TContext, TPolicySet>(Type registeredType, Type mappedToType, string name, ref TPolicySet policies)
72+
{
73+
if (IsInstanceInterceptor)
74+
AddDefaultInstanceInterceptor(ref policies);
75+
else
76+
AddDefaultTypeInterceptor(ref policies);
77+
}
78+
79+
private bool IsInstanceInterceptor
80+
{
81+
get
82+
{
83+
if (_interceptor != null)
84+
{
85+
return _interceptor is IInstanceInterceptor;
86+
}
87+
return typeof(IInstanceInterceptor).IsAssignableFrom(_interceptorKey.Type);
88+
}
89+
}
90+
91+
private void AddDefaultInstanceInterceptor<TPolicySet>(ref TPolicySet policies)
92+
where TPolicySet : IPolicySet
93+
{
94+
IInstanceInterceptionPolicy policy;
95+
96+
if (_interceptor != null)
97+
{
98+
policy = new FixedInstanceInterceptionPolicy((IInstanceInterceptor)_interceptor);
99+
}
100+
else
101+
{
102+
policy = new ResolvedInstanceInterceptionPolicy(_interceptorKey);
103+
}
104+
105+
policies.Set(typeof(IInstanceInterceptionPolicy), policy);
106+
}
107+
108+
private void AddDefaultTypeInterceptor<TPolicySet>(ref TPolicySet policies)
109+
where TPolicySet : IPolicySet
110+
{
111+
ITypeInterceptionPolicy policy;
112+
113+
if (_interceptor != null)
114+
{
115+
policy = new FixedTypeInterceptionPolicy((ITypeInterceptor)_interceptor);
116+
}
117+
else
118+
{
119+
policy = new ResolvedTypeInterceptionPolicy(_interceptorKey);
120+
}
121+
122+
policies.Set(typeof(ITypeInterceptionPolicy), policy);
123+
}
124+
}
125+
126+
/// <summary>
127+
/// A generic version of <see cref="DefaultInterceptor"/> so that
128+
/// you can specify the interceptor type using generics.
129+
/// </summary>
130+
/// <typeparam name="TInterceptor"></typeparam>
131+
internal class DefaultInterceptor<TInterceptor> : DefaultInterceptor
132+
where TInterceptor : ITypeInterceptor
133+
{
134+
/// <summary>
135+
/// Create a new instance of <see cref="DefaultInterceptor{TInterceptor}"/>.
136+
/// </summary>
137+
/// <param name="name">Name to use when resolving interceptor.</param>
138+
public DefaultInterceptor(string name)
139+
: base(typeof(TInterceptor), name)
140+
{
141+
}
142+
143+
/// <summary>
144+
/// Create a new instance of <see cref="DefaultInterceptor{TInterceptor}"/>.
145+
/// </summary>
146+
public DefaultInterceptor()
147+
: base(typeof(TInterceptor))
148+
{
149+
}
150+
}
151+
}

src/InterceptionConfigurationExtension.Desktop.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2-
3-
using Microsoft.Practices.Unity.Configuration;
4-
using Unity.Interception.ContainerIntegration;
1+
using Microsoft.Practices.Unity.Configuration;
2+
using Unity.Interception;
53
using Unity.Interception.InterceptionBehaviors;
64
using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception;
75
using Unity.Interception.Interceptors.InstanceInterceptors.TransparentProxyInterception;

src/InterceptionConfigurationExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33
using Microsoft.Practices.Unity.Configuration;
4-
using Unity.Interception.ContainerIntegration;
4+
using Unity.Interception;
55
using Unity.Interception.InterceptionBehaviors;
66
using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception;
77
using Unity.Interception.Interceptors.TypeInterceptors.VirtualMethodInterception;

src/InterceptorElement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Unity.Interception.Configuration.Properties;
1212
using Unity.Interception.ContainerIntegration;
1313
using Unity.Interception.Interceptors;
14+
using DefaultInterceptor = Unity.Interception.Configuration.DefaultInterceptor;
1415

1516
namespace Microsoft.Practices.Unity.InterceptionExtension.Configuration
1617
{

src/KeyElement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Practices.Unity.Configuration;
77
using Microsoft.Practices.Unity.Configuration.ConfigurationHelpers;
88
using Unity;
9+
using Unity.Interception;
910
using Unity.Interception.ContainerIntegration;
1011
using Unity.Interception.Interceptors;
1112
using Unity.Interception.Interceptors.InstanceInterceptors;

src/PolicyElement.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-

2-
3-
using System;
1+
using System;
42
using System.Configuration;
53
using System.Xml;
64
using Microsoft.Practices.Unity.Configuration.ConfigurationHelpers;
75
using Unity;
6+
using Unity.Interception;
87
using Unity.Interception.ContainerIntegration;
98

109
namespace Microsoft.Practices.Unity.InterceptionExtension.Configuration

tests/When_ConfiguringContainerForInterception.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-

2-
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Runtime.Remoting;
7-
using Microsoft.Practices.Unity.Configuration;
1+
using Microsoft.Practices.Unity.Configuration;
82
using Microsoft.Practices.Unity.InterceptionExtension.Configuration.Tests.ConfigFiles;
93
using Microsoft.Practices.Unity.InterceptionExtension.Configuration.Tests.TestObjects;
104
using Microsoft.Practices.Unity.TestSupport;
115
using Microsoft.Practices.Unity.TestSupport.Configuration;
126
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using System;
8+
using System.Linq;
9+
using System.Runtime.Remoting;
1310
using Unity;
14-
using Unity.Interception.ContainerIntegration;
11+
using Unity.Interception;
1512
using Unity.Interception.InterceptionBehaviors;
16-
using Unity.Interception.Interceptors.TypeInterceptors.VirtualMethodInterception;
1713

1814
namespace Microsoft.Practices.Unity.InterceptionExtension.Configuration.Tests
1915
{
@@ -108,12 +104,11 @@ public void Then_CanConfigureBehaviorWithNameOnly()
108104
[TestMethod]
109105
public void Then_CanConfigureDefaultInterceptor()
110106
{
111-
IUnityContainer container = this.ConfiguredContainer("configuringDefaultInterceptor")
112-
.Configure<Interception>()
113-
.AddPolicy("all")
114-
.AddMatchingRule<AlwaysMatchingRule>()
115-
.AddCallHandler<GlobalCountCallHandler>()
116-
.Container;
107+
IUnityContainer container = this.ConfiguredContainer("configuringDefaultInterceptor");
108+
container.Configure<Interception>()
109+
.AddPolicy("all")
110+
.AddMatchingRule<AlwaysMatchingRule>()
111+
.AddCallHandler<GlobalCountCallHandler>();
117112

118113
var instance1 = container.Resolve<Wrappable>();
119114
var instance2 = container.Resolve<Wrappable>("two");

tests/When_ConfiguringInterceptorsThroughContainerElementExtension.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-

2-
3-
using System.Runtime.Remoting;
4-
using Microsoft.Practices.Unity.Configuration;
1+
using Microsoft.Practices.Unity.Configuration;
52
using Microsoft.Practices.Unity.InterceptionExtension.Configuration.Tests.ConfigFiles;
63
using Microsoft.Practices.Unity.InterceptionExtension.Configuration.Tests.TestObjects;
74
using Microsoft.Practices.Unity.TestSupport;
85
using Microsoft.Practices.Unity.TestSupport.Configuration;
96
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using System.Runtime.Remoting;
108
using Unity;
11-
using Unity.Interception.ContainerIntegration;
9+
using Unity.Interception;
1210

1311
namespace Microsoft.Practices.Unity.InterceptionExtension.Configuration.Tests
1412
{
@@ -97,14 +95,12 @@ public void Then_CanConfigureSeveralInterceptors()
9795
[TestMethod]
9896
public void Then_CanMixDefaultAndNonDefaultInterceptors()
9997
{
100-
IUnityContainer container = this.GetContainer("mixingDefaultAndNonDefaultInterceptors");
98+
IUnityContainer container = GetContainer("mixingDefaultAndNonDefaultInterceptors");
10199

102-
var anonymousWrappable = container.Resolve<Wrappable>();
103-
var namedWrappable = container.Resolve<Wrappable>("name");
104-
var anonymousWrappableWithProperty
105-
= container.Resolve<WrappableWithProperty>();
106-
var namedWrappableWithProperty
107-
= container.Resolve<WrappableWithProperty>("name");
100+
var anonymousWrappable = container.Resolve<Wrappable>();
101+
var namedWrappable = container.Resolve<Wrappable>("name");
102+
var anonymousWrappableWithProperty = container.Resolve<WrappableWithProperty>();
103+
var namedWrappableWithProperty = container.Resolve<WrappableWithProperty>("name");
108104

109105
Assert.IsTrue(RemotingServices.IsTransparentProxy(anonymousWrappable));
110106
Assert.IsTrue(RemotingServices.IsTransparentProxy(namedWrappable));

0 commit comments

Comments
 (0)