-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathIdentifierValue.cs
125 lines (106 loc) · 2.91 KB
/
IdentifierValue.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
117
118
119
120
121
122
123
124
125
namespace NHibernate.Engine
{
/// <summary>
/// A strategy for determining if an identifier value is an identifier of a new
/// transient instance or a previously persistent transient instance. The strategy
/// is determined by the <c>Unsaved-Value</c> attribute in the mapping file.
/// </summary>
public class IdentifierValue
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(IdentifierValue));
private readonly object value;
/// <summary></summary>
protected IdentifierValue()
{
value = null;
}
/// <summary>
/// Assume the transient instance is newly instantiated if its identifier is null or
/// equal to <c>Value</c>
/// </summary>
/// <param name="value"></param>
public IdentifierValue(object value)
{
this.value = value;
}
/// <summary>
/// Does the given identifier belong to a new instance
/// </summary>
public virtual bool? IsUnsaved(object id)
{
if (log.IsDebugEnabled())
{
log.Debug("unsaved-value: {0}", value);
}
return id == null || id.Equals(value);
}
public virtual object GetDefaultValue(object currentValue)
{
return value;
}
private const string UnsavedStrategyLog = "unsaved-value strategy {0}";
/// <summary>
/// Always assume the transient instance is newly instantiated
/// </summary>
public static readonly IdentifierValue SaveAny = new SaveAnyClass();
private class SaveAnyClass : IdentifierValue
{
public override bool? IsUnsaved(object id)
{
log.Debug(UnsavedStrategyLog, "ANY");
return true;
}
public override object GetDefaultValue(object currentValue)
{
return currentValue;
}
}
/// <summary>
/// Never assume that transient instance is newly instantiated
/// </summary>
public static readonly IdentifierValue SaveNone = new SaveNoneClass();
private class SaveNoneClass : IdentifierValue
{
public override bool? IsUnsaved(object id)
{
log.Debug(UnsavedStrategyLog, "NONE");
return false;
}
public override object GetDefaultValue(object currentValue)
{
return currentValue;
}
}
/// <summary>
/// Assume the transient instance is newly instantiated if the identifier
/// is null.
/// </summary>
public static readonly IdentifierValue SaveNull = new SaveNullClass();
private class SaveNullClass : IdentifierValue
{
public override bool? IsUnsaved(object id)
{
log.Debug(UnsavedStrategyLog, "NULL");
return id == null;
}
public override object GetDefaultValue(object currentValue)
{
return null;
}
}
/// <summary> Assume nothing.</summary>
public static readonly IdentifierValue Undefined = new UndefinedClass();
public class UndefinedClass : IdentifierValue
{
public override bool? IsUnsaved(object id)
{
log.Debug(UnsavedStrategyLog, "UNDEFINED");
return null;
}
public override object GetDefaultValue(object currentValue)
{
return null;
}
}
}
}