1
+ using StructureMap . Building . Interception ;
2
+ using StructureMap . Pipeline ;
3
+ using StructureMap . TypeRules ;
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
7
+
8
+ namespace StructureMap . DynamicInterception
9
+ {
10
+ public class DynamicProxyInterceptorPolicy : IInterceptorPolicy
11
+ {
12
+ private static readonly Func < Type , Instance , bool > TrueFilter = ( type , instance ) => true ;
13
+
14
+ private readonly Func < Type , Instance , bool > _filter ;
15
+ private readonly object [ ] _interceptionBehaviors ;
16
+
17
+ public DynamicProxyInterceptorPolicy ( Func < Type , Instance , bool > filter , params IInterceptionBehavior [ ] interceptionBehaviors )
18
+ {
19
+ _filter = filter ?? TrueFilter ;
20
+ _interceptionBehaviors = interceptionBehaviors ;
21
+ }
22
+
23
+ public DynamicProxyInterceptorPolicy ( Func < Type , bool > filter , params IInterceptionBehavior [ ] interceptionBehaviors )
24
+ : this ( ( type , instance ) => filter ( type ) , interceptionBehaviors )
25
+ {
26
+ }
27
+
28
+ public DynamicProxyInterceptorPolicy ( Func < Instance , bool > filter , params IInterceptionBehavior [ ] interceptionBehaviors )
29
+ : this ( ( type , instance ) => filter ( instance ) , interceptionBehaviors )
30
+ {
31
+ }
32
+
33
+ public DynamicProxyInterceptorPolicy ( params IInterceptionBehavior [ ] interceptionBehaviors )
34
+ : this ( TrueFilter , interceptionBehaviors )
35
+ {
36
+ }
37
+
38
+ public DynamicProxyInterceptorPolicy ( Func < Type , Instance , bool > filter , params Type [ ] interceptionBehaviorTypes )
39
+ {
40
+ _filter = filter ?? TrueFilter ;
41
+ _interceptionBehaviors = interceptionBehaviorTypes ;
42
+ }
43
+
44
+ public DynamicProxyInterceptorPolicy ( Func < Type , bool > filter , params Type [ ] interceptionBehaviorTypes )
45
+ : this ( ( type , instance ) => filter ( type ) , interceptionBehaviorTypes )
46
+ {
47
+ }
48
+
49
+ public DynamicProxyInterceptorPolicy ( Func < Instance , bool > filter , params Type [ ] interceptionBehaviorTypes )
50
+ : this ( ( type , instance ) => filter ( instance ) , interceptionBehaviorTypes )
51
+ {
52
+ }
53
+
54
+ public DynamicProxyInterceptorPolicy ( params Type [ ] interceptionBehaviorTypes )
55
+ : this ( TrueFilter , interceptionBehaviorTypes )
56
+ {
57
+ }
58
+
59
+ public IEnumerable < IInterceptor > DetermineInterceptors ( Type pluginType , Instance instance )
60
+ {
61
+ if ( _filter ( pluginType , instance ) )
62
+ {
63
+ var interceptorType = typeof ( DynamicProxyInterceptor < > ) . MakeGenericType ( pluginType ) ;
64
+ var interceptor = ( IInterceptor ) Activator . CreateInstance ( interceptorType , new object [ ] { _interceptionBehaviors } ) ;
65
+ yield return interceptor ;
66
+ }
67
+ }
68
+
69
+ public string Description
70
+ {
71
+ get
72
+ {
73
+ return string . Format ( "Decorate with dynamic proxy classes using the following interception behaviors: {0}" ,
74
+ string . Join ( ", " , _interceptionBehaviors . Select ( b => ( b as Type ?? b . GetType ( ) ) . GetFullName ( ) ) ) ) ;
75
+ }
76
+ }
77
+ }
78
+ }
0 commit comments