-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathVersionValue.cs
123 lines (106 loc) · 3.09 KB
/
VersionValue.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
using System;
using NHibernate.Id;
namespace NHibernate.Engine
{
/// <summary>
/// A strategy for determining if a version value is an version 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 VersionValue
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(VersionValue));
private readonly object value;
/// <summary></summary>
protected VersionValue()
{
value = null;
}
/// <summary>
/// Assume the transient instance is newly instantiated if its version is null or
/// equal to <c>Value</c>
/// </summary>
/// <param name="value"></param>
public VersionValue(object value)
{
this.value = value;
}
/// <summary>
/// Does the given identifier belong to a new instance
/// </summary>
public virtual bool? IsUnsaved(object version)
{
if (log.IsDebugEnabled())
{
log.Debug("unsaved-value: {0}", value);
}
return version == null || version.Equals(value);
}
public virtual object GetDefaultValue(object currentValue)
{
return value;
}
private const string UnsavedStrategyLog = "version unsaved-value strategy {0}";
/// <summary>
/// Assume the transient instance is newly instantiated if the version
/// is null, otherwise assume it is a detached instance.
/// </summary>
public static VersionValue VersionSaveNull = new VersionSaveNullClass();
private class VersionSaveNullClass : VersionValue
{
public override bool? IsUnsaved(object version)
{
log.Debug(UnsavedStrategyLog, "NULL");
return version == null;
}
public override object GetDefaultValue(object currentValue)
{
return null;
}
}
/// <summary>
/// Assume the transient instance is newly instantiated if the version
/// is null, otherwise defer to the identifier unsaved-value.
/// </summary>
public static VersionValue VersionUndefined = new VersionUndefinedClass();
private class VersionUndefinedClass : VersionValue
{
public override bool? IsUnsaved(object version)
{
log.Debug(UnsavedStrategyLog, "UNDEFINED");
if (version == null)
return true;
else
return null;
}
public override object GetDefaultValue(object currentValue)
{
return currentValue;
}
}
/// <summary>
/// Assume the transient instance is newly instantiated if the identifier
/// is null.
/// </summary>
public static VersionValue VersionNegative = new VersionNegativeClass();
private class VersionNegativeClass : VersionValue
{
public override bool? IsUnsaved(object version)
{
log.Debug(UnsavedStrategyLog, "NEGATIVE");
if (version is short || version is int || version is long)
{
return Convert.ToInt64(version) < 0L;
}
else
{
throw new MappingException("unsaved-value strategy NEGATIVE may only be used with short, int and long types");
}
}
public override object GetDefaultValue(object currentValue)
{
return IdentifierGeneratorFactory.CreateNumber(-1L, currentValue.GetType());
}
}
}
}