diff --git a/src/NHibernate/Proxy/StaticProxyFactory.cs b/src/NHibernate/Proxy/StaticProxyFactory.cs index 2c6d7dc66cc..278ac748212 100644 --- a/src/NHibernate/Proxy/StaticProxyFactory.cs +++ b/src/NHibernate/Proxy/StaticProxyFactory.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Linq.Expressions; +using System.Reflection; using NHibernate.Engine; using NHibernate.Intercept; +using NHibernate.Type; namespace NHibernate.Proxy { @@ -10,20 +13,22 @@ public sealed class StaticProxyFactory : AbstractProxyFactory { private static readonly ConcurrentDictionary> Cache = new ConcurrentDictionary>(); - private static readonly ConcurrentDictionary> - FieldInterceptorCache = new ConcurrentDictionary>(); + private static readonly ConcurrentDictionary> + FieldInterceptorCache = new ConcurrentDictionary>(); private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof(StaticProxyFactory)); + private NHibernateProxyFactoryInfo _proxyFactoryInfo; + private ProxyCacheEntry _cacheEntry; + public override INHibernateProxy GetProxy(object id, ISessionImplementor session) { try { - var cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces); - var proxyActivator = Cache.GetOrAdd(cacheEntry, pke => CreateProxyActivator(pke)); + var proxyActivator = Cache.GetOrAdd(_cacheEntry, pke => CreateProxyActivator(pke)); return proxyActivator( new LiteLazyInitializer(EntityName, id, session, PersistentClass), - new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType)); + _proxyFactoryInfo); } catch (Exception ex) { @@ -32,6 +37,26 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session } } + public override void PostInstantiate( + string entityName, + System.Type persistentClass, + ISet interfaces, + MethodInfo getIdentifierMethod, + MethodInfo setIdentifierMethod, + IAbstractComponentType componentIdType) + { + base.PostInstantiate(entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType); + + _proxyFactoryInfo = new NHibernateProxyFactoryInfo( + EntityName, + PersistentClass, + Interfaces, + GetIdentifierMethod, + SetIdentifierMethod, + ComponentIdType); + _cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces); + } + private Func CreateProxyActivator(ProxyCacheEntry pke) { var proxyBuilder = new NHibernateProxyBuilder(GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, OverridesEquals); @@ -51,15 +76,13 @@ public override object GetFieldInterceptionProxy(object instanceToWrap) public object GetFieldInterceptionProxy() { - var cacheEntry = new ProxyCacheEntry(PersistentClass, System.Type.EmptyTypes); - var proxyActivator = FieldInterceptorCache.GetOrAdd(cacheEntry, CreateFieldInterceptionProxyActivator); - return proxyActivator( - new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType)); + var proxyActivator = FieldInterceptorCache.GetOrAdd(PersistentClass, CreateFieldInterceptionProxyActivator); + return proxyActivator(_proxyFactoryInfo); } - private Func CreateFieldInterceptionProxyActivator(ProxyCacheEntry pke) + private Func CreateFieldInterceptionProxyActivator(System.Type baseType) { - var type = FieldInterceptorProxyBuilder.CreateProxyType(pke.BaseType); + var type = FieldInterceptorProxyBuilder.CreateProxyType(baseType); var ctor = type.GetConstructor(new[] { typeof(NHibernateProxyFactoryInfo) }); var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo)); return Expression.Lambda>(Expression.New(ctor, pf), pf).Compile();