-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAuxiliaryObjects.cs
141 lines (135 loc) · 5.19 KB
/
AuxiliaryObjects.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
namespace NonPersistentObjectsDemo.Module.BusinessObjects {
[DefaultClassOptions]
[DefaultListViewOptions(true, NewItemRowPosition.Top)]
[DefaultProperty("Subject")]
[DevExpress.ExpressApp.DC.DomainComponent]
public class Message : NonPersistentObjectBase {
private int id;
[Browsable(false)]
[DevExpress.ExpressApp.Data.Key]
public int ID {
get { return id; }
}
public void SetKey(int id) {
this.id = id;
}
private Account _Sender;
public Account Sender {
get { return _Sender; }
set { SetPropertyValue(nameof(Sender), ref _Sender, value); }
}
private Account _Recepient;
public Account Recepient {
get { return _Recepient; }
set { SetPropertyValue(nameof(Recepient), ref _Recepient, value); }
}
private string _Subject;
public string Subject {
get { return _Subject; }
set { SetPropertyValue<string>(nameof(Subject), ref _Subject, value); }
}
private string _Body;
[FieldSize(-1)]
public string Body {
get { return _Body; }
set { SetPropertyValue<string>(nameof(Body), ref _Body, value); }
}
}
[DefaultClassOptions]
[DefaultListViewOptions(true, NewItemRowPosition.Top)]
[DefaultProperty("PublicName")]
[DevExpress.ExpressApp.DC.DomainComponent]
public class Account : NonPersistentObjectBase {
private string userName;
//[Browsable(false)]
[DevExpress.ExpressApp.ConditionalAppearance.Appearance("", Enabled = false, Criteria = "Not IsNewObject(This)")]
[RuleRequiredField]
[DevExpress.ExpressApp.Data.Key]
public string UserName {
get { return userName; }
set { userName = value; }
}
public void SetKey(string userName) {
this.userName = userName;
}
private string publicName;
public string PublicName {
get { return publicName; }
set { SetPropertyValue(nameof(PublicName), ref publicName, value); }
}
private BindingList<Message> _messagesSent;
public BindingList<Message> MessagesSent {
get {
if(_messagesSent == null) {
_messagesSent = new BindingList<Message>();
CriteriaOperator criteria = new BinaryOperator(
new OperandProperty("Sender.UserName"), new OperandValue(UserName),
BinaryOperatorType.Equal
);
var mgs = ObjectSpace.GetObjects<Message>(criteria);
foreach(var m in mgs) {
_messagesSent.Add(m);
}
}
return _messagesSent;
}
}
}
public class PostOfficeFactory : NonPersistentObjectFactoryBase {
private PostOfficeClient Storage => GlobalServiceProvider<PostOfficeClient>.GetService();
private bool isLoading = false;
private ObjectMap objectMap;
public PostOfficeFactory(ObjectMap objectMap) {
this.objectMap = objectMap;
}
public override object GetObjectByKey(Type objectType, object key) {
if(key == null) {
throw new ArgumentNullException(nameof(key));
}
if(Storage.Mappings.TryGetValue(objectType, out var mapping)) {
return WrapLoading(() => {
var loader = new DataStoreObjectLoader(Storage.Mappings, Storage.DataStore, objectMap);
return loader.LoadObjectByKey(objectType, key);
});
}
throw new NotImplementedException();
}
public override IEnumerable GetObjects(Type objectType, CriteriaOperator criteria, IList<DevExpress.Xpo.SortProperty> sorting) {
if(Storage.Mappings.TryGetValue(objectType, out var mapping)) {
return WrapLoading(() => {
var loader = new DataStoreObjectLoader(Storage.Mappings, Storage.DataStore, objectMap);
return loader.LoadObjects(objectType, criteria);
});
}
throw new NotImplementedException();
}
private T WrapLoading<T>(Func<T> doer) {
if(isLoading) {
throw new InvalidOperationException();
}
isLoading = true;
try {
return doer.Invoke();
}
finally {
isLoading = false;
}
}
public override void SaveObjects(ICollection toInsert, ICollection toUpdate, ICollection toDelete) {
var saver = new DataStoreObjectSaver(Storage.Mappings, Storage.DataStore);
saver.SaveObjects(toInsert, toUpdate, toDelete);
}
}
}