-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathEntityUniqueKey.cs
116 lines (103 loc) · 3.94 KB
/
EntityUniqueKey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Runtime.Serialization;
using NHibernate.Impl;
using NHibernate.Type;
namespace NHibernate.Engine
{
/// <summary>
/// Used to uniquely key an entity instance in relation to a particular session
/// by some unique property reference, as opposed to identifier.
/// Unique information consists of the entity-name, the referenced
/// property name, and the referenced property value.
/// </summary>
/// <seealso cref="EntityKey"/>
[Serializable]
public class EntityUniqueKey : IDeserializationCallback
{
private readonly string entityName;
private readonly string uniqueKeyName;
private readonly object key;
private readonly IType keyType;
private readonly ISessionFactoryImplementor _factory;
// hashcode may vary among processes, they cannot be stored and have to be re-computed after deserialization
[NonSerialized]
private int? _hashCode;
// 6.0 TODO: rename keyType as semiResolvedKeyType. That is not the responsibility of this class to make any
// assumption on the key type being semi-resolved or not, that is the responsibility of its callers.
public EntityUniqueKey(string entityName, string uniqueKeyName, object semiResolvedKey, IType keyType, ISessionFactoryImplementor factory)
{
if (string.IsNullOrEmpty(entityName))
throw new ArgumentNullException("entityName");
if (string.IsNullOrEmpty(uniqueKeyName))
throw new ArgumentNullException("uniqueKeyName");
if (semiResolvedKey == null)
throw new ArgumentNullException("semiResolvedKey");
if (keyType == null)
throw new ArgumentNullException("keyType");
this.entityName = entityName;
this.uniqueKeyName = uniqueKeyName;
key = semiResolvedKey;
this.keyType = keyType;
_factory = factory;
// No need to delay computation here, but we need the lazy for the deserialization case.
_hashCode = GenerateHashCode();
}
public string EntityName
{
get { return entityName; }
}
public object Key
{
get { return key; }
}
public string UniqueKeyName
{
get { return uniqueKeyName; }
}
public override int GetHashCode()
{
// If the object is put in a set or dictionary during deserialization, the hashcode will not yet be
// computed. Compute the hashcode on the fly. So long as this happens only during deserialization, there
// will be no thread safety issues. For the hashcode to be always defined after deserialization, the
// deserialization callback is used.
return _hashCode ?? GenerateHashCode();
}
/// <inheritdoc />
public void OnDeserialization(object sender)
{
_hashCode = GenerateHashCode();
}
public int GenerateHashCode()
{
int result = 17;
unchecked
{
result = 37 * result + entityName.GetHashCode();
result = 37 * result + uniqueKeyName.GetHashCode();
result = 37 * result + keyType.GetHashCode(key, _factory);
}
return result;
}
public override bool Equals(object obj)
{
if(ReferenceEquals(this,obj)) return true;
return Equals(obj as EntityUniqueKey);
}
public bool Equals(EntityUniqueKey that)
{
return that != null && that.EntityName.Equals(entityName) && that.UniqueKeyName.Equals(uniqueKeyName) &&
// Normally entities are cached by semi-resolved type only, but the initial fix of #1226 causes them to
// be cached by type too. This may then cause issues (including Stack Overflow Exception) when this
// happens with the that.keyType being an entity type while its value is an uninitialized proxy: if
// this.keyType is not an entity type too, its IsEqual will trigger the proxy loading.
// So we need to short-circuit on keyType inequality, at least till Loader.CacheByUniqueKey is removed.
// 6.0 TODO: consider removing the keyType.Equals(that.keyType) check, see above comment.
keyType.Equals(that.keyType) &&
keyType.IsEqual(that.key, key);
}
public override string ToString()
{
return string.Format("EntityUniqueKey{0}", MessageHelper.InfoString(entityName, uniqueKeyName, key));
}
}
}