11using System ;
22using System . Collections . Concurrent ;
3+ using System . Collections . Generic ;
34using System . Linq . Expressions ;
5+ using System . Reflection ;
46using NHibernate . Engine ;
57using NHibernate . Intercept ;
8+ using NHibernate . Type ;
69
710namespace NHibernate . Proxy
811{
912 public sealed class StaticProxyFactory : AbstractProxyFactory
1013 {
1114 private static readonly ConcurrentDictionary < ProxyCacheEntry , Func < ILazyInitializer , NHibernateProxyFactoryInfo , INHibernateProxy > >
1215 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 > > ( ) ;
1518
1619 private static readonly INHibernateLogger Log = NHibernateLogger . For ( typeof ( StaticProxyFactory ) ) ;
1720
21+ private NHibernateProxyFactoryInfo _proxyFactoryInfo ;
22+ private ProxyCacheEntry _cacheEntry ;
23+
1824 public override INHibernateProxy GetProxy ( object id , ISessionImplementor session )
1925 {
2026 try
2127 {
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 ) ) ;
2429 return proxyActivator (
2530 new LiteLazyInitializer ( EntityName , id , session , PersistentClass ) ,
26- new NHibernateProxyFactoryInfo ( EntityName , PersistentClass , Interfaces , GetIdentifierMethod , SetIdentifierMethod , ComponentIdType ) ) ;
31+ _proxyFactoryInfo ) ;
2732 }
2833 catch ( Exception ex )
2934 {
@@ -32,6 +37,26 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
3237 }
3338 }
3439
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+
3560 private Func < ILazyInitializer , NHibernateProxyFactoryInfo , INHibernateProxy > CreateProxyActivator ( ProxyCacheEntry pke )
3661 {
3762 var proxyBuilder = new NHibernateProxyBuilder ( GetIdentifierMethod , SetIdentifierMethod , ComponentIdType , OverridesEquals ) ;
@@ -51,15 +76,13 @@ public override object GetFieldInterceptionProxy(object instanceToWrap)
5176
5277 public object GetFieldInterceptionProxy ( )
5378 {
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 ) ;
5881 }
5982
60- private Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > CreateFieldInterceptionProxyActivator ( ProxyCacheEntry pke )
83+ private Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > CreateFieldInterceptionProxyActivator ( System . Type baseType )
6184 {
62- var type = FieldInterceptorProxyBuilder . CreateProxyType ( pke . BaseType ) ;
85+ var type = FieldInterceptorProxyBuilder . CreateProxyType ( baseType ) ;
6386 var ctor = type . GetConstructor ( new [ ] { typeof ( NHibernateProxyFactoryInfo ) } ) ;
6487 var pf = Expression . Parameter ( typeof ( NHibernateProxyFactoryInfo ) ) ;
6588 return Expression . Lambda < Func < NHibernateProxyFactoryInfo , IFieldInterceptorAccessor > > ( Expression . New ( ctor , pf ) , pf ) . Compile ( ) ;
0 commit comments