Skip to content

Commit 6c0ec4c

Browse files
committed
Implement new StaticProxyFactoryFactory
- The factory generates high-efficient static proxies (no interceptor)
1 parent 73797df commit 6c0ec4c

9 files changed

+561
-10
lines changed

src/NHibernate/Bytecode/AbstractBytecodeProvider.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public virtual IProxyFactoryFactory ProxyFactoryFactory
2828
throw new HibernateByteCodeException("Failed to create an instance of '" + proxyFactoryFactory.FullName + "'!", e);
2929
}
3030
}
31-
return new DefaultProxyFactoryFactory();
31+
32+
return StaticProxyFactoryFactory.Instance;
3233
}
3334
}
3435

@@ -116,4 +117,4 @@ public void SetCollectionTypeFactoryClass(System.Type type)
116117

117118
#endregion
118119
}
119-
}
120+
}
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+
}

src/NHibernate/Proxy/DynamicProxy/DefaultProxyMethodBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public DefaultyProxyMethodBuilder(IMethodBodyEmitter emitter)
3030

3131
public IMethodBodyEmitter MethodBodyEmitter { get; private set; }
3232

33-
private static MethodBuilder GenerateMethodSignature(string name, MethodInfo method, TypeBuilder typeBuilder)
33+
internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo method, TypeBuilder typeBuilder)
3434
{
3535
//TODO: Should we use attributes of base method?
3636
var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;

src/NHibernate/Proxy/DynamicProxy/ProxyFactory.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public sealed class ProxyFactory
2121
{
2222
internal static readonly ConcurrentDictionary<ProxyCacheEntry, TypeInfo> _cache = new ConcurrentDictionary<ProxyCacheEntry, TypeInfo>();
2323

24-
private static readonly ConstructorInfo defaultBaseConstructor = typeof(object).GetConstructor(new System.Type[0]);
24+
internal static readonly ConstructorInfo defaultBaseConstructor = typeof(object).GetConstructor(new System.Type[0]);
2525

2626
private static readonly MethodInfo getValue = ReflectHelper.GetMethod<SerializationInfo>(
2727
si => si.GetValue(null, null));
28-
private static readonly MethodInfo setType = ReflectHelper.GetMethod<SerializationInfo>(
28+
internal static readonly MethodInfo setType = ReflectHelper.GetMethod<SerializationInfo>(
2929
si => si.SetType(null));
3030
private static readonly MethodInfo addValue = ReflectHelper.GetMethod<SerializationInfo>(
3131
si => si.AddValue(null, null));
@@ -129,7 +129,7 @@ private TypeInfo CreateUncachedProxyType(System.Type baseType, IReadOnlyCollecti
129129
return proxyType;
130130
}
131131

132-
private IEnumerable<MethodInfo> GetProxiableMethods(System.Type type, IEnumerable<System.Type> interfaces)
132+
internal static IEnumerable<MethodInfo> GetProxiableMethods(System.Type type, IEnumerable<System.Type> interfaces)
133133
{
134134
const BindingFlags candidateMethodsBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
135135
return
@@ -139,7 +139,7 @@ private IEnumerable<MethodInfo> GetProxiableMethods(System.Type type, IEnumerabl
139139
.Distinct();
140140
}
141141

142-
private static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder, System.Type parentType)
142+
internal static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder, System.Type parentType)
143143
{
144144
const MethodAttributes constructorAttributes = MethodAttributes.Public |
145145
MethodAttributes.HideBySig | MethodAttributes.SpecialName |
@@ -166,7 +166,7 @@ private static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder, Sys
166166
return constructor;
167167
}
168168

169-
private static void ImplementGetObjectData(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField)
169+
internal static void ImplementGetObjectData(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField)
170170
{
171171
const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
172172
MethodAttributes.Virtual;
@@ -217,7 +217,7 @@ private static void ImplementGetObjectData(System.Type baseType, IReadOnlyCollec
217217
IL.Emit(OpCodes.Ret);
218218
}
219219

220-
private static void DefineSerializationConstructor(TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor)
220+
internal static void DefineSerializationConstructor(TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor)
221221
{
222222
const MethodAttributes constructorAttributes = MethodAttributes.Public |
223223
MethodAttributes.HideBySig | MethodAttributes.SpecialName |
@@ -254,7 +254,7 @@ private static void DefineSerializationConstructor(TypeBuilder typeBuilder, Fiel
254254
IL.Emit(OpCodes.Ret);
255255
}
256256

257-
private static void AddSerializationSupport(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor)
257+
internal static void AddSerializationSupport(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor)
258258
{
259259
ConstructorInfo serializableConstructor = typeof(SerializableAttribute).GetConstructor(new System.Type[0]);
260260
var customAttributeBuilder = new CustomAttributeBuilder(serializableConstructor, new object[0]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using NHibernate.Engine;
3+
4+
namespace NHibernate.Proxy
5+
{
6+
[Serializable]
7+
sealed class LiteLazyInitializer : AbstractLazyInitializer
8+
{
9+
internal LiteLazyInitializer(string entityName, object id, ISessionImplementor session, System.Type persistentClass)
10+
: base(entityName, id, session)
11+
{
12+
PersistentClass = persistentClass;
13+
}
14+
15+
public override System.Type PersistentClass { get; }
16+
}
17+
}

0 commit comments

Comments
 (0)