Skip to content

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

Merged
merged 8 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void NotConfiguredProxyFactoryFactory()
{
var bcp = new BytecodeProviderImpl();
IProxyFactoryFactory p = bcp.ProxyFactoryFactory;
Assert.That(p, Is.InstanceOf<DefaultProxyFactoryFactory>());
Assert.That(p, Is.InstanceOf<StaticProxyFactoryFactory>());
}

[Test]
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate/Bytecode/AbstractBytecodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public virtual IProxyFactoryFactory ProxyFactoryFactory
throw new HibernateByteCodeException("Failed to create an instance of '" + proxyFactoryFactory.FullName + "'!", e);
}
}
return new DefaultProxyFactoryFactory();

return StaticProxyFactoryFactory.Instance;
}
}

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

#endregion
}
}
}
17 changes: 17 additions & 0 deletions src/NHibernate/Bytecode/StaticProxyFactoryFactory.cs
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();
Copy link
Member

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.

Copy link
Member Author

@hazzik hazzik Dec 12, 2017

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.

Copy link
Member

@fredericDelaporte fredericDelaporte Dec 12, 2017

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.


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
Expand Up @@ -30,7 +30,7 @@ public DefaultyProxyMethodBuilder(IMethodBodyEmitter emitter)

public IMethodBodyEmitter MethodBodyEmitter { get; private set; }

private static MethodBuilder GenerateMethodSignature(string name, MethodInfo method, TypeBuilder typeBuilder)
internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo method, TypeBuilder typeBuilder)
{
//TODO: Should we use attributes of base method?
var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
Expand Down
14 changes: 7 additions & 7 deletions src/NHibernate/Proxy/DynamicProxy/ProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since DynamicProxy is supposed to be removed in some future further proxy refactoring, I would tend to duplicate such a field in NHibernateProxyBuilder rather than sharing it.
Otherwise I would also consider putting it in ReflectionCache.

Same for setType field.


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));
Expand Down Expand Up @@ -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
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used outside of ProxyFactory, could stay private.

{
const MethodAttributes constructorAttributes = MethodAttributes.Public |
MethodAttributes.HideBySig | MethodAttributes.SpecialName |
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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 [SecurityCritical] attribute to GetObjectData. I am not finding much information on the subject.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should, but I don't want to touch the DynamicProxy namespace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used outside of ProxyFactory, could stay private.

{
const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.Virtual;
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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 ProxyObjectReference. It should be implemented the same way than NHibernateProxyBuilder.ImplementDeserializationConstructor I think.

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used outside of ProxyFactory, could stay private.

{
const MethodAttributes constructorAttributes = MethodAttributes.Public |
MethodAttributes.HideBySig | MethodAttributes.SpecialName |
Expand Down Expand Up @@ -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]);
Expand Down
17 changes: 17 additions & 0 deletions src/NHibernate/Proxy/LiteLazyInitializer.cs
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; }
}
}
Loading