Skip to content

Commit c1d21f3

Browse files
committed
New code interception policy variants, new extensions
1 parent e3739da commit c1d21f3

7 files changed

+133
-40
lines changed
+19-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1+
using System;
12
using System.Reflection;
23
using Microsoft.Practices.Unity.InterceptionExtension;
34

45
namespace MvcInterception.Unity
56
{
67
public class MethodMatchingRule : IMatchingRule
78
{
8-
public MethodMatchingRule()
9+
public MethodMatchingRule(MethodBase method)
10+
: this(x => x.Equals(method))
911
{
12+
1013
}
1114

12-
public MethodMatchingRule(MethodInfo method)
15+
public MethodMatchingRule(Predicate<MethodBase> method)
1316
{
14-
Method = method;
17+
MethodEqChecker = method;
1518
}
1619

17-
public MethodInfo Method
20+
21+
public static MethodMatchingRule Create(Predicate<MethodBase> methodChecker, MethodBase method)
22+
{
23+
if (methodChecker != null)
24+
return new MethodMatchingRule(methodChecker);
25+
if (method != null)
26+
return new MethodMatchingRule(method);
27+
28+
return null;
29+
}
30+
31+
public Predicate<MethodBase> MethodEqChecker
1832
{
1933
get;
2034
set;
2135
}
2236

2337
public bool Matches(MethodBase member)
2438
{
25-
return member.Equals(Method);
39+
return MethodEqChecker(member);
2640
}
2741
}
2842
}

MvcInterception.Unity/UnityCallInterceptorRegistry.cs

+17-4
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,28 @@ protected Interception InterceptionExtension
3232
}
3333
}
3434

35-
protected override void RegisterMethod(MethodInfo method, Type type, IEnumerable objects)
35+
protected override void RegisterMethod<TObj>(Predicate<MethodBase> methodPred, MethodBase method, IEnumerable objects)
3636
{
37-
var policyName = type.FullName + "." + method.Name + ", " + type.Assembly.FullName;
37+
var type = method == null ? typeof(TObj) : method.ReflectedType;
38+
39+
string mname = null;
40+
41+
if (method != null)
42+
{
43+
mname = "." + method.Name;
44+
}
45+
else if (methodPred != null)
46+
{
47+
mname = "[" + methodPred.Method.ReflectedType.FullName + "." + methodPred.Method.Name + "]";
48+
}
49+
50+
var policyName = type.FullName + mname + ", " + type.Assembly.FullName;
3851

3952
PolicyDefinition policy;
4053

4154
if (!cachePolicy.TryGetValue(policyName, out policy))
4255
{
43-
Interception interceptionExtension = null;
56+
Interception interceptionExtension;
4457

4558
if (transparentProxyInterceptor.CanIntercept(type))
4659
interceptionExtension = InterceptionExtension.SetInterceptorFor(type, transparentProxyInterceptor);
@@ -49,7 +62,7 @@ protected override void RegisterMethod(MethodInfo method, Type type, IEnumerable
4962
else
5063
interceptionExtension = InterceptionExtension.SetInterceptorFor(type, virtualMethodInterceptor);
5164

52-
policy = interceptionExtension.AddPolicy(policyName).AddMatchingRule(new MethodMatchingRule(method));
65+
policy = interceptionExtension.AddPolicy(policyName).AddMatchingRule(MethodMatchingRule.Create(methodPred, method));
5366

5467
cachePolicy.Add(policyName, policy);
5568
}

MvcInterception/CallInterceptorRegistry.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@ public ContainerAdapter Container
1919
set;
2020
}
2121

22-
public ICallInterceptorRegistry RegisterObjects<TObj>(Expression<Action<TObj>> action, params object[] objs)
22+
public ICallInterceptorRegistry RegisterInterceptors<TObj>(Expression<Action<TObj>> action, params object[] objs)
2323
{
2424
var methodCall = action.Body as MethodCallExpression;
2525

26-
RegisterMethod(methodCall.Method, methodCall.Object.Type, ConvertInputObjects(objs));
26+
RegisterMethod<TObj>(null, methodCall.Method, ConvertInputObjects(objs));
27+
28+
return this;
29+
}
30+
31+
public ICallInterceptorRegistry RegisterInterceptors<TObj>(Predicate<MethodBase> methodPred, params object[] objs)
32+
{
33+
RegisterMethod<TObj>(methodPred, null, ConvertInputObjects(objs));
2734

2835
return this;
2936
}
3037

3138
protected abstract IEnumerable ConvertInputObjects(IEnumerable objs);
3239

33-
protected abstract void RegisterMethod(MethodInfo method, Type type, IEnumerable objects);
40+
protected abstract void RegisterMethod<TObj>(Predicate<MethodBase> methodPred, MethodBase method, IEnumerable objects);
3441
}
3542
}

