Skip to content

Commit c353f3c

Browse files
committed
Remove IDeserializationCallback from EntityKey
1 parent 14c5cdb commit c353f3c

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

src/NHibernate/Engine/EntityKey.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Security;
44
using NHibernate.Impl;
55
using NHibernate.Persister.Entity;
6-
using NHibernate.Type;
76

87
namespace NHibernate.Engine
98
{
@@ -12,20 +11,19 @@ namespace NHibernate.Engine
1211
/// and the identifier space (eg. tablename)
1312
/// </summary>
1413
[Serializable]
15-
public sealed class EntityKey : IDeserializationCallback, ISerializable, IEquatable<EntityKey>
14+
public sealed class EntityKey : ISerializable, IEquatable<EntityKey>
1615
{
1716
private readonly object identifier;
1817
private readonly IEntityPersister _persister;
1918
// hashcode may vary among processes, they cannot be stored and have to be re-computed after deserialization
20-
[NonSerialized]
21-
private int? _hashCode;
19+
private readonly int _hashCode;
2220

2321
/// <summary> Construct a unique identifier for an entity class instance</summary>
2422
public EntityKey(object id, IEntityPersister persister)
2523
{
2624
identifier = id ?? throw new AssertionFailure("null identifier");
2725
_persister = persister;
28-
_hashCode = GenerateHashCode();
26+
_hashCode = GenerateHashCode(persister, id);
2927
}
3028

3129
private EntityKey(SerializationInfo info, StreamingContext context)
@@ -34,6 +32,7 @@ private EntityKey(SerializationInfo info, StreamingContext context)
3432
var factory = (ISessionFactoryImplementor) info.GetValue(nameof(_persister.Factory), typeof(ISessionFactoryImplementor));
3533
var entityName = (string) info.GetValue(nameof(EntityName), typeof(string));
3634
_persister = factory.GetEntityPersister(entityName);
35+
_hashCode = GenerateHashCode(_persister, identifier);
3736
}
3837

3938
public bool IsBatchLoadable => _persister.IsBatchLoadable;
@@ -66,26 +65,16 @@ public bool Equals(EntityKey other)
6665

6766
public override int GetHashCode()
6867
{
69-
// If the object is put in a set or dictionary during deserialization, the hashcode will not yet be
70-
// computed. Compute the hashcode on the fly. So long as this happens only during deserialization, there
71-
// will be no thread safety issues. For the hashcode to be always defined after deserialization, the
72-
// deserialization callback is used.
73-
return _hashCode ?? GenerateHashCode();
68+
return _hashCode;
7469
}
7570

76-
/// <inheritdoc />
77-
public void OnDeserialization(object sender)
78-
{
79-
_hashCode = GenerateHashCode();
80-
}
81-
82-
private int GenerateHashCode()
71+
private static int GenerateHashCode(IEntityPersister persister, object id)
8372
{
8473
int result = 17;
8574
unchecked
8675
{
87-
result = 37 * result + RootEntityName.GetHashCode();
88-
result = 37 * result + _persister.IdentifierType.GetHashCode(identifier, _persister.Factory);
76+
result = 37 * result + persister.RootEntityName.GetHashCode();
77+
result = 37 * result + persister.IdentifierType.GetHashCode(id, persister.Factory);
8978
}
9079
return result;
9180
}

0 commit comments

Comments
 (0)