温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:LiveBlog v1.0测试版源码
当前文件:
LiveBlog/LiveBlog.Core/BusinessBase.cs,打开代码结构图
LiveBlog/LiveBlog.Core/BusinessBase.cs,打开代码结构图1Using#region Using 2
3
using System; 4
using System.Text; 5
using System.ComponentModel; 6
using System.Collections.Specialized; 7
using System.Globalization; 8
9
#endregion 10
11
namespace LiveBlog.Core 12
...{ 13
/**//// <summary> 14
/// This is the base class from which most business objects will be derived. 15
/// To create a business object, inherit from this class. 16
/// </summary> 17
/// <typeparam name="TYPE">The type of the derived class.</typeparam> 18
/// <typeparam name="KEY">The type of the Id property.</typeparam> 19
[Serializable] 20
public abstract class BusinessBase<TYPE, KEY> : IDataErrorInfo, INotifyPropertyChanged, IChangeTracking, IDisposable where TYPE : BusinessBase<TYPE, KEY>, new() 21
...{ 22
23
Properties#region Properties 24
25
private KEY _Id; 26
/**//// <summary> 27
/// Gets the unique Identification of the object. 28
/// </summary> 29
public KEY Id 30
...{ 31
get ...{ return _Id; } 32
set ...{ _Id = value; } 33
} 34
35
private DateTime _DateCreated = DateTime.MinValue; 36
/**//// <summary> 37
/// The date on which the instance was created. 38
/// </summary> 39
public DateTime DateCreated 40
...{ 41
get 42
...{ 43
if (_DateCreated == DateTime.MinValue) 44
return _DateCreated; 45
46
return _DateCreated.AddHours(BlogSettings.Instance.Timezone); 47
} 48
set 49
...{ 50
if (_DateCreated != value) MarkChanged("DateCreated"); 51
_DateCreated = value; 52
} 53
} 54
55
private DateTime _DateModified = DateTime.MinValue; 56
/**//// <summary> 57
/// The date on which the instance was modified. 58
/// </summary> 59
public DateTime DateModified 60
...{ 61
get 62
...{ 63
if (_DateModified == DateTime.MinValue) 64
return _DateModified; 65
66
return _DateModified.AddHours(BlogSettings.Instance.Timezone); 67
} 68
set ...{ _DateModified = value; } 69
} 70
71
#endregion 72
73
IsNew, IsDeleted, IsChanged#region IsNew, IsDeleted, IsChanged 74
75
private bool _IsNew = true; 76
/**//// <summary> 77
/// Gets if this is a new object, False if it is a pre-existing object. 78
/// </summary> 79
public bool IsNew 80
...{ 81
get ...{ return _IsNew; } 82
} 83
84
private bool _IsDeleted; 85
/**//// <summary> 86
/// Gets if this object is marked for deletion. 87
/// </summary> 88
public bool IsDeleted 89
...{ 90
get ...{ return _IsDeleted; } 91
} 92
93
private bool _IsChanged = true; 94
/**//// <summary> 95
/// Gets if this object's data has been changed. 96
/// </summary> 97
public virtual bool IsChanged 98
...{ 99
get ...{ return _IsChanged; } 100
} 101
102
/**//// <summary> 103
/// Marks the object for deletion. It will then be 104
/// deleted when the object's Save() method is called. 105
/// </summary> 106
public void Delete() 107
...{ 108
_IsDeleted = true; 109
_IsChanged = true; 110
} 111
112
private StringCollection _ChangedProperties = new StringCollection(); 113
/**//// <summary> 114
/// A collection of the properties that have 115
/// been marked as being dirty. 116
/// </summary> 117
protected virtual StringCollection ChangedProperties 118
...{ 119
get ...{ return _ChangedProperties; } 120
} 121
122
/**//// <summary> 123
/// Marks an object as being dirty, or changed. 124
/// </summary> 125
/// <param name="propertyName">The name of the property to mark dirty.</param> 126
protected virtual void MarkChanged(string propertyName) 127
...{ 128
_IsChanged = true; 129
if (!_ChangedProperties.Contains(propertyName)) 130
...{ 131
_ChangedProperties.Add(propertyName); 132
} 133
134
OnPropertyChanged(propertyName); 135
} 136
137
/**//// <summary> 138
/// Marks the object as being an clean, 139
/// which means not dirty. 140
/// </summary> 141
public virtual void MarkOld() 142
...{ 143
_IsChanged = false; 144
_IsNew = false; 145
_ChangedProperties.Clear(); 146
} 147
148
/**////// <summary> 149
///// Check whether or not the specified property has been changed 150
///// </summary> 151
///// <param name="propertyName">The name of the property to check.</param> 152
//protected bool IsPropertyDirty(string propertyName) 153
//{ 154
// return DirtyProperties.Contains(propertyName.ToLowerInvariant()); 155
//} 156
157
/**////// <summary> 158
///// Check whether or not the specified properties has been changed 159
///// </summary> 160
///// <param name="propertyNames">The names of the properties to check.</param> 161
///// <returns>True if all of the specified properties have been changed.</returns> 162
//protected bool IsPropertyDirty(string[] propertyNames) 163
//{ 164
// foreach (string name in propertyNames) 165
// { 166
// if (!DirtyProperties.Contains(name.ToUpperInvariant())) 167
// { 168
// return false; 169
// } 170
// } 171
172
// return true; 173
//} 174
175
#endregion 176
177
Validation#region Validation 178
179
private StringDictionary _BrokenRules = new StringDictionary(); 180
181
/**//// <summary> 182
/// Add or remove a broken rule. 183
/// </summary> 184
/// <param name="propertyName">The name of the property.</param> 185
/// <param name="errorMessage">The description of the error</param> 186
/// <param name="isBroken">True if the validation rule is broken.</param> 187
protected virtual void AddRule(string propertyName, string errorMessage, bool isBroken) 188
...{ 189
if (isBroken) 190
...{ 191
_BrokenRules[propertyName] = errorMessage; 192
} 193
else 194
...{ 195
if (_BrokenRules.ContainsKey(propertyName)) 196
...{ 197
_BrokenRules.Remove(propertyName); 198
} 199
} 200
} 201
202
/**//// <summary> 203
/// Reinforces the business rules by adding additional rules to the 204
/// broken rules collection. 205
/// </summary> 206
protected abstract void ValidationRules(); 207
208
/**//// <summary> 209
/// Gets whether the object is valid or not. 210
/// </summary> 211
public bool IsValid 212
...{ 213
get 214
...{ 215
ValidationRules(); 216
return this._BrokenRules.Count == 0; 217
} 218
} 219
220
/**//// /// <summary> 221
/// If the object has broken business rules, use this property to get access 222
/// to the different validation messages. 223
/// </summary> 224
public virtual string ValidationMessage 225
...{ 226
get 227
...{ 228
if (!IsValid) 229
...{ 230
StringBuilder sb = new StringBuilder(); 231
foreach (string messages in this._BrokenRules.Values) 232
...{ 233
sb.AppendLine(messages); 234
} 235
236
return sb.ToString(); 237
} 238
239
return string.Empty; 240
} 241
} 242
243
#endregion 244
245
Methods#region Methods 246
247
/**//// <summary> 248
/// Loads an instance of the object based on the Id. 249
/// </summary> 250
/// <param name="id">The unique identifier of the object</param> 251
public static TYPE Load(KEY id) 252
...{ 253
TYPE instance = new TYPE(); 254
instance = instance.DataSelect(id); 255
instance.Id = id; 256
257
if (instance != null) 258
...{ 259
instance.MarkOld(); 260
return instance; 261
} 262
263
return null; 264
} 265
266
/**//// <summary> 267
/// Saves the object to the data store (inserts, updates or deletes). 268
/// </summary> 269
virtual public SaveAction Save() 270