MvcInterception/CallInterceptorRegistryExtensions.cs MvcInterception/CallInterceptorRegistryExpressionExtensions.cs

+11-26
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55

66
namespace MvcInterception
77
{
8-
public static class CallInterceptorRegistryExtensions
8+
public static class CallInterceptorRegistryExpressionExtensions
99
{
1010
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, params ICallInterceptor[] objs)
1111
{
12-
return Register(@this, action, (IEnumerable<ICallInterceptor>)objs);
12+
return @this.Register(action, (IEnumerable<ICallInterceptor>)objs);
1313
}
1414

1515
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, IEnumerable<ICallInterceptor> objs)
1616
{
17-
return @this.RegisterObjects(action, objs.Cast<object>().ToArray());
17+
return @this.RegisterInterceptors(action, objs.Cast<object>().ToArray());
1818
}
1919

2020
public static ICallInterceptorRegistry Register<TObj, T1>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action)
2121
where T1: ICallInterceptor, new()
2222
{
23-
return @this.RegisterObjects(action, typeof(T1));
23+
return @this.RegisterInterceptors(action, typeof(T1));
2424
}
2525

2626
public static ICallInterceptorRegistry Register<TObj, T1, T2>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action)
2727
where T1 : ICallInterceptor, new()
2828
where T2 : ICallInterceptor, new()
2929
{
30-
return @this.RegisterObjects(action, typeof(T1), typeof(T2));
30+
return @this.RegisterInterceptors(action, typeof(T1), typeof(T2));
3131
}
3232

3333
public static ICallInterceptorRegistry Register<TObj, T1, T2, T3>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action)
3434
where T1 : ICallInterceptor, new()
3535
where T2 : ICallInterceptor, new()
3636
where T3 : ICallInterceptor, new()
3737
{
38-
return @this.RegisterObjects(action, typeof(T1), typeof(T2), typeof(T3));
38+
return @this.RegisterInterceptors(action, typeof(T1), typeof(T2), typeof(T3));
3939
}
4040

4141
public static ICallInterceptorRegistry Register<TObj, T1, T2, T3, T4>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action)
@@ -44,42 +44,27 @@ public static ICallInterceptorRegistry Register<TObj, T1, T2, T3, T4>(this ICall
4444
where T3 : ICallInterceptor, new()
4545
where T4 : ICallInterceptor, new()
4646
{
47-
return @this.RegisterObjects(action, typeof(T1), typeof(T2), typeof(T3), typeof(T4));
47+
return @this.RegisterInterceptors(action, typeof(T1), typeof(T2), typeof(T3), typeof(T4));
4848
}
4949

5050
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Predicate<IActionInvocation> before, Action<IActionReturn> after)
5151
{
52-
return @this.RegisterObjects(action, new DelegatedCallInterceptor(before, after));
53-
}
54-
55-
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Action<IActionInvocation> before)
56-
{
57-
return Register(@this, action, x => { before(x); return true; }, null);
58-
}
59-
60-
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Predicate<IActionInvocation> before)
61-
{
62-
return Register(@this, action, before, null);
63-
}
64-
65-
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Action<IActionReturn> after)
66-
{
67-
return Register(@this, action, null, after);
52+
return @this.RegisterInterceptors(action, new DelegatedCallInterceptor(before, after));
6853
}
6954

7055
public static ICallInterceptorRegistry RegisterBefore<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Action<IActionInvocation> before)
7156
{
72-
return Register(@this, action, x => { before(x); return true; }, null);
57+
return @this.Register(action, x => { before(x); return true; }, null);
7358
}
7459

7560
public static ICallInterceptorRegistry RegisterBeforeCancellable<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Predicate<IActionInvocation> before)
7661
{
77-
return Register(@this, action, before, null);
62+
return @this.Register(action, before, null);
7863
}
7964

