Skip to content

Commit ea8fc88

Browse files
committed
Make AbstractProxyFactory non serializable
1 parent cc1222c commit ea8fc88

File tree

5 files changed

+84
-62
lines changed

5 files changed

+84
-62
lines changed
Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
4-
using System.Runtime.Serialization;
5-
using System.Security;
64
using NHibernate.Engine;
75
using NHibernate.Type;
86
using NHibernate.Util;
@@ -12,7 +10,7 @@ namespace NHibernate.Proxy
1210
/// <summary>
1311
/// Convenient common implementation for ProxyFactory
1412
/// </summary>
15-
public abstract class AbstractProxyFactory: IProxyFactory, ISerializable
13+
public abstract class AbstractProxyFactory: IProxyFactory
1614
{
1715
protected virtual string EntityName { get; private set; }
1816
protected virtual System.Type PersistentClass { get; private set; }
@@ -27,21 +25,6 @@ protected bool IsClassProxy
2725
get { return Interfaces.Length == 1; }
2826
}
2927

30-
protected AbstractProxyFactory()
31-
{
32-
}
33-
34-
protected AbstractProxyFactory(SerializationInfo info, StreamingContext context)
35-
{
36-
EntityName = (string) info.GetValue(nameof(EntityName), typeof(string));
37-
PersistentClass = (System.Type) info.GetValue(nameof(PersistentClass), typeof(System.Type));
38-
Interfaces = (System.Type[]) info.GetValue(nameof(Interfaces), typeof(System.Type[]));
39-
GetIdentifierMethod = (MethodInfo) info.GetValue(nameof(GetIdentifierMethod), typeof(MethodInfo));
40-
SetIdentifierMethod = (MethodInfo) info.GetValue(nameof(SetIdentifierMethod), typeof(MethodInfo));
41-
ComponentIdType = (IAbstractComponentType) info.GetValue(nameof(ComponentIdType), typeof(IAbstractComponentType));
42-
OverridesEquals = (bool) info.GetValue(nameof(OverridesEquals), typeof(bool));
43-
}
44-
4528
public virtual void PostInstantiate(string entityName, System.Type persistentClass, ISet<System.Type> interfaces,
4629
MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod,
4730
IAbstractComponentType componentIdType)
@@ -68,17 +51,5 @@ public virtual object GetFieldInterceptionProxy(object instanceToWrap)
6851
{
6952
throw new NotSupportedException();
7053
}
71-
72-
[SecurityCritical]
73-
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
74-
{
75-
info.AddValue(nameof(EntityName), EntityName);
76-
info.AddValue(nameof(PersistentClass), PersistentClass);
77-
info.AddValue(nameof(Interfaces), Interfaces);
78-
info.AddValue(nameof(GetIdentifierMethod), GetIdentifierMethod);
79-
info.AddValue(nameof(SetIdentifierMethod), SetIdentifierMethod);
80-
info.AddValue(nameof(ComponentIdType), ComponentIdType);
81-
info.AddValue(nameof(OverridesEquals), OverridesEquals);
82-
}
8354
}
8455
}

src/NHibernate/Proxy/DefaultProxyFactory.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Linq.Expressions;
4-
using System.Runtime.Serialization;
54
using NHibernate.Engine;
65
using NHibernate.Intercept;
76
using NHibernate.Proxy.DynamicProxy;
87

98
namespace NHibernate.Proxy
109
{
11-
[Serializable]
1210
public class DefaultProxyFactory : AbstractProxyFactory
1311
{
14-
static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, IProxyFactory, INHibernateProxy>> Cache =
15-
new ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, IProxyFactory, INHibernateProxy>>();
12+
static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>> Cache =
13+
new ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>();
1614

1715
protected static readonly INHibernateLogger log = NHibernateLogger.For(typeof (DefaultProxyFactory));
1816

19-
public DefaultProxyFactory()
20-
{
21-
}
22-
23-
protected DefaultProxyFactory(SerializationInfo info, StreamingContext context)
24-
: base(info, context) { }
25-
2617
public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
2718
{
2819
try
2920
{
3021
var cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
3122
var proxyActivator = Cache.GetOrAdd(cacheEntry, pke => CreateProxyActivator(pke));
32-
return proxyActivator(new LiteLazyInitializer(EntityName, id, session, PersistentClass), this);
23+
return proxyActivator(
24+
new LiteLazyInitializer(EntityName, id, session, PersistentClass),
25+
new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType));
3326
}
3427
catch (Exception ex)
3528
{
@@ -38,14 +31,14 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
3831
}
3932
}
4033

41-
Func<ILazyInitializer, IProxyFactory, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)
34+
Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)
4235
{
4336
var proxyBuilder = new NHibernateProxyBuilder(GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, OverridesEquals);
4437
var type = proxyBuilder.CreateProxyType(pke.BaseType, pke.Interfaces);
45-
var ctor = type.GetConstructor(new[] {typeof(ILazyInitializer), typeof(IProxyFactory)});
38+
var ctor = type.GetConstructor(new[] {typeof(ILazyInitializer), typeof(NHibernateProxyFactoryInfo)});
4639
var li = Expression.Parameter(typeof(ILazyInitializer));
47-
var pf = Expression.Parameter(typeof(IProxyFactory));
48-
return Expression.Lambda<Func<ILazyInitializer, IProxyFactory, INHibernateProxy>>(Expression.New(ctor, li, pf), li, pf).Compile();
40+
var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo));
41+
return Expression.Lambda<Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>(Expression.New(ctor, li, pf), li, pf).Compile();
4942
}
5043

