-
Notifications
You must be signed in to change notification settings - Fork 934
New StaticProxyFactoryFactory #1451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
c48beb8
9cc5161
de78104
b4bfc90
bf80568
64e4b57
fa51673
01ed700
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using NHibernate.Proxy; | ||
|
||
namespace NHibernate.Bytecode | ||
{ | ||
public class StaticProxyFactoryFactory : IProxyFactoryFactory | ||
{ | ||
internal static StaticProxyFactoryFactory Instance = new StaticProxyFactoryFactory(); | ||
|
||
public IProxyFactory BuildProxyFactory() => new StaticProxyFactory(); | ||
|
||
public IProxyValidator ProxyValidator => new DynProxyTypeValidator(); | ||
|
||
public bool IsInstrumented(System.Type entityClass) => true; | ||
|
||
public bool IsProxy(object entity) => entity is INHibernateProxy; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,11 @@ public sealed class ProxyFactory | |
{ | ||
internal static readonly ConcurrentDictionary<ProxyCacheEntry, TypeInfo> _cache = new ConcurrentDictionary<ProxyCacheEntry, TypeInfo>(); | ||
|
||
private static readonly ConstructorInfo defaultBaseConstructor = typeof(object).GetConstructor(new System.Type[0]); | ||
internal static readonly ConstructorInfo defaultBaseConstructor = typeof(object).GetConstructor(new System.Type[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since Same for |
||
|
||
private static readonly MethodInfo getValue = ReflectHelper.GetMethod<SerializationInfo>( | ||
si => si.GetValue(null, null)); | ||
private static readonly MethodInfo setType = ReflectHelper.GetMethod<SerializationInfo>( | ||
internal static readonly MethodInfo setType = ReflectHelper.GetMethod<SerializationInfo>( | ||
si => si.SetType(null)); | ||
private static readonly MethodInfo addValue = ReflectHelper.GetMethod<SerializationInfo>( | ||
si => si.AddValue(null, null)); | ||
|
@@ -129,7 +129,7 @@ private TypeInfo CreateUncachedProxyType(System.Type baseType, IReadOnlyCollecti | |
return proxyType; | ||
} | ||
|
||
private IEnumerable<MethodInfo> GetProxiableMethods(System.Type type, IEnumerable<System.Type> interfaces) | ||
internal static IEnumerable<MethodInfo> GetProxiableMethods(System.Type type, IEnumerable<System.Type> interfaces) | ||
{ | ||
const BindingFlags candidateMethodsBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; | ||
return | ||
|
@@ -139,7 +139,7 @@ private IEnumerable<MethodInfo> GetProxiableMethods(System.Type type, IEnumerabl | |
.Distinct(); | ||
} | ||
|
||
private static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder, System.Type parentType) | ||
internal static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder, System.Type parentType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used outside of |
||
{ | ||
const MethodAttributes constructorAttributes = MethodAttributes.Public | | ||
MethodAttributes.HideBySig | MethodAttributes.SpecialName | | ||
|
@@ -166,7 +166,7 @@ private static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder, Sys | |
return constructor; | ||
} | ||
|
||
private static void ImplementGetObjectData(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField) | ||
internal static void ImplementGetObjectData(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related subject but not introduced by this PR: I wonder if in the case of dynamic code we should also add that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should, but I don't want to touch the DynamicProxy namespace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used outside of |
||
{ | ||
const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | | ||
MethodAttributes.Virtual; | ||
|
@@ -217,7 +217,7 @@ private static void ImplementGetObjectData(System.Type baseType, IReadOnlyCollec | |
IL.Emit(OpCodes.Ret); | ||
} | ||
|
||
private static void DefineSerializationConstructor(TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor) | ||
internal static void DefineSerializationConstructor(TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related subject but not introduced by this PR: Is the method body emitted here useful? Since it is supposed to be deserialized by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to touch/fix/change DynamicProxy namespace other than for major bugs. It's working, so let it be this way. The namespace itself is going to be retired in 6.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not used outside of |
||
{ | ||
const MethodAttributes constructorAttributes = MethodAttributes.Public | | ||
MethodAttributes.HideBySig | MethodAttributes.SpecialName | | ||
|
@@ -254,7 +254,7 @@ private static void DefineSerializationConstructor(TypeBuilder typeBuilder, Fiel | |
IL.Emit(OpCodes.Ret); | ||
} | ||
|
||
private static void AddSerializationSupport(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor) | ||
internal static void AddSerializationSupport(System.Type baseType, IReadOnlyCollection<System.Type> baseInterfaces, TypeBuilder typeBuilder, FieldInfo interceptorField, ConstructorBuilder defaultConstructor) | ||
{ | ||
ConstructorInfo serializableConstructor = typeof(SerializableAttribute).GetConstructor(new System.Type[0]); | ||
var customAttributeBuilder = new CustomAttributeBuilder(serializableConstructor, new object[0]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using NHibernate.Engine; | ||
|
||
namespace NHibernate.Proxy | ||
{ | ||
[Serializable] | ||
sealed class LiteLazyInitializer : AbstractLazyInitializer | ||
{ | ||
internal LiteLazyInitializer(string entityName, object id, ISessionImplementor session, System.Type persistentClass) | ||
: base(entityName, id, session) | ||
{ | ||
PersistentClass = persistentClass; | ||
} | ||
|
||
public override System.Type PersistentClass { get; } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For enforcing the singleton pattern, I would define a private default constructor.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a small memory optimization. I don't want to enforce the singleton pattern here. And, in fact, we cannot enforce it, as
StaticProxyFactoryFactory
can be manually configured as a proxy factory factory and will require the public constructor to be creatable by reflection.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I forgot to think about configuration.