forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticProxyFactory.cs
93 lines (83 loc) · 3.77 KB
/
StaticProxyFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
{
public sealed class StaticProxyFactory : AbstractProxyFactory
{
private static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>
Cache = new ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>();
private static readonly ConcurrentDictionary<System.Type, Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>
FieldInterceptorCache = new ConcurrentDictionary<System.Type, Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>();
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 proxyActivator = Cache.GetOrAdd(_cacheEntry, pke => CreateProxyActivator(pke));
return proxyActivator(
new LiteLazyInitializer(EntityName, id, session, PersistentClass),
_proxyFactoryInfo);
}
catch (Exception ex)
{
Log.Error(ex, "Creating a proxy instance failed");
throw new HibernateException("Creating a proxy instance failed", ex);
}
}
public override void PostInstantiate(
string entityName,
System.Type persistentClass,
ISet<System.Type> interfaces,
MethodInfo getIdentifierMethod,
MethodInfo setIdentifierMethod,
IAbstractComponentType componentIdType,
bool isClassProxy)
{
base.PostInstantiate(entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType, isClassProxy);
_proxyFactoryInfo = new NHibernateProxyFactoryInfo(
EntityName,
PersistentClass,
Interfaces,
GetIdentifierMethod,
SetIdentifierMethod,
ComponentIdType,
IsClassProxy);
_cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
}
private Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)
{
var proxyBuilder = new NHibernateProxyBuilder(GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, OverridesEquals);
var type = proxyBuilder.CreateProxyType(pke.BaseType, pke.Interfaces);
var ctor = type.GetConstructor(new[] {typeof(ILazyInitializer), typeof(NHibernateProxyFactoryInfo)});
var li = Expression.Parameter(typeof(ILazyInitializer));
var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo));
return Expression.Lambda<Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>(Expression.New(ctor, li, pf), li, pf).Compile();
}
// Since 5.3
[Obsolete("Use ProxyFactoryExtensions.GetFieldInterceptionProxy extension method instead.")]
public override object GetFieldInterceptionProxy(object instanceToWrap)
{
return GetFieldInterceptionProxy();
}
public object GetFieldInterceptionProxy()
{
var proxyActivator = FieldInterceptorCache.GetOrAdd(PersistentClass, CreateFieldInterceptionProxyActivator);
return proxyActivator(_proxyFactoryInfo);
}
private Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor> CreateFieldInterceptionProxyActivator(System.Type baseType)
{
var type = FieldInterceptorProxyBuilder.CreateProxyType(baseType);
var ctor = type.GetConstructor(new[] { typeof(NHibernateProxyFactoryInfo) });
var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo));
return Expression.Lambda<Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>(Expression.New(ctor, pf), pf).Compile();
}
}
}