8065
public static ICallInterceptorRegistry RegisterAfter<TObj>(this ICallInterceptorRegistry @this, Expression<Action<TObj>> action, Action<IActionReturn> after)
8166
{
82-
return Register(@this, action, null, after);
67+
return @this.Register(action, null, after);
8368
}
8469
}
8570
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace MvcInterception
7+
{
8+
public static class CallInterceptorRegistryMethodExtensions
9+
{
10+
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred, params ICallInterceptor[] objs)
11+
{
12+
return @this.Register<TObj>(methodPred, (IEnumerable<ICallInterceptor>)objs);
13+
}
14+
15+
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred, IEnumerable<ICallInterceptor> objs)
16+
{
17+
return @this.RegisterInterceptors<TObj>(methodPred, objs.Cast<object>().ToArray());
18+
}
19+
20+
public static ICallInterceptorRegistry Register<TObj, T1>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred)
21+
where T1: ICallInterceptor, new()
22+
{
23+
return @this.RegisterInterceptors<TObj>(methodPred, typeof(T1));
24+
}
25+
26+
public static ICallInterceptorRegistry Register<TObj, T1, T2>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred)
27+
where T1 : ICallInterceptor, new()
28+
where T2 : ICallInterceptor, new()
29+
{
30+
return @this.RegisterInterceptors<TObj>(methodPred, typeof(T1), typeof(T2));
31+
}
32+
33+
public static ICallInterceptorRegistry Register<TObj, T1, T2, T3>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred)
34+
where T1 : ICallInterceptor, new()
35+
where T2 : ICallInterceptor, new()
36+
where T3 : ICallInterceptor, new()
37+
{
38+
return @this.RegisterInterceptors<TObj>(methodPred, typeof(T1), typeof(T2), typeof(T3));
39+
}
40+
41+
public static ICallInterceptorRegistry Register<TObj, T1, T2, T3, T4>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred)
42+
where T1 : ICallInterceptor, new()
43+
where T2 : ICallInterceptor, new()
44+
where T3 : ICallInterceptor, new()
45+
where T4 : ICallInterceptor, new()
46+
{
47+
return @this.RegisterInterceptors<TObj>(methodPred, typeof(T1), typeof(T2), typeof(T3), typeof(T4));
48+
}
49+
50+
public static ICallInterceptorRegistry Register<TObj>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred, Predicate<IActionInvocation> before, Action<IActionReturn> after)
51+
{
52+
return @this.RegisterInterceptors<TObj>(methodPred, new DelegatedCallInterceptor(before, after));
53+
}
54+
55+
public static ICallInterceptorRegistry RegisterBefore<TObj>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred, Action<IActionInvocation> before)
56+
{
57+
return @this.Register<TObj>(methodPred, x => { before(x); return true; }, null);
58+
}
59+
60+
public static ICallInterceptorRegistry RegisterBeforeCancellable<TObj>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred, Predicate<IActionInvocation> before)
61+
{
62+
return @this.Register<TObj>(methodPred, before, null);
63+
}
64+
65+
public static ICallInterceptorRegistry RegisterAfter<TObj>(this ICallInterceptorRegistry @this, Predicate<MethodBase> methodPred, Action<IActionReturn> after)
66+
{
67+
return @this.Register<TObj>(methodPred, null, after);
68+
}
69+
}
70+
}

MvcInterception/ICallInterceptorRegistry.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq.Expressions;
3+
using System.Reflection;
34
using MvcExtensions;
45

56
namespace MvcInterception
@@ -12,6 +13,8 @@ ContainerAdapter Container
1213
set;
1314
}
1415

15-
ICallInterceptorRegistry RegisterObjects<TObj>(Expression<Action<TObj>> action, params object[] objs);
16+
ICallInterceptorRegistry RegisterInterceptors<TObj>(Expression<Action<TObj>> action, params object[] objs);
17+
18+
ICallInterceptorRegistry RegisterInterceptors<TObj>(Predicate<MethodBase> methodPred, params object[] objs);
1619
}
1720
}

MvcInterception/MvcInterception.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@
6161
<Compile Include="ActionParameter.cs" />
6262
<Compile Include="ActionReturn.cs" />
6363
<Compile Include="CallInterceptorRegistry.cs" />
64-
<Compile Include="CallInterceptorRegistryExtensions.cs" />
64+
<Compile Include="CallInterceptorRegistryExpressionExtensions.cs" />
6565
<Compile Include="ConfigureCallInterceptorsBase.cs" />
66+
<Compile Include="CallInterceptorRegistryMethodExtensions.cs" />
6667
<Compile Include="DelegatedCallInterceptor.cs" />
6768
<Compile Include="ICallInterceptor.cs" />
6869
<Compile Include="IActionInvocation.cs" />

0 commit comments

Comments
 (0)