/
Northwind.cs
144 lines (127 loc) · 5.07 KB
/
Northwind.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
using System;
using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using System.Collections.Generic;
namespace Northwind {
[NonPersistent]
public class Base : XPLiteObject {
public Base(Session session) : base(session) { }
private void UpdateAudit(Action act) {
Audit audit = new Audit(Session);
audit.Action = act;
audit.Date = DateTime.Now;
audit.AuditedRecord = string.Format("{0}({1})", ClassInfo.FullName, Session.GetKeyValue(this));
audit.User = Environment.UserName;
foreach(Change change in changes) {
ModificationInfo modInfo = new ModificationInfo(Session);
modInfo.Audit = audit;
modInfo.PropertyName = change.PropertyName;
modInfo.OldValue = change.PrevValue;
modInfo.NewValue = change.Value;
modInfo.Save();
}
audit.Save();
}
protected override void OnSaving() {
base.OnSaving();
if(Session.IsNewObject(this)) UpdateAudit(Action.Insert);
else UpdateAudit(Action.Update);
}
protected override void OnDeleting() {
base.OnDeleting();
Change change = new Change();
change.PropertyName = this.ClassInfo.KeyProperty.Name;
change.PrevValue = this.Session.GetKeyValue(this).ToString();
change.Value = "<DELETED>";
changes.Add(change);
UpdateAudit(Action.Delete);
}
private List<Change> changes = new List<Change>();
protected override void OnChanged(string propertyName, object oldValue, object newValue) {
base.OnChanged(propertyName, oldValue, newValue);
Change change = new Change();
change.PropertyName = propertyName;
change.PrevValue = oldValue == null ? "<NULL>" : oldValue.ToString();
change.Value = newValue == null ? "<NULL>" : newValue.ToString();
changes.Add(change);
}
private struct Change {
public string PropertyName;
public string PrevValue;
public string Value;
}
}
public class Categories : Base {
int fCategoryID;
[Key(true)]
public int CategoryID {
get { return fCategoryID; }
set { SetPropertyValue<int>("CategoryID", ref fCategoryID, value); }
}
string fCategoryName;
[Size(15)]
public string CategoryName {
get { return fCategoryName; }
set { SetPropertyValue<string>("CategoryName", ref fCategoryName, value); }
}
string fDescription;
[Size(1073741823)]
public string Description {
get { return fDescription; }
set { SetPropertyValue<string>("Description", ref fDescription, value); }
}
public Categories(Session session) : base(session) { }
public Categories() : base(Session.DefaultSession) { }
public override void AfterConstruction() { base.AfterConstruction(); }
}
public class Audit : XPObject {
public Audit(Session session) : base(session) { }
private string fUser;
public string User {
get { return fUser; }
set { SetPropertyValue<string>("User", ref fUser, value); }
}
private DateTime fDate;
public DateTime Date {
get { return fDate; }
set { SetPropertyValue<DateTime>("Date", ref fDate, value); }
}
private Action fAction;
public Action Action {
get { return fAction; }
set { SetPropertyValue<Action>("Action", ref fAction, value); }
}
private string fAuditedRecord;
public string AuditedRecord {
get { return fAuditedRecord; }
set { SetPropertyValue<string>("AuditedRecord", ref fAuditedRecord, value); }
}
[Association("Audit-ModificationInfo")]
public XPCollection<ModificationInfo> Modifications { get { return GetCollection<ModificationInfo>("Modifications"); } }
}
public class ModificationInfo : XPObject {
public ModificationInfo(Session session) : base(session) { }
private string fPropertyName;
public string PropertyName {
get { return fPropertyName; }
set { SetPropertyValue<string>("PropertyName", ref fPropertyName, value); }
}
private string fOldValue;
public string OldValue {
get { return fOldValue; }
set { SetPropertyValue<string>("OldValue", ref fOldValue, value); }
}
private string fNewValue;
public string NewValue {
get { return fNewValue; }
set { SetPropertyValue<string>("NewValue", ref fNewValue, value); }
}
private Audit fAudit;
[Association("Audit-ModificationInfo")]
public Audit Audit {
get { return fAudit; }
set { SetPropertyValue<Audit>("Audit", ref fAudit, value); }
}
}
public enum Action { Insert, Update, Delete }
}