3
3
using System . Security ;
4
4
using NHibernate . Impl ;
5
5
using NHibernate . Persister . Entity ;
6
- using NHibernate . Type ;
7
6
8
7
namespace NHibernate . Engine
9
8
{
9
+ //TODO 6.0: Remove IDeserializationCallback interface
10
10
/// <summary>
11
11
/// A globally unique identifier of an instance, consisting of the user-visible identifier
12
12
/// and the identifier space (eg. tablename)
@@ -17,15 +17,14 @@ public sealed class EntityKey : IDeserializationCallback, ISerializable, IEquata
17
17
private readonly object identifier ;
18
18
private readonly IEntityPersister _persister ;
19
19
// hashcode may vary among processes, they cannot be stored and have to be re-computed after deserialization
20
- [ NonSerialized ]
21
- private int ? _hashCode ;
20
+ private readonly int _hashCode ;
22
21
23
22
/// <summary> Construct a unique identifier for an entity class instance</summary>
24
23
public EntityKey ( object id , IEntityPersister persister )
25
24
{
26
25
identifier = id ?? throw new AssertionFailure ( "null identifier" ) ;
27
26
_persister = persister ;
28
- _hashCode = GenerateHashCode ( ) ;
27
+ _hashCode = GenerateHashCode ( persister , id ) ;
29
28
}
30
29
31
30
private EntityKey ( SerializationInfo info , StreamingContext context )
@@ -34,6 +33,7 @@ private EntityKey(SerializationInfo info, StreamingContext context)
34
33
var factory = ( ISessionFactoryImplementor ) info . GetValue ( nameof ( _persister . Factory ) , typeof ( ISessionFactoryImplementor ) ) ;
35
34
var entityName = ( string ) info . GetValue ( nameof ( EntityName ) , typeof ( string ) ) ;
36
35
_persister = factory . GetEntityPersister ( entityName ) ;
36
+ _hashCode = GenerateHashCode ( _persister , identifier ) ;
37
37
}
38
38
39
39
public bool IsBatchLoadable => _persister . IsBatchLoadable ;
@@ -66,26 +66,16 @@ public bool Equals(EntityKey other)
66
66
67
67
public override int GetHashCode ( )
68
68
{
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 ( ) ;
69
+ return _hashCode ;
74
70
}
75
71
76
- /// <inheritdoc />
77
- public void OnDeserialization ( object sender )
78
- {
79
- _hashCode = GenerateHashCode ( ) ;
80
- }
81
-
82
- private int GenerateHashCode ( )
72
+ private static int GenerateHashCode ( IEntityPersister persister , object id )
83
73
{
84
74
int result = 17 ;
85
75
unchecked
86
76
{
87
- result = 37 * result + RootEntityName . GetHashCode ( ) ;
88
- result = 37 * result + _persister . IdentifierType . GetHashCode ( identifier , _persister . Factory ) ;
77
+ result = 37 * result + persister . RootEntityName . GetHashCode ( ) ;
78
+ result = 37 * result + persister . IdentifierType . GetHashCode ( id , persister . Factory ) ;
89
79
}
90
80
return result ;
91
81
}
@@ -96,11 +86,15 @@ public override string ToString()
96
86
}
97
87
98
88
[ SecurityCritical ]
99
- public void GetObjectData ( SerializationInfo info , StreamingContext context )
89
+ void ISerializable . GetObjectData ( SerializationInfo info , StreamingContext context )
100
90
{
101
91
info . AddValue ( nameof ( Identifier ) , identifier ) ;
102
92
info . AddValue ( nameof ( _persister . Factory ) , _persister . Factory ) ;
103
93
info . AddValue ( nameof ( EntityName ) , EntityName ) ;
104
94
}
95
+
96
+ [ Obsolete ( "IDeserializationCallback interface has no usages and will be removed in a future version" ) ]
97
+ public void OnDeserialization ( object sender )
98
+ { }
105
99
}
106
100
}
0 commit comments