Skip to content

Commit 3dd57a0

Browse files
committed
Implement a new StaticProxyFactoryFactory
1 parent de1f489 commit 3dd57a0

File tree

5 files changed

+82
-25
lines changed

5 files changed

+82
-25
lines changed

src/NHibernate/Bytecode/AbstractBytecodeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public virtual IProxyFactoryFactory ProxyFactoryFactory
2929
}
3030
}
3131

32-
return DefaultProxyFactoryFactory.Instance;
32+
return StaticProxyFactoryFactory.Instance;
3333
}
3434
}
3535

src/NHibernate/Bytecode/DefaultProxyFactoryFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace NHibernate.Bytecode
44
{
55
public class DefaultProxyFactoryFactory : IProxyFactoryFactory
66
{
7-
internal static DefaultProxyFactoryFactory Instance = new DefaultProxyFactoryFactory();
7+
#region IProxyFactoryFactory Members
88

99
public IProxyFactory BuildProxyFactory()
1010
{
@@ -25,5 +25,7 @@ public bool IsProxy(object entity)
2525
{
2626
return entity is INHibernateProxy;
2727
}
28+
29+
#endregion
2830
}
29-
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using NHibernate.Proxy;
2+
3+
namespace NHibernate.Bytecode
4+
{
5+
public class StaticProxyFactoryFactory : IProxyFactoryFactory
6+
{
7+
internal static StaticProxyFactoryFactory Instance = new StaticProxyFactoryFactory();
8+
9+
public IProxyFactory BuildProxyFactory() => new DefaultProxyFactory();
10+
11+
public IProxyValidator ProxyValidator => new DynProxyTypeValidator();
12+
13+
public bool IsInstrumented(System.Type entityClass) => true;
14+
15+
public bool IsProxy(object entity) => entity is INHibernateProxy;
16+
}
17+
}
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Concurrent;
3-
using System.Linq.Expressions;
42
using NHibernate.Engine;
53
using NHibernate.Intercept;
64
using NHibernate.Proxy.DynamicProxy;
@@ -9,20 +7,20 @@ namespace NHibernate.Proxy
97
{
108
public class DefaultProxyFactory : AbstractProxyFactory
119
{
12-
static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>> Cache =
13-
new ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>();
14-
10+
private readonly ProxyFactory factory = new ProxyFactory();
1511
protected static readonly INHibernateLogger log = NHibernateLogger.For(typeof (DefaultProxyFactory));
1612

1713
public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
1814
{
1915
try
2016
{
21-
var cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
22-
var proxyActivator = Cache.GetOrAdd(cacheEntry, pke => CreateProxyActivator(pke));
23-
return proxyActivator(
24-
new LiteLazyInitializer(EntityName, id, session, PersistentClass),
25-
new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType));
17+
var initializer = new DefaultLazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, session, OverridesEquals);
18+
19+
object proxyInstance = IsClassProxy
20+
? factory.CreateProxy(PersistentClass, initializer, Interfaces)
21+
: factory.CreateProxy(Interfaces[0], initializer, Interfaces);
22+
23+
return (INHibernateProxy) proxyInstance;
2624
}
2725
catch (Exception ex)
2826
{
@@ -31,21 +29,10 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
3129
}
3230
}
3331

34-
Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)
35-
{
36-
var proxyBuilder = new NHibernateProxyBuilder(GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, OverridesEquals);
37-
var type = proxyBuilder.CreateProxyType(pke.BaseType, pke.Interfaces);
38-
var ctor = type.GetConstructor(new[] {typeof(ILazyInitializer), typeof(NHibernateProxyFactoryInfo)});
39-
var li = Expression.Parameter(typeof(ILazyInitializer));
40-
var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo));
41-
return Expression.Lambda<Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>(Expression.New(ctor, li, pf), li, pf).Compile();
42-
}
43-
4432
public override object GetFieldInterceptionProxy(object instanceToWrap)
4533
{
46-
var factory = new ProxyFactory();
4734
var interceptor = new DefaultDynamicLazyFieldInterceptor();
4835
return factory.CreateProxy(PersistentClass, interceptor, new[] { typeof(IFieldInterceptorAccessor) });
4936
}
5037
}
51-
}
38+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Linq.Expressions;
4+
using NHibernate.Engine;
5+
using NHibernate.Intercept;
6+
using NHibernate.Proxy.DynamicProxy;
7+
8+
namespace NHibernate.Proxy
9+
{
10+
public class StaticProxyFactory : AbstractProxyFactory
11+
{
12+
static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>> Cache =
13+
new ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>();
14+
15+
protected static readonly INHibernateLogger log = NHibernateLogger.For(typeof (DefaultProxyFactory));
16+
17+
public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
18+
{
19+
try
20+
{
21+
var cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
22+
var proxyActivator = Cache.GetOrAdd(cacheEntry, pke => CreateProxyActivator(pke));
23+
return proxyActivator(
24+
new LiteLazyInitializer(EntityName, id, session, PersistentClass),
25+
new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType));
26+
}
27+
catch (Exception ex)
28+
{
29+
log.Error(ex, "Creating a proxy instance failed");
30+
throw new HibernateException("Creating a proxy instance failed", ex);
31+
}
32+
}
33+
34+
Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)
35+
{
36+
var proxyBuilder = new NHibernateProxyBuilder(GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, OverridesEquals);
37+
var type = proxyBuilder.CreateProxyType(pke.BaseType, pke.Interfaces);
38+
var ctor = type.GetConstructor(new[] {typeof(ILazyInitializer), typeof(NHibernateProxyFactoryInfo)});
39+
var li = Expression.Parameter(typeof(ILazyInitializer));
40+
var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo));
41+
return Expression.Lambda<Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>(Expression.New(ctor, li, pf), li, pf).Compile();
42+
}
43+
44+
public override object GetFieldInterceptionProxy(object instanceToWrap)
45+
{
46+
var factory = new ProxyFactory();
47+
var interceptor = new DefaultDynamicLazyFieldInterceptor();
48+
return factory.CreateProxy(PersistentClass, interceptor, new[] { typeof(IFieldInterceptorAccessor) });
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)