-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbizObject.cs
More file actions
191 lines (173 loc) · 6.16 KB
/
bizObject.cs
File metadata and controls
191 lines (173 loc) · 6.16 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Microsoft.Data.SqlClient;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace CPUFramework
{
public class bizObject <T>: INotifyPropertyChanged where T : bizObject<T>, new()
{
string _typename = ""; string _tablename = ""; string _getsproc = ""; string _updatesproc = ""; string _deletesproc = "";
string _primarykeyname = ""; string _primarykeyparamname = "";
DataTable _datatable = new();
List<PropertyInfo> _properties = new();
public event PropertyChangedEventHandler? PropertyChanged;
public bizObject()
{
Type t = this.GetType();
_typename = t.Name;
_tablename = _typename;
if (_tablename.ToLower().StartsWith("biz"))
{
_tablename = _tablename.Substring(3);
}
_getsproc = _tablename + "Get";
_updatesproc = _tablename + "Update";
_deletesproc = _tablename + "Delete";
_primarykeyname = _tablename + "Id";
_primarykeyparamname = "@" + _primarykeyname;
_properties = t.GetProperties().ToList<PropertyInfo>();
}
public DataTable Load(int primarykeyvalue)
{
DataTable dt = new();
SqlCommand cmd = SQLUtility.GetSQLCommand(_getsproc);
SQLUtility.SetParamValue(cmd, _primarykeyparamname, primarykeyvalue);
dt = SQLUtility.GetDataTable(cmd);
if (dt.Rows.Count > 0)
{
LoadProps(dt.Rows[0]);
}
_datatable = dt;
return dt;
}
public List<T> GetList(bool includeblank = false)
{
List<T> lst = new();
SqlCommand cmd = SQLUtility.GetSQLCommand(_getsproc);
SQLUtility.SetParamValue(cmd, "@All", 1);
SQLUtility.SetParamValue(cmd, "@IncludeBlank", includeblank);
var dt = SQLUtility.GetDataTable(cmd);
return GetListFromDataTable(dt);
}
protected List<T> GetListFromDataTable(DataTable dt)
{
List<T> lst = new();
foreach (DataRow dr in dt.Rows)
{
T obj = new T();
obj.LoadProps(dr);
lst.Add(obj);
}
return lst;
}
protected void LoadProps(DataRow dr)
{
foreach (DataColumn col in dr.Table.Columns)
{
SetProp(col.ColumnName, dr[col.ColumnName]);
}
}
public void Delete(int id)
{
this.ErrorMessage = "";
SqlCommand cmd = SQLUtility.GetSQLCommand(_deletesproc);
SQLUtility.SetParamValue(cmd, _primarykeyparamname, id);
SQLUtility.ExecuteSQL(cmd);
}
public void Delete()
{
this.ErrorMessage = "";
PropertyInfo? prop = GetProp(_primarykeyname, true, false);
if (prop != null)
{
int? id = (int?)prop.GetValue(this);
if (id != null)
{
this.Delete((int)id);
}
}
}
public void Delete(DataTable datatable)
{
int id = (int)datatable.Rows[0][_primarykeyname];
this.Delete(id);
}
public void Save()
{
this.ErrorMessage = "";
SqlCommand cmd = SQLUtility.GetSQLCommand(_updatesproc);
foreach (SqlParameter param in cmd.Parameters)
{
var prop = GetProp(param.ParameterName, true, false);
if (prop != null)
{
object? val = prop.GetValue(this);
if (val == null)
{
val = DBNull.Value;
}
param.Value = val;
}
}
SQLUtility.ExecuteSQL(cmd);
foreach (SqlParameter param in cmd.Parameters)
{
if(param.Direction == ParameterDirection.Output || param.Direction == ParameterDirection.InputOutput)
{
SetProp(param.ParameterName, param.Value);
}
}
}
public void Save(DataTable datatable)
{
this.ErrorMessage = "";
if (datatable.Rows.Count == 0)
{
throw new Exception($"Cannot call {_tablename} Save method because there ar no rows in the table");
}
DataRow r = datatable.Rows[0];
SQLUtility.SaveDataRow(r, _updatesproc);
}
private PropertyInfo? GetProp(string propname, bool forread, bool forwrite)
{
propname = propname.ToLower();
if (propname.StartsWith("@"))
{
propname = propname.Substring(1);
}
PropertyInfo? prop = _properties.FirstOrDefault(p =>
p.Name.ToLower() == propname
&& (forread == false || p.CanRead == true)
&& (forwrite == false || p.CanWrite == true)
);
return prop;
}
private void SetProp(string propname, object value)
{
var prop = GetProp(propname, false, true);
if (prop != null)
{
if (value == DBNull.Value)
{
value = null;
}
try
{
prop.SetValue(this, value);
}
catch (Exception ex)
{
string msg = $"{_typename}.{prop.Name} is being set to {value?.ToString()} and that is the wrong data type. {ex.Message}";
throw new CPUDevException(msg, ex);
}
}
}
protected string GetSprocName { get => _getsproc; }
protected void InvokePropertyChanged([CallerMemberName] string propertyname = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
public string ErrorMessage { get; set; } = "";
}
}