1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
+ using System . Collections . Generic ;
3
4
using System . Linq . Expressions ;
5
+ using System . Reflection ;
4
6
using NHibernate . Engine ;
5
7
using NHibernate . Intercept ;
8
+ using NHibernate . Type ;
6
9
7
10
namespace NHibernate . Proxy
8
11
{
9
12
public sealed class StaticProxyFactory : AbstractProxyFactory
10
13
{
11
14
private static readonly ConcurrentDictionary < ProxyCacheEntry , Func < ILazyInitializer , NHibernateProxyFactoryInfo , INHibernateProxy > >
12
15
Cache = new ConcurrentDictionary < ProxyCacheEntry , Func < ILazyInitializer , NHibernateProxyFactoryInfo , INHibernateProxy > > ( ) ;
13
- private static readonly ConcurrentDictionary < ProxyCacheEntry , Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > >
14
- FieldInterceptorCache = new ConcurrentDictionary < ProxyCacheEntry , Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > > ( ) ;
16
+ private static readonly ConcurrentDictionary < System . Type , Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > >
17
+ FieldInterceptorCache = new ConcurrentDictionary < System . Type , Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > > ( ) ;
15
18
16
19
private static readonly INHibernateLogger Log = NHibernateLogger . For ( typeof ( StaticProxyFactory ) ) ;
17
20
21
+ private NHibernateProxyFactoryInfo _proxyFactoryInfo ;
22
+ private ProxyCacheEntry _cacheEntry ;
23
+
18
24
public override INHibernateProxy GetProxy ( object id , ISessionImplementor session )
19
25
{
20
26
try
21
27
{
22
- var cacheEntry = new ProxyCacheEntry ( IsClassProxy ? PersistentClass : typeof ( object ) , Interfaces ) ;
23
- var proxyActivator = Cache . GetOrAdd ( cacheEntry , pke => CreateProxyActivator ( pke ) ) ;
28
+ var proxyActivator = Cache . GetOrAdd ( _cacheEntry , pke => CreateProxyActivator ( pke ) ) ;
24
29
return proxyActivator (
25
30
new LiteLazyInitializer ( EntityName , id , session , PersistentClass ) ,
26
- new NHibernateProxyFactoryInfo ( EntityName , PersistentClass , Interfaces , GetIdentifierMethod , SetIdentifierMethod , ComponentIdType ) ) ;
31
+ _proxyFactoryInfo ) ;
27
32
}
28
33
catch ( Exception ex )
29
34
{
@@ -32,6 +37,26 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
32
37
}
33
38
}
34
39
40
+ public override void PostInstantiate (
41
+ string entityName ,
42
+ System . Type persistentClass ,
43
+ ISet < System . Type > interfaces ,
44
+ MethodInfo getIdentifierMethod ,
45
+ MethodInfo setIdentifierMethod ,
46
+ IAbstractComponentType componentIdType )
47
+ {
48
+ base . PostInstantiate ( entityName , persistentClass , interfaces , getIdentifierMethod , setIdentifierMethod , componentIdType ) ;
49
+
50
+ _proxyFactoryInfo = new NHibernateProxyFactoryInfo (
51
+ EntityName ,
52
+ PersistentClass ,
53
+ Interfaces ,
54
+ GetIdentifierMethod ,
55
+ SetIdentifierMethod ,
56
+ ComponentIdType ) ;
57
+ _cacheEntry = new ProxyCacheEntry ( IsClassProxy ? PersistentClass : typeof ( object ) , Interfaces ) ;
58
+ }
59
+
35
60
private Func < ILazyInitializer , NHibernateProxyFactoryInfo , INHibernateProxy > CreateProxyActivator ( ProxyCacheEntry pke )
36
61
{
37
62
var proxyBuilder = new NHibernateProxyBuilder ( GetIdentifierMethod , SetIdentifierMethod , ComponentIdType , OverridesEquals ) ;
@@ -51,15 +76,13 @@ public override object GetFieldInterceptionProxy(object instanceToWrap)
51
76
52
77
public object GetFieldInterceptionProxy ( )
53
78
{
54
- var cacheEntry = new ProxyCacheEntry ( PersistentClass , System . Type . EmptyTypes ) ;
55
- var proxyActivator = FieldInterceptorCache . GetOrAdd ( cacheEntry , CreateFieldInterceptionProxyActivator ) ;
56
- return proxyActivator (
57
- new NHibernateProxyFactoryInfo ( EntityName , PersistentClass , Interfaces , GetIdentifierMethod , SetIdentifierMethod , ComponentIdType ) ) ;
79
+ var proxyActivator = FieldInterceptorCache . GetOrAdd ( PersistentClass , CreateFieldInterceptionProxyActivator ) ;
80
+ return proxyActivator ( _proxyFactoryInfo ) ;
58
81
}
59
82
60
- private Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > CreateFieldInterceptionProxyActivator ( ProxyCacheEntry pke )
83
+ private Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > CreateFieldInterceptionProxyActivator ( System . Type baseType )
61
84
{
62
- var type = FieldInterceptorProxyBuilder . CreateProxyType ( pke . BaseType ) ;
85
+ var type = FieldInterceptorProxyBuilder . CreateProxyType ( baseType ) ;
63
86
var ctor = type . GetConstructor ( new [ ] { typeof ( NHibernateProxyFactoryInfo ) } ) ;
64
87
var pf = Expression . Parameter ( typeof ( NHibernateProxyFactoryInfo ) ) ;
65
88
return Expression . Lambda < Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > > ( Expression . New ( ctor , pf ) , pf ) . Compile ( ) ;
0 commit comments