5144
public override object GetFieldInterceptionProxy(object instanceToWrap)

src/NHibernate/Proxy/NHibernateProxyBuilder.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
7979
var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());
8080

8181
var lazyInitializerField = typeBuilder.DefineField("__lazyInitializer", LazyInitializerType, FieldAttributes.Private);
82-
var proxyFactoryField = typeBuilder.DefineField("_proxyFactory", typeof(IProxyFactory), FieldAttributes.Private);
82+
var proxyInfoField = typeBuilder.DefineField("_proxyInfo", typeof(NHibernateProxyFactoryInfo), FieldAttributes.Private);
8383

84-
ImplementConstructor(typeBuilder, parentType, lazyInitializerField, proxyFactoryField);
84+
ImplementConstructor(typeBuilder, parentType, lazyInitializerField, proxyInfoField);
8585

8686
// Provide a custom implementation of ISerializable
8787
// instead of redirecting it back to the interceptor
@@ -96,7 +96,7 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
9696
typeBuilder.SetCustomAttribute(customAttributeBuilder);
9797

9898
ImplementDeserializationConstructor(typeBuilder);
99-
ImplementGetObjectData(typeBuilder, proxyFactoryField);
99+
ImplementGetObjectData(typeBuilder, proxyInfoField);
100100

101101
var proxyType = typeBuilder.CreateTypeInfo();
102102

@@ -105,9 +105,9 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
105105
return proxyType;
106106
}
107107

108-
static void ImplementConstructor(TypeBuilder typeBuilder, System.Type parentType, FieldInfo lazyInitializerField, FieldInfo proxyFactoryField)
108+
static void ImplementConstructor(TypeBuilder typeBuilder, System.Type parentType, FieldInfo lazyInitializerField, FieldInfo proxyInfoField)
109109
{
110-
var constructor = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, new[] {LazyInitializerType, typeof(IProxyFactory)});
110+
var constructor = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, new[] {LazyInitializerType, typeof(NHibernateProxyFactoryInfo)});
111111

112112
var baseConstructor = parentType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, System.Type.EmptyTypes, null);
113113

@@ -129,7 +129,7 @@ static void ImplementConstructor(TypeBuilder typeBuilder, System.Type parentType
129129

130130
IL.Emit(OpCodes.Ldarg_0);
131131
IL.Emit(OpCodes.Ldarg_2);
132-
IL.Emit(OpCodes.Stfld, proxyFactoryField);
132+
IL.Emit(OpCodes.Stfld, proxyInfoField);
133133

134134
IL.Emit(OpCodes.Ret);
135135
}
@@ -145,7 +145,7 @@ static void ImplementDeserializationConstructor(TypeBuilder typeBuilder)
145145
IL.Emit(OpCodes.Ret);
146146
}
147147

