forked from PeteGoo/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelBase.cs
More file actions
75 lines (68 loc) · 2.43 KB
/
ModelBase.cs
File metadata and controls
75 lines (68 loc) · 2.43 KB
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
using System;
using System.Runtime.Serialization;
namespace ReactiveUI.Serialization
{
#if WINDOWS_PHONE
/// <summary>
/// ModelBase represents the base implementation of ISerializableItem and
/// handles a lot of the infrastructure plumbing around maintaining the
/// Content Hash.
///
/// For objects who are frequently serialized/deserialized, derived classes
/// should override CalculateHash and implement it in a more specific
/// manner.
/// </summary>
[DataContract]
public abstract class ModelBase : ReactiveObject, ISerializableItem
#else
/// <summary>
/// ModelBase represents the base implementation of ISerializableItem and
/// handles a lot of the infrastructure plumbing around maintaining the
/// Content Hash.
///
/// For objects who are frequently serialized/deserialized, derived classes
/// should override CalculateHash and implement it in a more specific
/// manner.
/// </summary>
[DataContract]
public abstract class ModelBase : ReactiveValidatedObject, ISerializableItem
#endif
{
static Guid inProgressGuid = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
[IgnoreDataMember] Guid _ContentHash;
[IgnoreDataMember]
public Guid ContentHash {
get {
if (_ContentHash == Guid.Empty) {
// XXX: This is a blatant hack to make sure that the JSON serializer
// doesn't end up recursively calling CalculateHash
_ContentHash = inProgressGuid;
_ContentHash = CalculateHash();
}
return _ContentHash;
}
protected set { _ContentHash = value; }
}
public ModelBase()
{
setupModelBase();
}
[OnDeserialized]
void setupModelBase(StreamingContext sc) { setupModelBase(); }
void setupModelBase()
{
this.Log().InfoFormat("Deserialized ModelBase 0x{0:X}", this.GetHashCode());
Changed.Subscribe(_ => invalidateHash());
invalidateHash();
}
public virtual Guid CalculateHash()
{
return new Guid(this.ObjectContentsHash());
}
protected virtual void invalidateHash()
{
ContentHash = Guid.Empty;
}
}
}
// vim: tw=120 ts=4 sw=4 et :