forked from unitycontainer/interception
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterception.cs
More file actions
260 lines (230 loc) · 12.6 KB
/
Copy pathInterception.cs
File metadata and controls
260 lines (230 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Globalization;
using Unity.Builder;
using Unity.Extension;
using Unity.Interception.ContainerIntegration.ObjectBuilder;
using Unity.Interception.Interceptors;
using Unity.Interception.Interceptors.InstanceInterceptors;
using Unity.Interception.Interceptors.TypeInterceptors;
using Unity.Interception.PolicyInjection;
using Unity.Interception.PolicyInjection.Policies;
using Unity.Interception.Properties;
using Unity.Interception.Utilities;
namespace Unity.Interception.ContainerIntegration
{
/// <summary>
/// A Unity container extension that allows you to configure
/// whether an object should be intercepted and which mechanism should
/// be used to do it, and also provides a convenient set of methods for
/// configuring injection for <see cref="RuleDrivenPolicy"/> instances.
/// </summary>
/// <seealso cref="Interception.SetDefaultInterceptorFor(Type, IInstanceInterceptor)"/>
/// <seealso cref="Interception.SetDefaultInterceptorFor(Type, ITypeInterceptor)"/>
/// <seealso cref="Interception.SetInterceptorFor(Type, string, IInstanceInterceptor)"/>
/// <seealso cref="Interception.SetInterceptorFor(Type, string, ITypeInterceptor)"/>
/// <seealso cref="Interception.AddPolicy"/>
public class Interception : UnityContainerExtension
{
/// <summary>
/// Initial the container with this extension's functionality.
/// </summary>
protected override void Initialize()
{
Context.Strategies.Add(new InstanceInterceptionStrategy(), UnityBuildStage.Lifetime);
Context.Strategies.Add(new TypeInterceptionStrategy(), UnityBuildStage.PreCreation);
Context.Container.RegisterInstance<InjectionPolicy>(typeof(AttributeDrivenPolicy).AssemblyQualifiedName,
new AttributeDrivenPolicy());
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <param name="typeToIntercept">Type to intercept.</param>
/// <param name="name">Name type is registered under.</param>
/// <param name="interceptor">Interceptor to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor(Type typeToIntercept, string name, ITypeInterceptor interceptor)
{
Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
Guard.ArgumentNotNull(interceptor, "interceptor");
GuardTypeInterceptable(typeToIntercept, interceptor);
var key = new NamedTypeBuildKey(typeToIntercept, name);
var policy = new FixedTypeInterceptionPolicy(interceptor);
Context.Policies.Set(key.Type, key.Name, typeof(ITypeInterceptionPolicy), policy);
// add policy injection behavior if using this configuration API to set the interceptor
var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();
interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make<PolicyInjectionBehavior>());
Context.Policies.Set(key.Type, key.Name, typeof(IInterceptionBehaviorsPolicy), interceptionBehaviorsPolicy);
return this;
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <param name="typeToIntercept">Type to intercept.</param>
/// <param name="interceptor">Interceptor to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor(Type typeToIntercept, ITypeInterceptor interceptor)
{
return SetInterceptorFor(typeToIntercept, null, interceptor);
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <typeparam name="T">Type to intercept</typeparam>
/// <param name="name">Name type is registered under.</param>
/// <param name="interceptor">Interceptor object to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor<T>(string name, ITypeInterceptor interceptor)
{
return SetInterceptorFor(typeof(T), name, interceptor);
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <typeparam name="T">Type to intercept</typeparam>
/// <param name="interceptor">Interceptor object to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor<T>(ITypeInterceptor interceptor)
{
return SetInterceptorFor(typeof(T), null, interceptor);
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <param name="typeToIntercept">Type to intercept.</param>
/// <param name="name">Name type is registered under.</param>
/// <param name="interceptor">Instance interceptor to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor(Type typeToIntercept, string name, IInstanceInterceptor interceptor)
{
Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
Guard.ArgumentNotNull(interceptor, "interceptor");
GuardTypeInterceptable(typeToIntercept, interceptor);
var key = new NamedTypeBuildKey(typeToIntercept, name);
var policy = new FixedInstanceInterceptionPolicy(interceptor);
Context.Policies.Set(key.Type, key.Name, typeof(IInstanceInterceptionPolicy), policy);
// add policy injection behavior if using this configuration API to set the interceptor
var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();
interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make<PolicyInjectionBehavior>());
Context.Policies.Set(key.Type, key.Name, typeof(IInterceptionBehaviorsPolicy), interceptionBehaviorsPolicy);
return this;
}
/// <summary>
/// Set the interceptor for a type, regardless of what name is used to resolve the instances.
/// </summary>
/// <param name="typeToIntercept">Type to intercept</param>
/// <param name="interceptor">Interceptor instance.</param>
/// <returns>This extension object.</returns>
public Interception SetDefaultInterceptorFor(Type typeToIntercept, ITypeInterceptor interceptor)
{
Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
Guard.ArgumentNotNull(interceptor, "interceptor");
GuardTypeInterceptable(typeToIntercept, interceptor);
Context.Policies.Set(typeToIntercept, string.Empty, typeof(ITypeInterceptionPolicy),
new FixedTypeInterceptionPolicy(interceptor));
// add policy injection behavior if using this configuration API to set the interceptor
var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();
interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make<PolicyInjectionBehavior>());
Context.Policies.Set(typeToIntercept, string.Empty,
typeof(IInterceptionBehaviorsPolicy), interceptionBehaviorsPolicy);
return this;
}
/// <summary>
/// Set the interceptor for a type, regardless of what name is used to resolve the instances.
/// </summary>
/// <typeparam name="TTypeToIntercept">Type to intercept</typeparam>
/// <param name="interceptor">Interceptor instance.</param>
/// <returns>This extension object.</returns>
public Interception SetDefaultInterceptorFor<TTypeToIntercept>(ITypeInterceptor interceptor)
{
return SetDefaultInterceptorFor(typeof(TTypeToIntercept), interceptor);
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <param name="typeToIntercept">Type to intercept.</param>
/// <param name="interceptor">Instance interceptor to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor(Type typeToIntercept, IInstanceInterceptor interceptor)
{
return SetInterceptorFor(typeToIntercept, null, interceptor);
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <typeparam name="T">Type to intercept.</typeparam>
/// <param name="name">Name type is registered under.</param>
/// <param name="interceptor">Instance interceptor to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor<T>(string name, IInstanceInterceptor interceptor)
{
return SetInterceptorFor(typeof(T), name, interceptor);
}
/// <summary>
/// API to configure interception for a type.
/// </summary>
/// <typeparam name="T">Type to intercept.</typeparam>
/// <param name="interceptor">Instance interceptor to use.</param>
/// <returns>This extension object.</returns>
public Interception SetInterceptorFor<T>(IInstanceInterceptor interceptor)
{
return SetInterceptorFor(typeof(T), null, interceptor);
}
/// <summary>
/// API to configure the default interception settings for a type.
/// </summary>
/// <param name="typeToIntercept">Type the interception is being configured for.</param>
/// <param name="interceptor">The interceptor to use by default.</param>
/// <returns>This extension object.</returns>
public Interception SetDefaultInterceptorFor(Type typeToIntercept, IInstanceInterceptor interceptor)
{
Guard.ArgumentNotNull(typeToIntercept, "typeToIntercept");
Guard.ArgumentNotNull(interceptor, "interceptor");
GuardTypeInterceptable(typeToIntercept, interceptor);
Context.Policies.Set(typeToIntercept, string.Empty, typeof(IInstanceInterceptionPolicy), new FixedInstanceInterceptionPolicy(interceptor));
// add policy injection behavior if using this configuration API to set the interceptor
var interceptionBehaviorsPolicy = new InterceptionBehaviorsPolicy();
interceptionBehaviorsPolicy.AddBehaviorKey(NamedTypeBuildKey.Make<PolicyInjectionBehavior>());
Context.Policies.Set(typeToIntercept, string.Empty, typeof(IInterceptionBehaviorsPolicy), interceptionBehaviorsPolicy);
return this;
}
/// <summary>
/// API to configure the default interception settings for a type.
/// </summary>
/// <typeparam name="TTypeToIntercept">Type the interception is being configured for.</typeparam>
/// <param name="interceptor">The interceptor to use by default.</param>
/// <returns>This extension object.</returns>
public Interception SetDefaultInterceptorFor<TTypeToIntercept>(IInstanceInterceptor interceptor)
{
return SetDefaultInterceptorFor(typeof(TTypeToIntercept), interceptor);
}
private static void GuardTypeInterceptable(Type typeToIntercept, IInterceptor interceptor)
{
if (!interceptor.CanIntercept(typeToIntercept))
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.InterceptionNotSupported,
typeToIntercept.FullName),
nameof(typeToIntercept));
}
}
/// <summary>
/// Starts the definition of a new <see cref="RuleDrivenPolicy"/>.
/// </summary>
/// <param name="policyName">The policy name.</param>
/// <returns></returns>
/// <remarks>This is a convenient way for defining a new policy and the <see cref="IMatchingRule"/>
/// instances and <see cref="ICallHandler"/> instances that are required by a policy.
/// <para/>
/// This mechanism is just a shortcut for what can be natively expressed by wiring up together objects
/// with repeated calls to the <see cref="IUnityContainer.RegisterType"/> method.
/// </remarks>
public PolicyDefinition AddPolicy(string policyName)
{
Guard.ArgumentNotNullOrEmpty(policyName, "policyName");
return new PolicyDefinition(policyName, this);
}
}
}