@@ -12,8 +12,13 @@ namespace NHibernate.Engine
12
12
/// and the identifier space (eg. tablename)
13
13
/// </summary>
14
14
[ Serializable ]
15
- public sealed class EntityKey : IDeserializationCallback , ISerializable , IEquatable < EntityKey >
15
+ public struct EntityKey : ISerializable , IEquatable < EntityKey >
16
16
{
17
+ public static EntityKey Null { get ; } = new EntityKey ( ) ;
18
+
19
+ public bool IsNull => identifier == null ;
20
+ public bool IsNotNull => ! IsNull ;
21
+
17
22
private readonly object identifier ;
18
23
private readonly IEntityPersister _persister ;
19
24
// hashcode may vary among processes, they cannot be stored and have to be re-computed after deserialization
@@ -30,8 +35,16 @@ public EntityKey(object id, IEntityPersister persister)
30
35
private EntityKey ( SerializationInfo info , StreamingContext context )
31
36
{
32
37
identifier = info . GetValue ( nameof ( Identifier ) , typeof ( object ) ) ;
38
+ if ( identifier == null )
39
+ {
40
+ _hashCode = 0 ;
41
+ _persister = null ;
42
+ return ;
43
+ }
44
+
33
45
var factory = ( ISessionFactoryImplementor ) info . GetValue ( nameof ( _persister . Factory ) , typeof ( ISessionFactoryImplementor ) ) ;
34
- var entityName = ( string ) info . GetValue ( nameof ( EntityName ) , typeof ( string ) ) ;
46
+ var entityName = info . GetString ( nameof ( EntityName ) ) ;
47
+
35
48
_persister = factory . GetEntityPersister ( entityName ) ;
36
49
_hashCode = GenerateHashCode ( _persister , identifier ) ;
37
50
}
@@ -54,10 +67,8 @@ public override bool Equals(object other)
54
67
55
68
public bool Equals ( EntityKey other )
56
69
{
57
- if ( other == null )
58
- {
59
- return false ;
60
- }
70
+ if ( other . IsNull )
71
+ return IsNull ;
61
72
62
73
return
63
74
other . RootEntityName . Equals ( RootEntityName )
@@ -82,13 +93,18 @@ private static int GenerateHashCode(IEntityPersister persister, object id)
82
93
83
94
public override string ToString ( )
84
95
{
85
- return "EntityKey" + MessageHelper . InfoString ( _persister , Identifier , _persister . Factory ) ;
96
+ return IsNull
97
+ ? Util . StringHelper . NullObject
98
+ : "EntityKey" + MessageHelper . InfoString ( _persister , Identifier , _persister ? . Factory ) ;
86
99
}
87
100
88
101
[ SecurityCritical ]
89
102
void ISerializable . GetObjectData ( SerializationInfo info , StreamingContext context )
90
103
{
91
104
info . AddValue ( nameof ( Identifier ) , identifier ) ;
105
+ if ( identifier == null )
106
+ return ;
107
+
92
108
info . AddValue ( nameof ( _persister . Factory ) , _persister . Factory ) ;
93
109
info . AddValue ( nameof ( EntityName ) , EntityName ) ;
94
110
}
0 commit comments