148-
static void ImplementGetObjectData(TypeBuilder typeBuilder, FieldInfo proxyFactoryField)
148+
static void ImplementGetObjectData(TypeBuilder typeBuilder, FieldInfo proxyInfoField)
149149
{
150150
const MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig |
151151
MethodAttributes.Virtual;
@@ -163,9 +163,9 @@ static void ImplementGetObjectData(TypeBuilder typeBuilder, FieldInfo proxyFacto
163163
IL.Emit(OpCodes.Call, ReflectionCache.TypeMethods.GetTypeFromHandle);
164164
IL.Emit(OpCodes.Callvirt, ProxyFactory.setType);
165165

166-
//this.__proxyFactory
166+
//this._proxyInfo
167167
IL.Emit(OpCodes.Ldarg_0);
168-
IL.Emit(OpCodes.Ldfld, proxyFactoryField);
168+
IL.Emit(OpCodes.Ldfld, proxyInfoField);
169169

170170
//this.LazyInitializer.Identifier
171171
EmitGetLazyInitializer(IL);
@@ -174,7 +174,7 @@ static void ImplementGetObjectData(TypeBuilder typeBuilder, FieldInfo proxyFacto
174174
var constructor = typeof(NHibernateProxyObjectReference).GetConstructor(
175175
new[]
176176
{
177-
typeof(IProxyFactory),
177+
typeof(NHibernateProxyFactoryInfo),
178178
typeof(object),
179179
});
180180
IL.Emit(OpCodes.Newobj, constructor);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Runtime.Serialization;
5+
using System.Security;
6+
using NHibernate.Type;
7+
8+
namespace NHibernate.Proxy
9+
{
10+
[Serializable]
11+
public sealed class NHibernateProxyFactoryInfo : ISerializable
12+
{
13+
readonly string _entityName;
14+
readonly System.Type _persistentClass;
15+
readonly System.Type[] _interfaces;
16+
readonly MethodInfo _getIdentifierMethod;
17+
readonly MethodInfo _setIdentifierMethod;
18+
readonly IAbstractComponentType _componentIdType;
19+
20+
internal NHibernateProxyFactoryInfo(string entityName, System.Type persistentClass, System.Type[] interfaces, MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod, IAbstractComponentType componentIdType)
21+
{
22+
_entityName = entityName;
23+
_persistentClass = persistentClass;
24+
_interfaces = interfaces;
25+
_getIdentifierMethod = getIdentifierMethod;
26+
_setIdentifierMethod = setIdentifierMethod;
27+
_componentIdType = componentIdType;
28+
}
29+
30+
public IProxyFactory CreateProxyFactory()
31+
{
32+
var factory = new DefaultProxyFactory();
33+
factory.PostInstantiate(_entityName, _persistentClass, new HashSet<System.Type>(_interfaces), _getIdentifierMethod, _setIdentifierMethod, _componentIdType);
34+
return factory;
35+
}
36+
37+
NHibernateProxyFactoryInfo(SerializationInfo info, StreamingContext context)
38+
{
39+
_entityName = (string) info.GetValue(nameof(_entityName), typeof(string));
40+
_persistentClass = (System.Type) info.GetValue(nameof(_persistentClass), typeof(System.Type));
41+
_interfaces = (System.Type[]) info.GetValue(nameof(_interfaces), typeof(System.Type[]));
42+
_getIdentifierMethod = (MethodInfo) info.GetValue(nameof(_getIdentifierMethod), typeof(MethodInfo));
43+
_setIdentifierMethod = (MethodInfo) info.GetValue(nameof(_setIdentifierMethod), typeof(MethodInfo));
44+
_componentIdType = (IAbstractComponentType) info.GetValue(nameof(_componentIdType), typeof(IAbstractComponentType));
45+
}
46+
47+
[SecurityCritical]
48+
public void GetObjectData(SerializationInfo info, StreamingContext context)
49+
{
50+
info.AddValue(nameof(_entityName), _entityName);
51+
info.AddValue(nameof(_persistentClass), _persistentClass);
52+
info.AddValue(nameof(_interfaces), _interfaces);
53+
info.AddValue(nameof(_getIdentifierMethod), _getIdentifierMethod);
54+
info.AddValue(nameof(_setIdentifierMethod), _setIdentifierMethod);
55+
info.AddValue(nameof(_componentIdType), _componentIdType);
56+
}
57+
}
58+
}

src/NHibernate/Proxy/NHibernateProxyObjectReference.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ namespace NHibernate.Proxy
77
[Serializable]
88
public sealed class NHibernateProxyObjectReference : IObjectReference, ISerializable
99
{
10-
readonly IProxyFactory _proxyFactory;
10+
readonly NHibernateProxyFactoryInfo _proxyFactoryInfo;
1111
readonly object _identifier;
1212

13-
public NHibernateProxyObjectReference(IProxyFactory proxyFactory, object identifier)
13+
public NHibernateProxyObjectReference(NHibernateProxyFactoryInfo proxyFactoryInfo, object identifier)
1414
{
15-
_proxyFactory = proxyFactory;
15+
_proxyFactoryInfo = proxyFactoryInfo;
1616
_identifier = identifier;
1717
}
1818

1919
NHibernateProxyObjectReference(SerializationInfo info, StreamingContext context)
2020
{
21-
_proxyFactory = (IProxyFactory) info.GetValue(nameof(_proxyFactory), typeof(IProxyFactory));
21+
_proxyFactoryInfo = (NHibernateProxyFactoryInfo) info.GetValue(nameof(_proxyFactoryInfo), typeof(NHibernateProxyFactoryInfo));
2222
_identifier = info.GetValue(nameof(_identifier), typeof(object));
2323
}
2424

2525
[SecurityCritical]
2626
public object GetRealObject(StreamingContext context)
2727
{
28-
return _proxyFactory.GetProxy(_identifier, null);
28+
return _proxyFactoryInfo.CreateProxyFactory().GetProxy(_identifier, null);
2929
}
3030

3131
[SecurityCritical]
3232
public void GetObjectData(SerializationInfo info, StreamingContext context)
3333
{
34-
info.AddValue(nameof(_proxyFactory), _proxyFactory);
34+
info.AddValue(nameof(_proxyFactoryInfo), _proxyFactoryInfo);
3535
info.AddValue(nameof(_identifier), _identifier);
3636
}
3737
}

0 commit comments

Comments
 (0)