-
Notifications
You must be signed in to change notification settings - Fork 2
/
NonPersistentObjectAdapter.cs
169 lines (165 loc) · 6.71 KB
/
NonPersistentObjectAdapter.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.Persistent.Base;
namespace NonPersistentObjectsDemo.Module.BusinessObjects {
public interface IAssignable<T> {
void Assign(T source);
}
public abstract class NonPersistentObjectAdapter<TObject, TKey> {
private NonPersistentObjectSpace objectSpace;
private Dictionary<TKey, TObject> objectMap;
protected NonPersistentObjectSpace ObjectSpace { get { return objectSpace; } }
public NonPersistentObjectAdapter(NonPersistentObjectSpace npos) {
this.objectSpace = npos;
objectSpace.ObjectsGetting += ObjectSpace_ObjectsGetting;
objectSpace.ObjectGetting += ObjectSpace_ObjectGetting;
objectSpace.ObjectByKeyGetting += ObjectSpace_ObjectByKeyGetting;
objectSpace.Reloaded += ObjectSpace_Reloaded;
objectSpace.ObjectReloading += ObjectSpace_ObjectReloading;
objectSpace.CustomCommitChanges += ObjectSpace_CustomCommitChanges;
objectMap = new Dictionary<TKey, TObject>();
}
protected virtual void GuardKeyNotEmpty(TObject obj) {
if(Object.Equals(GetKeyValue(obj), default(TKey)))
throw new InvalidOperationException(); // DEBUG
}
protected virtual TKey GetKeyValue(TObject obj) {
return (TKey)objectSpace.GetKeyValue(obj);
}
private void AcceptObject(TObject obj) {
TObject result;
GuardKeyNotEmpty(obj);
if(!objectMap.TryGetValue(GetKeyValue(obj), out result)) {
objectMap.Add(GetKeyValue(obj), obj);
}
else {
if(!Object.Equals(result, obj)) {
throw new InvalidOperationException();
}
}
}
protected TObject GetObjectByKey(TKey key) {
TObject result;
if(!objectMap.TryGetValue(key, out result)) {
result = LoadObjectByKey(key);
if(result != null) {
AcceptObject(result);
}
}
return result;
}
protected virtual TObject LoadObjectByKey(TKey key) {
throw new NotSupportedException();
}
private void ObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) {
if(e.Key != null) {
if(e.ObjectType == typeof(TObject)) {
e.Object = GetObjectByKey((TKey)e.Key);
}
}
}
private void ObjectSpace_ObjectsGetting(object sender, ObjectsGettingEventArgs e) {
if(e.ObjectType == typeof(TObject)) {
var objects = GetObjects();
e.Objects = (System.Collections.IList)objects;
}
}
protected virtual IList<TObject> GetObjects() {
throw new NotSupportedException();
}
private void ObjectSpace_Reloaded(object sender, EventArgs e) {
objectMap.Clear();
}
private void ObjectSpace_ObjectGetting(object sender, ObjectGettingEventArgs e) {
if(e.SourceObject is TObject) {
var obj = (TObject)e.SourceObject;
var link = e.SourceObject as IObjectSpaceLink;
if(link != null) {
GuardKeyNotEmpty(obj);
if(link.ObjectSpace == null) {
e.TargetObject = AcceptOrUpdate(obj);
}
else {
if(link.ObjectSpace.IsNewObject(obj)) {
if(link.ObjectSpace == objectSpace) {
e.TargetObject = e.SourceObject;
}
else {
e.TargetObject = null;
}
}
else {
if(link.ObjectSpace == objectSpace) {
e.TargetObject = AcceptOrUpdate(obj);
}
else {
TObject result;
if(!objectMap.TryGetValue(GetKeyValue(obj), out result)) {
result = LoadSameObject(obj);
if(result != null) {
AcceptObject(result);
}
}
e.TargetObject = result;
}
}
}
}
}
}
protected virtual bool ThrowOnAcceptingMismatchedObject { get { return false; } }
private TObject AcceptOrUpdate(TObject obj) {
var key = GetKeyValue(obj);
TObject result;
if(!objectMap.TryGetValue(key, out result)) {
objectMap.Add(key, obj);
result = obj;
}
else {
// if objectMap contains an object with the same key, assume SourceObject is a reloaded copy.
// then refresh contents of the found object and return it.
if(!Object.Equals(result, obj)) {
if(ThrowOnAcceptingMismatchedObject)
throw new InvalidOperationException();
if(result is IAssignable<TObject> a) {
a.Assign(obj);
}
}
}
return result;
}
protected virtual TObject LoadSameObject(TObject obj) {
return LoadObjectByKey(GetKeyValue(obj));
}
private void ObjectSpace_ObjectReloading(object sender, ObjectGettingEventArgs e) {
if(e.SourceObject is TObject) {
var tobj = (TObject)e.SourceObject;
e.TargetObject = ReloadObject(tobj);
}
}
protected virtual TObject ReloadObject(TObject obj) {
return obj;
}
private void ObjectSpace_CustomCommitChanges(object sender, HandledEventArgs e) {
var list = new List<TObject>();
foreach(var obj in objectSpace.ModifiedObjects) {
if(obj is TObject) {
list.Add((TObject)obj);
}
}
if(list.Count > 0) {
CommitChanges(list);
}
}
protected virtual void CommitChanges(List<TObject> objects) {
}
}
}