【Aspx应用开发平台教程】架构篇:解析微系统构件的组成和运作机制
时间:2010-10-10 来源:大道无痕
在Aspx应用开发平台的体系结构中,微系统构件是最基础的构件单元,所有的应用程序模块对象,无论是业务逻辑模块,还是Aspx应用开发平台本身都是采用不同的微系统构件,如同搭积木一样组装出来。
那么微系统构件和大家所熟悉的ORM数据实体对象有什么不同呢?Aspx应用开发平台的体系架构和运作机制又是什么样的?本文将为大家做详尽的讲解。
Aspx应用开发平台的微系统构件相当于一个微型的、由数据层、业务层和表现层的三层架构系统,其中业务层功能,是以一个实现了IDataEntityProvider(数据实体对象提供者)接口的数据对象来完成的,它相当于微系统构件的CPU。

{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
/// <summary>
/// 数据实体对象提供者接口
/// </summary>
/// <example>
/// <code source="..\ObjectService\IDataEntityProvider.cs"></code>
/// </example>
/// <remarks>
/// (1)映射数据表的元数据定义接口;<br/>
/// (2)对映射数据表的CRUD操作接口;<br/>
/// (3)CRUD操作的事件接口;<br/>
/// (4)数据访问对象的接口<br/>
/// </remarks>
public interface IDataEntityProvider
{
#region 数据驱动接口
/// <summary>
/// 数据访问对象
/// </summary>
DataAccess.IDataDriver DataAccess
{
set;
get;
}
#endregion
#region 数据操作接口
/// <summary>
/// 使用数据视图
/// </summary>
bool IsUseView
{
get;
}
/// <summary>
/// 新增数据
/// </summary>
/// <returns>新增数据列</returns>
DataRow Add();
/// <summary>
/// 清除数据
/// </summary>
void ClearData();
/// <summary>
/// 删除指定的数据列
/// </summary>
/// <param name="Rows">需要删除的数据列</param>
void Delete(List<DataRow> Rows);
/// <summary>
/// 删除指定的主键数组
/// </summary>
/// <param name="Keys">主键数组</param>
void Delete(ArrayList Keys);
/// <summary>
/// 删除指定的数据列
/// </summary>
/// <param name="Rows">需要删除的数据列</param>
void Delete(DataRow[] Rows);
/// <summary>
/// 删除指定的主键
/// </summary>
/// <param name="Key">主键</param>
void Delete(object Key);
/// <summary>
/// 获得指定主键的数据列
/// </summary>
/// <param name="Key">主键值</param>
/// <returns>数据列</returns>
DataRow Edit(object Key);
/// <summary>
/// 合并数据
/// </summary>
/// <param name="DataSet">合并数据集</param>
void Merge(System.Data.DataSet DataSet);
/// <summary>
/// 获得指定数据筛选条件的数据视图
/// </summary>
/// <param name="recordFilter">数据筛选条件</param>
/// <returns>数据视图</returns>
DataView GetDataView(string recordFilter);
/// <summary>
/// 获得指定数据筛选条件的数据视图,并排序
/// </summary>
/// <param name="recordFilter">数据筛选条件</param>
/// <param name="order">排序条件</param>
/// <returns>数据视图</returns>
DataView GetDataView(string recordFilter, string order);
/// <summary>
/// 获得指定范围和数据筛选条件的数据视图,并排序
/// </summary>
/// <param name="startRecord">开始记录位置</param>
/// <param name="maxRecord">记录数</param>
/// <param name="recordFilter">数据筛选条件</param>
/// <param name="order">排序条件</param>
/// <returns>数据视图</returns>
DataView GetDataView(int startRecord, int maxRecord, string recordFilter, string order);
/// <summary>
/// 获得指定范围和数据筛选条件的数据视图,并排序,同时指明是否从缓存中
/// </summary>
/// <param name="startRecord">开始记录位置</param>
/// <param name="maxRecord">记录数</param>
/// <param name="recordFilter">数据筛选条件</param>
/// <param name="order">排序条件</param>
/// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
/// <returns>数据视图</returns>
DataView GetDataView(int startRecord, int maxRecord, string recordFilter, string order, bool isLoad);
/// <summary>
/// 根据主键值,获得对应数据列的指定字段的值
/// </summary>
/// <param name="FieldName">字段名称</param>
/// <param name="key">主键值</param>
/// <returns>字段值</returns>
string GetNameById(string FieldName, object key);
/// <summary>
/// 根据数据筛选条件,获得对应的记录数
/// </summary>
/// <param name="recordFilter">数据筛选条件</param>
/// <returns>记录数</returns>
int GetRecordCount(string recordFilter);
/// <summary>
/// 获得分组数据
/// </summary>
/// <param name="groupColumn">分组栏目</param>
/// <param name="filter">数据筛选条件</param>
/// <param name="order">数据排序条件</param>
/// <returns>分组数据集</returns>
DataSet GetGroupCount(string groupColumn, string filter, string order);
/// <summary>
/// 将数据加载到DataSet
/// </summary>
/// <param name="dataSet">数据集</param>
/// <param name="recordFilter">数据筛选条件</param>
/// <param name="order">排序防守</param>
/// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
/// <returns>符合条件的记录数</returns>
int LoadData(System.Data.DataSet dataSet,string recordFilter, string order, bool isLoad);
/// <summary>
/// 分页加载数据到数据集
/// </summary>
/// <param name="dataSet">数据集</param>
/// <param name="pageIndex">页编号</param>
/// <param name="pageSize">页记录数</param>
/// <param name="recordFilter">数据筛选条件</param>
/// <param name="order">排序条件</param>
/// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
/// <returns>符合条件的记录数</returns>
int LoadData(System.Data.DataSet dataSet,int pageIndex, int pageSize, string recordFilter, string order, bool isLoad);
/// <summary>
/// 保存数据
/// </summary>
/// <param name="Row">数据列</param>
void Save(DataRow Row);
/// <summary>
/// 保存数据
/// </summary>
/// <param name="Rows">数据列范型数组</param>
void Save(List<DataRow> Rows);
/// <summary>
/// 保存数据
/// </summary>
/// <param name="Rows">数据列数组</param>
void Save(DataRow[] Rows);
/// <summary>
/// 选择出指定范围的数据列
/// </summary>
/// <param name="recordFilter">数据筛选条件</param>
/// <returns>符合条件的数据列数组</returns>
DataRow[] Select(string recordFilter);
/// <summary>
/// 选择出指定范围的数据列,并排序
/// </summary>
/// <param name="recordFilter">数据筛选条件</param>
/// <param name="order">排序条件</param>
/// <returns>符合条件并已经排序的数据列数组</returns>
DataRow[] Select(string recordFilter,string order);
/// <summary>
/// 选择出指定范围的数据列,并放入泛型数组中
/// </summary>
/// <param name="selectRows">数据列泛型数组</param>
/// <param name="recordFilter">数据筛选条件</param>
void Select(List<DataRow> selectRows, string recordFilter);
/// <summary>
/// 获得数据筛选条件
/// </summary>
/// <param name="fieldName">字段名称</param>
/// <param name="fieldValue">字段值</param>
/// <returns>数据筛选条件</returns>
string GetFilter(string fieldName, string fieldValue);
/// <summary>
/// 静态数据对象标志
/// </summary>
bool IsStatic { get; }
/// <summary>
/// 主键名称
/// </summary>
string KeyName { get; }
/// <summary>
/// 名称
/// </summary>
string Name { get; }
/// <summary>
/// 数据表
/// </summary>
DataTable Table { get; }
/// <summary>
/// 数据表名称
/// </summary>
string TableName { get; }
/// <summary>
/// 数据视图
/// </summary>
DataTable View { get; }
/// <summary>
/// 数据视图名称
/// </summary>
string ViewName { get; }
#endregion
#region 数据委托事件接口
/// <summary>
/// 数据添加事件
/// </summary>
Events.OnAddEvent OnAdd { get; set;}
/// <summary>
/// 数据已经删除事件
/// </summary>
Events.OnDeletedEvent OnDeleted { get;set; }
/// <summary>
/// 数据删除事件
/// </summary>
Events.OnDeletingEvent OnDeleting { get; set;}
/// <summary>
/// 数据编辑时事件
/// </summary>
Events.OnEditEvent OnEdit { get; set;}
/// <summary>
/// 数据已经保存后事件
/// </summary>
Events.OnSavedEvent OnSaved { get;set; }
/// <summary>
/// 数据保存委托事件
/// </summary>
Events.OnSavingEvent OnSaving { get; set; }
/// <summary>
/// 数据加载事件
/// </summary>
Events.OnLoadEvent OnLoad { get; set;}
#endregion
#region 界面UI接口
/// <summary>
/// 获得编辑页面Url
/// </summary>
/// <returns>编辑页面Url</returns>
string GetEditLogic();
/// <summary>
/// 获得列表页面Url
/// </summary>
/// <returns>列表页面Url</returns>
string GetTableLogic();
/// <summary>
/// 根据编辑状态获得对应的页面Url
/// </summary>
/// <param name="status">编辑状态</param>
/// <returns>页面Url</returns>
string GetUILogic(UI.UIStatus status);
#endregion
}
}
IDataEntityProvider与ORM的不同点,
(1) IDataEntityProvider 不是以泛型实体对象作为数据操作接口, 而是以Dataset 、DataTable、DataRow 等Net原生对象为接口。如果要为每个对象都单独编写实体类代码,就失去敏捷开发的意义了,在后续的文章中将作专题讲解。
(2) 通过数据处理事件接口,定义数据增、删、改、加载时的处理事件;
(3) 通过界面UI接口定义编辑、列表页面的Url;
在Aspx开发平台中,每一个数据表对应一个实现IDataEntityProvider接口的数据对象,对象工厂实现数据对象的唯一访问点:

/// 用户对象当前唯一实例
/// </summary>
public static UserHandler Current
{
get
{
return (UserHandler) ObjectService.Factory.CreateDataEntity("t_Users");
}
}
对象工厂采用经典的单例模式,创建管理IDataEntityProvider对象:

2 /// 创建数据对象
3 /// </summary>
4 /// <param name="objectName">对象名称</param>
5 /// <returns>数据对象</returns>
6 public IDataEntityProvider CreateDataEntity(string objectName)
7 {
8 if (String.IsNullOrEmpty(objectName)) return null;
9
10 string name = objectName.Trim().ToLower();
11
12 if (this.dbEntityCache.ContainsKey(name) && (this.dbEntityCache[name] != null))
13 {
14 return this.dbEntityCache[name];
15 }
16
17
18 IDataEntityProvider currentObject = null;
19
20 lock (thisLock)
21 {
22
23 try
24 {
25
26 if (this.dbEntityCache.ContainsKey(name))
27 dbEntityCache.Remove(name);
28
29 object[] args = new object[] { name };
30
31 ObjectEntity currentDomin = ObjectService.Object.ObjectHandler.Current[name];
32
33 if (currentDomin != null && currentDomin.ObjectType != null)
34 {
35 currentObject = (IDataEntityProvider)Activator.CreateInstance(currentDomin.ObjectType, args);
36
37 if (currentObject != null)
38 {
39 currentObject.DataAccess = ObjectService.DataAccess.DriverManager.Current.GetDriver(name);
40 this.dbEntityCache.Add(name, currentObject);
41
42 if (name != "t_triggers")
43 {
44 TriggerHandler.Current.RegistrTrigger(name);
45 }
46 }
47 }
48 }
49 catch (System.Exception error)
50 {
51 string message = error.Message;
52 }
53
54
55 }
56
57 return currentObject;
58 }
59
当然,我们不可能为每个数据对象都单独编写数据对象代码,那样就太笨拙了!我们创建两种数据对象的基类, 静态数据对象(数据持久化模式AspxWork.FormHelper.StaticEntity),动态数据对象(数据非持久化模式, AspxWork.FormHelper.DataEntity)
静态数据对象(数据持久化模式)用于需要频繁读取、且数据量较少的数据表。比如系统用户、数据字典等等;采用一次性将所有数据加载到内存的方式,数据从内存中读取,减少数据库IO;

2 {
3 using ComponentArt.Web.UI;
4
5 using AspxWork.FormHelper.Dictionarys;
6 using AspxWork.FormHelper.Tree;
7 using Microsoft.Practices.EnterpriseLibrary.Data;
8 using ObjectService;
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Data;
13 using System.Text;
14 using ObjectService.UI;
15 using ObjectService.Events;
16 using DataLayer;
17 using ObjectService.Trigger;
18 using ObjectService.DataEntity;
19 using ObjectService.Relation;
20
21
22
23 /// <summary>
24 /// 静态数据对象
25 /// </summary>
26 /// <example>
27 /// <code source="..\ObjectService\DataEntity\StaticDataEntity.cs"></code>
28 /// </example>
29 /// <remarks>
30 /// (1)实现IDataEntityProvider 数据对象提供者接口;<br/>
31 /// (2)用于使用频繁、数据量较少的数据表。比如系统用户、数据字典等等;<br/>
32 /// (3)采用一次性将所有数据加载到内存的方式,数据从内存中读取,减少数据库IO<br/>
33 /// (4)可以附加数据的增、删、改、加载的事件<br/>
34 /// </remarks>
35 public class StaticEntity : IDataEntityProvider, IHasTrigger, IXMLDataProvider, ObjectService.Childs.IHasChildDataEntity
36 {
37 protected System.Data.DataSet ds = new System.Data.DataSet();
38 protected bool isGetRecordCountByView = true;
39 private bool isLoad = false;
40
41 protected string name;
42 private TriggerOfObjectEntity Trigger;
43
44 protected ObjectService.DataAccess.IDataDriver dataAccess;
45
46
47 /// <summary>
48 /// 数据实体缓存Cache
49 /// </summary>
50 protected EntityCache entityCache = new EntityCache();
51
52
53 /// <summary>
54 /// 子实体参数表
55 /// </summary>
56 Dictionary<string, string> childNameAndFields = new Dictionary<string, string>();
57
58
59 /// <summary>
60 /// 构造函数
61 /// </summary>
62 /// <param name="initName">对象数据表</param>
63 public StaticEntity(string initName)
64 {
65 this.name = initName;
66
67 this.Trigger = TriggerOfObjectHandler.Current[this.name];
68 }
69
70
71 #region IHasChildDataEntity 成员
72
73 public Dictionary<string, string> ChildNameAndFields
74 {
75 get
76 {
77 return childNameAndFields;
78 }
79 }
80
81 #endregion
82
83
84
85 /// <summary>
86 /// 参数对象
87 /// </summary>
88 public DataEntityParameter Parameter
89 {
90 get
91 {
92 return new DataEntityParameter(TableName);
93 }
94 }
95
96
97 /// <summary>
98 /// 数据驱动对象
99 /// </summary>
100 public ObjectService.DataAccess.IDataDriver DataAccess
101 {
102 get
103 {
104 return dataAccess;
105 }
106 set
107 {
108 bool isClear = (dataAccess != null);
109
110 dataAccess = value;
111
112 //第一次加载数据时,检查数据的一致性
113 if (isClear)
114 ds.Clear();
115 else
116 {
117 TableEntity.Check();
118 }
119
120 dataAccess.Load(this.ds, this.TableName);
121
122 ds.Tables[TableName].AcceptChanges();
123 }
124 }
125
126
127 /// <summary>
128 /// 启用数据视图标志
129 /// </summary>
130 public bool IsUseView
131 {
132 get
133 {
134 return TableName != ViewName;
135 }
136 }
137
138 /// <summary>
139 /// 当前登录用户ID
140 /// </summary>
141 public string UserId
142 {
143 get
144 {
145 return Roles.RoleOfUser.Current.UserId;
146 }
147 }
148
149 /// <summary>
150 /// 当前登录用户
151 /// </summary>
152 public UserHelper.UserEntity User
153 {
154 get
155 {
156 return WebControlLibrary.Globals.User;
157 }
158 }
159
160
161
162
163 /// <summary>
164 /// 新增数据
165 /// </summary>
166 /// <returns>新增数据列</returns>
167 public virtual DataRow Add()
168 {
169 DataRow row = DataAccess.Add(this.Table,TableEntity.KeyType);
170
171
172 this.TableEntity.SetDefaultValue(row);
173
174 if (this.OnAdd != null)
175 this.OnAdd(this,row);
176
177
178 if (this.Trigger != null)
179 {
180 this.Trigger.OnAdd(this, row);
181 }
182 return row;
183 }
184
185 /// <summary>
186 /// 创建TreeView控件数据
187 /// </summary>
188 /// <param name="treeView">TreeView控件</param>
189 /// <param name="Id">节点编号</param>
190 public virtual void BuildTree(TreeView treeView, string Id)
191 {
192 if (this.Dictionary != null)
193 {
194 string recordFilter;
195 if (string.IsNullOrEmpty(this.Dictionary.ParentField))
196 {
197 if (Id == "-1")
198 recordFilter = "";
199 else
200 recordFilter = string.Format("{0}='-1'", this.KeyName);
201 }
202 else
203 {
204 recordFilter = string.Format("{0}='{1}'", this.Dictionary.ParentField, Id);
205 }
206
207
208
209
210 DataView view = new DataView(this.Table);
211 view.RowFilter = this.GetRecordFilter(recordFilter);
212 view.Sort = this.Dictionary.Sort;
213 foreach (DataRowView row in view)
214 {
215 TreeViewNode node = new TreeViewNode();
216 node.Text = row[this.Dictionary.TextField].ToString();
217 node.ContentCallbackUrl = GetTreeXML.SetUrl(this.name, row[this.Dictionary.NameField].ToString(),false);
218
219 node.ID = this.TableName + "&" + row[this.Dictionary.NameField].ToString();
220 treeView.Nodes.Add(node);
221 }
222 }
223 }
224
225 /// <summary>
226 /// 创建TreeNode树节点数据
227 /// </summary>
228 /// <param name="treeNode">TreeView树节点</param>
229 /// <param name="Id">节点编号</param>
230 public virtual void BuildTree(TreeViewNode parentNode, string Id)
231 {
232 if (this.Dictionary != null)
233 {
234 string recordFilter;
235 if (string.IsNullOrEmpty(this.Dictionary.ParentField))
236 {
237 if (Id == "-1")
238 recordFilter = "";
239 else
240 recordFilter = string.Format("{0}='{1}'", this.KeyName, Id);
241 }
242 else
243 {
244 recordFilter = string.Format("{0}='{1}'", this.Dictionary.ParentField, Id);
245 }
246
247
248
249
250 DataView view = new DataView(this.Table);
251 view.RowFilter = this.GetRecordFilter( recordFilter);
252 view.Sort = this.Dictionary.Sort;
253 foreach (DataRowView row in view)
254 {
255 TreeViewNode node = new TreeViewNode();
256 node.Text = row[this.Dictionary.TextField].ToString();
257
258 if (String.IsNullOrEmpty(this.Dictionary.ParentField))
259 node.ContentCallbackUrl = "";
260 else
261 {
262 int recordCount = GetRecordCount(string.Format("{0}='{1}'", this.Dictionary.ParentField, row[this.Dictionary.NameField].ToString()));
263
264 if (recordCount > 0)
265 node.ContentCallbackUrl = GetTreeXML.SetUrl(this.name, row[this.Dictionary.NameField].ToString(),false);
266 else
267 node.ContentCallbackUrl = "";
268 }
269
270
271 node.ID = this.TableName + "&" + row[this.Dictionary.NameField].ToString();
272 parentNode.Nodes.Add(node);
273 }
274 }
275 }
276
277
278 /// <summary>
279 /// 检查对象触发器是否注册
280 /// </summary>
281 /// <param name="triggerId">触发器编号</param>
282 /// <param name="triggerType">触发器类型</param>
283 public void CheckTrigger(int triggerId, TriggerType triggerType)
284 {
285 TriggerOfObjectHandler.Current.Registr(this.name, triggerId, triggerType);
286 if (this.Trigger == null)
287 {
288 this.Trigger = TriggerOfObjectHandler.Current[this.name];
289 }
290
291 if (!this.Trigger[triggerType].Contains(TriggerHandler.Current[triggerId.ToString()]))
292 {
293 this.Trigger.Create();
294 }
295 }
296
297 private void Clear()
298 {
299 List<DataRow> deleteRows = new List<DataRow>();
300 foreach (DataRow row in this.Table.Rows)
301 {
302 if (string.IsNullOrEmpty(row[this.KeyName].ToString()))
303 {
304 deleteRows.Add(row);
305 }
306 }
307 this.Delete(deleteRows);
308 }
309
310 /// <summary>
311 /// 清空数据
312 /// </summary>
313 public virtual void ClearData()
314 {
315 this.ds.Clear();
316 DataAccess.Load(this.ds, this.TableName);
317 if (this.ViewName != this.TableName)
318 {
319 DataAccess.Load(this.ds, this.ViewName);
320 }
321 }
322
323
324 /// <summary>
325 /// 删除指定的数据列
326 /// </summary>
327 /// <param name="Rows">需要删除的数据列</param>
328 public virtual void Delete(DataRow[] Rows)
329 {
330 if ((Rows != null) && (Rows.Length != 0))
331 {
332 ArrayList Keys = new ArrayList();
333 foreach (DataRow row in Rows)
334 {
335 Keys.Add(row[this.KeyName]);
336 }
337 this.Delete(Keys);
338 }
339 }
340
341 /// <summary>
342 /// 删除指定的主键数组
343 /// </summary>
344 /// <param name="Keys">主键数组</param>
345 public virtual void Delete(ArrayList Keys)
346 {
347 if (ObjectService.DataAccess.DriverManager.Current.ReadOnly) return;
348
349
350 if (this is ObjectService.Childs.IHasChildDataEntity)
351 ObjectService.Childs.ChildManager.Current.Registr(TableName);
352
353
354
355 object[] keys = new object[Keys.Count];
356 for (int i = 0; i < Keys.Count; i++)
357 {
358 keys[i] = Keys[i];
359
360 entityCache.Remove(keys[i]);
361 }
362
363 if (this.OnDeleting != null)
364 this.OnDeleting(this, Keys);
365
366
367 if (this.Trigger != null)
368 {
369 this.Trigger.OnDeleting(this, Keys);
370 }
371 RelationHandler.Current.OnDeleted(this.Name, Keys);
372
373 if (Factory.DataLog != null)
374 {
375 if (Keys.Count > 0)
376 Factory.DataLog.LogOnDelete(this, Keys);
377 }
378
379 DataAccess.Delete(this.ds.Tables[this.TableName], keys);
380
381
382 if (this.OnDeleted != null)
383 this.OnDeleted(this, Keys);
384
385 if (this.Trigger != null)
386 {
387 this.Trigger.OnDeleted(this, Keys);
388 }
389
390 this.ReLoad();
391 }
392
393 /// <summary>
394 /// 删除指定的数据列
395 /// </summary>
396 /// <param name="Rows">需要删除的数据列</param>
397 public virtual void Delete(List<DataRow> Rows)
398 {
399 if ((Rows != null) && (Rows.Count != 0))
400 {
401 ArrayList Keys = new ArrayList();
402 foreach (DataRow row in Rows)
403 {
404 Keys.Add(row[this.KeyName]);
405 }
406
407 this.Delete(Keys);
408 }
409 }
410
411
412
413 /// <summary>
414 /// 删除指定的实体对象
415 /// </summary>
416 /// <param name="Entitys">删除实体对象</param>
417 public virtual void Delete(List<BaseEntity> entitys)
418 {
419 if ((entitys != null) && (entitys.Count != 0))
420 {
421 ArrayList Keys = new ArrayList();
422 foreach (BaseEntity enetity in entitys)
423 {
424 Keys.Add(enetity.Key);
425 }
426 this.Delete(Keys);
427 }
428 }
429
430
431
432 /// <summary>
433 /// 删除指定的主键
434 /// </summary>
435 /// <param name="Key">主键</param>
436 public virtual void Delete(object Key)
437 {
438 if (this is ObjectService.Childs.IHasChildDataEntity)
439 ObjectService.Childs.ChildManager.Current.Registr(TableName);
440
441 entityCache.Remove(Key);
442
443 object[] Keys = new object[] { Key };
444 ArrayList deleteKeys = new ArrayList();
445 deleteKeys.Add(Key);
446
447 Delete(deleteKeys);
448
449 }
450
451 /// <summary>
452 /// 编辑数据
453 /// </summary>
454 /// <param name="Key">数据主键</param>
455 /// <returns>数据列</returns>
456 public virtual DataRow Edit(object Key)
457 {
458 if (Key == null) return null;
459
460 if (string.IsNullOrEmpty(Key.ToString()))
461 {
462 return null;
463 }
464
465 DataRow row = null;
466 try
467 {
468 row = this.Table.Rows.Find(Key);
469 if (row == null)
470 {
471 string filter = Common.GetRecordFilter(this.Table, this.KeyName, "=", Key.ToString());
472 DataAccess.LoadByCondition(this.Table.DataSet, this.GetRecordFilter( filter), this.TableName);
473 row = this.Table.Rows.Find(Key);
474 }
475
476 if (this.OnEdit != null)
477 this.OnEdit(this, row);
478
479
480
481 if (this.Trigger != null)
482 {
483 this.Trigger.OnEdit(this, row);
484 }
485 }
486 catch { }
487 return row;
488 }
489
490
491 /// <summary>
492 /// 根据字典上级编码,获得对应的数据视图
493 /// </summary>
494 /// <param name="id">上级编码</param>
495 /// <returns>数据视图</returns>
496 public DataView GetDataViewById(string id)
497 {
498 string recordFilter;
499 if (string.IsNullOrEmpty(Dictionary.ParentField))
500 {
501 if (id == "-1")
502 recordFilter = "";
503 else
504 recordFilter = string.Format("{0}='{1}'", KeyName, id);
505 }
506 else
507 {
508 recordFilter = string.Format("{0}='{1}'", Dictionary.ParentField, id);
509 }
510
511
512 return GetDataView(recordFilter, Dictionary.Sort);
513 }
514
515 /// <summary>
516 /// 获得指定数据筛选条件的数据视图
517 /// </summary>
518 /// <param name="recordFilter">数据筛选条件</param>
519 /// <returns>数据视图</returns>
520 public virtual DataView GetDataView(string recordFilter)
521 {
522 DataView dataView = new DataView(this.View);
523 dataView.RowFilter = this.GetRecordFilter( recordFilter);
524 return dataView;
525 }
526
527 /// <summary>
528 /// 获得指定数据筛选条件的数据视图,并排序
529 /// </summary>
530 /// <param name="recordFilter">数据筛选条件</param>
531 /// <param name="order">排序条件</param>
532 /// <returns>数据视图</returns>
533 public virtual DataView GetDataView(string recordFilter, string order)
534 {
535 DataView dataView = new DataView(Common.CopyTable(this.View, 0, View.Rows.Count, recordFilter, order));
536 dataView.RowFilter = this.GetRecordFilter( recordFilter);
537 if (!string.IsNullOrEmpty(order))
538 {
539 dataView.Sort = order;
540 }
541 return dataView;
542 }
543
544
545 /// <summary>
546 /// 获得指定范围和数据筛选条件的数据视图,并排序
547 /// </summary>
548 /// <param name="startRecord">开始记录位置</param>
549 /// <param name="maxRecord">记录数</param>
550 /// <param name="recordFilter">数据筛选条件</param>
551 /// <param name="order">排序条件</param>
552 /// <returns>数据视图</returns>
553 public virtual DataView GetDataView(int startRecord, int maxRecord, string recordFilter, string order)
554 {
555 DataView dataView = new DataView(Common.CopyTable(this.View, startRecord, maxRecord, recordFilter, order));
556 dataView.RowFilter = this.GetRecordFilter(recordFilter);
557 if (!string.IsNullOrEmpty(order))
558 {
559 dataView.Sort = order;
560 }
561 return dataView;
562 }
563
564 /// <summary>
565 /// 获得指定范围和数据筛选条件的数据视图,并排序,同时指明是否从缓存中
566 /// </summary>
567 /// <param name="startRecord">开始记录位置</param>
568 /// <param name="maxRecord">记录数</param>
569 /// <param name="recordFilter">数据筛选条件</param>
570 /// <param name="order">排序条件</param>
571 /// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
572 /// <returns>数据视图</returns>
573 public virtual DataView GetDataView(int startRecord, int maxRecord, string recordFilter, string order, bool isLoad)
574 {
575 string filter = this.GetRecordFilter(recordFilter);
576 DataView dataView = new DataView(Common.CopyTable(this.View, startRecord, maxRecord, filter, order));
577 dataView.RowFilter = filter;
578 if (!string.IsNullOrEmpty(order))
579 {
580 dataView.Sort = order;
581 }
582 return dataView;
583 }
584
585
586 /// <summary>
587 /// 根据编辑状态获得对应的页面Url
588 /// </summary>
589 /// <param name="status">编辑状态</param>
590 /// <returns>页面Url</returns>
591 public virtual string GetEditLogic()
592 {
593 return this.TableEntity.GetUILogic(UIStatus.Edit);
594 }
595
596 /// <summary>
597 /// 根据字段名称、字段值获得数据筛选条件
598 /// </summary>
599 /// <param name="fieldName">字段名称</param>
600 /// <param name="fieldValue">字段值</param>
601 /// <returns>数据筛选条件</returns>
602 public virtual string GetFilter(string fieldName, string fieldValue)
603 {
604 if (fieldValue == "-1") return string.Empty;
605
606 if (this.Dictionary == null) return string.Format("{0} = '{1}'", fieldName, fieldValue);
607
608 if (string.IsNullOrEmpty(this.Dictionary.ParentField))
609 return string.Format("{0} = '{1}'", fieldName, fieldValue);
610
611 return this.Dictionary.GetRecordFilterById(fieldName, fieldValue);
612 }
613
614
615
616 /// <summary>
617 /// 根据字段名称、字段值获得数据筛选条件
618 /// </summary>
619 /// <param name="dataListName">数据列表名称</param>
620 /// <param name="fieldName">字段名称</param>
621 /// <param name="fieldValue">字段值</param>
622 /// <returns>数据筛选条件</returns>
623 public virtual string GetFilter(string dataListName, string fieldName, string fieldValue)
624 {
625 if (fieldValue == "-1") return string.Empty;
626
627 if (this.Dictionary == null) return string.Format("{0} = '{1}'", fieldName, fieldValue);
628
629 if (string.IsNullOrEmpty(this.Dictionary.ParentField))
630 return string.Format("{0} = '{1}'", fieldName, fieldValue);
631
632 return this.Dictionary.GetRecordFilterById(fieldName, fieldValue);
633 }
634
635
636
637 /// <summary>
638 /// 根据主键值,获得对应数据列的指定字段的值
639 /// </summary>
640 /// <param name="FieldName">字段名称</param>
641 /// <param name="key">主键值</param>
642 /// <returns>字段值</returns>
643 public virtual string GetNameById(string FieldName, object Id)
644 {
645 DataRow row = this.Edit(Id);
646 if (row == null)
647 {
648 return string.Empty;
649 }
650 return ((this.Table.Columns[FieldName] != null) ? row[FieldName].ToString() : string.Empty);
651 }
652
653
654 /// <summary>
655 /// 根据数据筛选条件,获得对应的记录数
656 /// </summary>
657 /// <param name="recordFilter">数据筛选条件</param>
658 /// <returns>记录数</returns>
659 public int GetRecordCount(string recordFilter)
660 {
661 string filter = this.GetRecordFilter(recordFilter);
662
663 if (this.isGetRecordCountByView)
664 {
665 if (string.IsNullOrEmpty(filter))
666 {
667 return this.View.Rows.Count;
668 }
669
670 return this.View.Select(filter).Length;
671 }
672
673
674 if (string.IsNullOrEmpty(filter))
675 {
676 return this.Table.Rows.Count;
677 }
678
679 return this.Table.Select(filter).Length;
680 }
681
682 /// <summary>
683 /// 获得数据筛选条件
684 /// </summary>
685 /// <param name="recordFilter">数据筛选条件</param>
686 /// <returns>数据筛选条件</returns>
687 protected virtual string GetRecordFilter(string recordFilter)
688 {
689 return ObjectService.Factory.GetRecordFilter(TableName, recordFilter);
690 }
691
692
693 /// <summary>
694 /// 获得列表页面Url
695 /// </summary>
696 /// <returns>列表页面Url</returns>
697 public virtual string GetTableLogic()
698 {
699 return this.TableEntity.GetUILogic(UIStatus.Delete);
700 }
701
702 /// <summary>
703 /// 根据编辑状态获得对应的页面Url
704 /// </summary>
705 /// <param name="status">编辑状态</param>
706 /// <returns>页面Url</returns>
707 public virtual string GetUILogic(UIStatus status)
708 {
709 return this.TableEntity.GetUILogic(status);
710 }
711
712 /// <summary>
713 /// 加载数据
714 /// </summary>
715 public void LoadData()
716 {
717 DataAccess.Load(this.ds, this.TableName);
718 }
719
720
721
722 /// <summary>
723 /// 将数据加载到DataSet
724 /// </summary>
725 /// <param name="dataSet">数据集</param>
726 /// <param name="recordFilter">数据筛选条件</param>
727 /// <param name="order">排序防守</param>
728 /// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
729 /// <returns>符合条件的记录数</returns>
730 public virtual int LoadData(System.Data.DataSet dataSet, string recordFilter, string order, bool isLoad)
731 {
732 string filter = this.GetRecordFilter( recordFilter);
733
734 int recordCount = GetRecordCount(filter);
735
736 DataTable table = Common.CopyTable(this.View, 0, recordCount, filter, order);
737
738 if (dataSet.Tables[ViewName] != null)
739 dataSet.Tables.Remove(ViewName);
740
741 dataSet.Tables.Add(table);
742
743 if (this.Trigger != null)
744 {
745 this.Trigger.OnLoad(this, dataSet);
746 }
747
748 return recordCount;
749 }
750
751 /// <summary>
752 /// 分页加载数据到数据集
753 /// </summary>
754 /// <param name="dataSet">数据集</param>
755 /// <param name="pageIndex">页编号</param>
756 /// <param name="pageSize">页记录数</param>
757 /// <param name="recordFilter">数据筛选条件</param>
758 /// <param name="order">排序条件</param>
759 /// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
760 /// <returns>符合条件的记录数</returns>
761 public virtual int LoadData(System.Data.DataSet dataSet, int pageIndex, int pageSize, string recordFilter, string order, bool isLoad)
762 {
763 string filter = this.GetRecordFilter( recordFilter);
764
765 int startRecord;
766
767 if (pageIndex == 0)
768 startRecord = 0;
769 else
770 startRecord = (pageIndex - 1) * pageSize;
771
772 DataTable table = Common.CopyTable(this.View, startRecord, pageSize, filter, order);
773
774 if (dataSet.Tables[ViewName] != null)
775 dataSet.Tables.Remove(ViewName);
776
777 dataSet.Tables.Add(table);
778
779 if (this.Trigger != null)
780 {
781 this.Trigger.OnLoad(this, dataSet);
782 }
783
784
785 return GetRecordCount(filter);
786 }
787
788
789 /// <summary>
790 /// 重新加载数据
791 /// </summary>
792 protected void ReLoad()
793 {
794 this.ds.Clear();
795 DataAccess.Load(this.ds, this.TableName);
796
797 if (this.ViewName != this.TableName)
798 {
799 DataAccess.Load(this.ds, this.ViewName);
800 }
801 }
802
803
804 /// <summary>
805 /// 重新加载数据视图
806 /// </summary>
807 private void ReLoadView()
808 {
809 if (this.ViewName != this.TableName)
810 {
811 DataAccess.Load(this.ds, this.ViewName);
812 }
813 }
814
815
816 /// <summary>
817 /// 保存数据
818 /// </summary>
819 /// <param name="Row">数据列</param>
820 public void Save(DataRow Row)
821 {
822 Save(new DataRow[] { Row });
823 }
824
825
826
827 /// <summary>
828 /// 获得分组数据
829 /// </summary>
830 /// <param name="groupColumn">分组栏目</param>
831 /// <param name="filter">数据筛选条件</param>
832 /// <param name="order">数据排序条件</param>
833 /// <returns>分组数据集</returns>
834 public System.Data.DataSet GetGroupCount(string groupColumn, string filter, string order)
835 {
836 System.Data.DataSet ds = new System.Data.DataSet();
837
838 DataTable table = new DataTable();
839
840 DataColumn column = new DataColumn(groupColumn, typeof(String));
841 table.Columns.Add(column);
842 table.PrimaryKey = new DataColumn[]{column};
843
844 table.Columns.Add("Count", typeof(int));
845
846 ds.Tables.Add(table);
847
848 DataRow[] rows = Select(filter, order);
849
850 foreach (DataRow row in rows)
851 {
852 DataRow countRow = table.Rows.Find(row[groupColumn].ToString());
853
854 if (countRow == null)
855 {
856 countRow = table.NewRow();
857 countRow[groupColumn] = row[groupColumn].ToString();
858 countRow["Count"] = 0;
859
860 table.Rows.Add(countRow);
861 }
862
863
864 countRow["Count"] = (int)countRow["Count"] + 1;
865
866 }
867
868
869 foreach (DataRow row in table.Rows)
870 {
871
872 if (Trigger != null)
873 {
874 Trigger.OnLoaded(this, row);
875 }
876 }
877
878
879 return ds;
880
881 }
882
883
884 /// <summary>
885 /// 保存数据
886 /// </summary>
887 /// <param name="Rows">数据列数组</param>
888 public virtual void Save(DataRow[] Rows)
889 {
890 if (ObjectService.DataAccess.DriverManager.Current.ReadOnly) return;
891
892 try
893 {
894 if (this.Trigger != null)
895 {
896 this.Trigger.OnSaving(this, Rows);
897 }
898
899 List<DataRow> addRows = new List<DataRow>();
900 List<DataRow> changeRows = new List<DataRow>();
901 List<DataRow> oldRows = new List<DataRow>();
902 foreach (DataRow row in Rows)
903 {
904 if (row != null)
905 {
906 if (Builders.ItemHandler.Current != null)
907 {
908 Builders.ItemHandler.Current.Generate(row);
909 }
910
911 switch (row.RowState)
912 {
913 case DataRowState.Detached:
914 this.Table.Rows.Add(row);
915 addRows.Add(row);
916 break;
917
918 case DataRowState.Modified:
919 changeRows.Add(row);
920 break;
921 }
922 }
923 }
924
925
926 System.Data.DataSet ds = new System.Data.DataSet();
927 StringBuilder filter = new StringBuilder();
928 int i = 0;
929 foreach (DataRow row in changeRows)
930 {
931 if (i > 0)
932 {
933 filter.Append(" and ");
934 }
935 filter.Append(Common.GetRecordFilter(this.Table, this.KeyName, "=", row[this.KeyName].ToString()));
936 i++;
937 }
938
939
940 DataAccess.LoadByCondition(ds, filter.ToString(), this.TableName);
941 if (ds.Tables[this.TableName] != null)
942 {
943 foreach (DataRow row in changeRows)
944 {
945 DataRow oldrow = ds.Tables[this.TableName].Rows.Find(row[this.KeyName]);
946 if (oldrow != null)
947 {
948 oldRows.Add(oldrow);
949 }
950 }
951 }
952
953 RelationHandler.Current.OnAdded(this.name, addRows);
954 RelationHandler.Current.OnChanged(this.name, oldRows, changeRows);
955
956 DataAccess.Update(Rows);
957 if (this.Trigger != null)
958 {
959 this.Trigger.OnSaved(this, Rows);
960 }
961
962 if (Factory.DataLog != null)
963 {
964 if (addRows.Count > 0)
965 Factory.DataLog.LogOnAdd(this, addRows);
966
967 if(oldRows.Count > 0)
968 Factory.DataLog.LogOnSave(this,changeRows,oldRows);
969 }
970
971
972 this.ReLoadView();
973
974
975 }
976 catch (Exception error)
977 {
978 this.ClearData();
979
980 //记录错误信息
981 Factory.ErrorLog.Log(error);
982
983 throw new ApplicationException(error.Message);
984 }
985 }
986
987 /// <summary>
988 /// 保存数据
989 /// </summary>
990 /// <param name="Rows">数据列数组</param>
991 public void Save(List<DataRow> Rows)
992 {
993 if ((Rows != null) && (Rows.Count > 0))
994 {
995 DataRow[] saveRows = new DataRow[Rows.Count];
996 Rows.CopyTo(saveRows);
997 this.Save(saveRows);
998 }
999 }
1000
1001
1002 /// <summary>
1003 /// 选择出指定范围的数据列
1004 /// </summary>
1005 /// <param name="recordFilter">数据筛选条件</param>
1006 /// <returns>符合条件的数据列数组</returns>
1007 public DataRow[] Select(string recordFilter)
1008 {
1009 return this.Table.Select(recordFilter);
1010 }
1011
1012
1013 /// <summary>
1014 /// 选择出指定范围的数据列,并排序
1015 /// </summary>
1016 /// <param name="recordFilter">数据筛选条件</param>
1017 /// <param name="order">排序条件</param>
1018 /// <returns>符合条件并已经排序的数据列数组</returns>
1019 public DataRow[] Select(string recordFilter, string order)
1020 {
1021 DataView view = new DataView(Table);
1022 view.RowFilter = recordFilter;
1023 view.Sort = order;
1024
1025 DataRow[] rows = new DataRow[view.Count];
1026
1027 for (int i = 0; i < view.Count; i += 1)
1028 {
1029 rows[i] = view[i].Row;
1030 }
1031
1032 return rows;
1033 }
1034
1035
1036
1037
1038
1039 /// <summary>
1040 /// 选择出指定范围的数据列,并放入数据列泛型数组中
1041 /// </summary>
1042 /// <param name="selectRows">数据列泛型数组</param>
1043 /// <param name="recordFilter">数据筛选条件</param>
1044 public void Select(List<DataRow> selectRows, string recordFilter)
1045 {
1046 DataRow[] rows = Select(recordFilter);
1047
1048 foreach (DataRow row in rows)
1049 {
1050 if (!selectRows.Contains(row))
1051 selectRows.Add(row);
1052 }
1053 }
1054
1055
1056
1057 /// <summary>
1058 /// 合并数据
1059 /// </summary>
1060 /// <param name="DataSet">数据集</param>
1061 public virtual void Merge(System.Data.DataSet DataSet)
1062 {
1063 if (DataSet.Tables[TableName] == null) return;
1064
1065 Common.SetPrimaryKey(DataSet.Tables[TableName], this.KeyName);
1066
1067 List<DataRow> addRows = new List<DataRow>();
1068 foreach (DataRow row in DataSet.Tables[TableName].Rows)
1069 {
1070 DataRow addRow = Edit(row[KeyName]);
1071 if (addRow == null)
1072 addRow = Add();
1073
1074 foreach (System.Data.DataColumn column in Table.Columns)
1075 {
1076 if (DataSet.Tables[TableName].Columns[column.ColumnName] != null)
1077 addRow[column.ColumnName] = row[column.ColumnName];
1078 }
1079
1080
1081 addRows.Add(addRow);
1082 }
1083
1084 Save(addRows);
1085 }
1086
1087
1088
1089 /// <summary>
1090 /// 对应的数据字典视图对象
1091 /// </summary>
1092 public ObjectService.Dictionarys.DictionaryEntity Dictionary
1093 {
1094 get
1095 {
1096 return DictionaryHandler.Current[this.Name];
1097 }
1098 }
1099
1100
1101 /*
1102 protected bool isGetUIBySelf = true;
1103
1104 public bool IsGetUIBySelf
1105 {
1106 get
1107 {
1108 return isGetUIBySelf;
1109 }
1110 }
1111 */
1112
1113
1114 /// <summary>
1115 /// 静态数据标志
1116 /// </summary>
1117 public bool IsStatic
1118 {
1119 get
1120 {
1121 return true;
1122 }
1123 }
1124
1125 /// <summary>
1126 /// 主键名称
1127 /// </summary>
1128 public string KeyName
1129 {
1130 get
1131 {
1132 return this.TableEntity.KeyName;
1133 }
1134 }
1135
1136
1137 /// <summary>
1138 /// 名称
1139 /// </summary>
1140 public string Name
1141 {
1142 get
1143 {
1144 return this.name;
1145 }
1146 }
1147
1148 /// <summary>
1149 /// 根节点名称
1150 /// </summary>
1151 public virtual string RootName
1152 {
1153 get
1154 {
1155 return ((this.Dictionary != null) ? ("所有" + this.Dictionary.Name.Replace("数据字典", "")) : "所有分类");
1156 }
1157 }
1158
1159
1160 /// <summary>
1161 /// 数据表
1162 /// </summary>
1163 public DataTable Table
1164 {
1165 get
1166 {
1167 return GetTable();
1168 }
1169 }
1170
1171 /// <summary>
1172 /// 获得数据表
1173 /// </summary>
1174 /// <returns>数据表</returns>
1175 protected virtual DataTable GetTable()
1176 {
1177 if (this.ds.Tables[this.TableName] == null)
1178 {
1179 DataAccess.Load(this.ds, this.TableName);
1180 }
1181
1182 if (!((this.ds.Tables[this.TableName].Rows.Count != 0) || this.isLoad))
1183 {
1184 DataAccess.Load(this.ds, this.TableName);
1185 this.isLoad = true;
1186 }
1187
1188
1189 return this.ds.Tables[this.TableName];
1190 }
1191
1192
1193 /// <summary>
1194 /// 表信息实体对象
1195 /// </summary>
1196 private AspxWork.FormHelper.TableEntity TableEntity
1197 {
1198 get
1199 {
1200 return AspxWork.FormHelper.TableHandler.Current[this.name];
1201 }
1202 }
1203
1204
1205 /// <summary>
1206 /// 数据表名称
1207 /// </summary>
1208 public string TableName
1209 {
1210 get
1211 {
1212 return this.TableEntity.GetTableName();
1213 }
1214 }
1215
1216
1217 /// <summary>
1218 /// 数据视图
1219 /// </summary>
1220 public DataTable View
1221 {
1222 get
1223 {
1224 return GetView();
1225 }
1226 }
1227
1228
1229 /// <summary>
1230 /// 获得数据视图
1231 /// </summary>
1232 /// <returns>数据视图表</returns>
1233 protected virtual DataTable GetView()
1234 {
1235 if (this.ds.Tables[this.ViewName] == null)
1236 {
1237 DataAccess.Load(this.ds, this.ViewName);
1238 }
1239 return this.ds.Tables[this.ViewName];
1240 }
1241
1242 /// <summary>
1243 /// 数据视图名称
1244 /// </summary>
1245 public string ViewName
1246 {
1247 get
1248 {
1249 return this.TableEntity.GetViewName();
1250 }
1251 }
1252
1253 #region IXMLDataProvider 成员
1254
1255 /// <summary>
1256 /// 获得数据的XML表示
1257 /// </summary>
1258 /// <param name="xmlWriter">xml写入器</param>
1259 /// <param name="Id">主键ID</param>
1260 public virtual void GetXML(System.Xml.XmlWriter xmlWriter, string Id, string nodeName, bool isShowCheckBox)
1261 {
1262 if (this.Dictionary != null)
1263 {
1264 string recordFilter;
1265 if (string.IsNullOrEmpty(this.Dictionary.ParentField))
1266 {
1267 if (Id == "-1")
1268 recordFilter = "";
1269 else
1270 recordFilter = string.Format("{0}='{1}'", this.KeyName, Id);
1271 }
1272 else
1273 {
1274 recordFilter = string.Format("{0}='{1}'", this.Dictionary.ParentField, Id);
1275 }
1276
1277
1278
1279
1280 if (Id == "-1")
1281 {
1282
1283 //写入根节点
1284 xmlWriter.WriteStartElement(nodeName);
1285 xmlWriter.WriteAttributeString("ID", this.TableName + "&-1");
1286 xmlWriter.WriteAttributeString("Text", RootName);
1287 xmlWriter.WriteAttributeString("ImageUrl", "root.gif");
1288 xmlWriter.WriteAttributeString("ContentCallbackUrl", GetTreeXML.SetUrl(this.name, "-1",isShowCheckBox));
1289
1290 if(isShowCheckBox)
1291 xmlWriter.WriteAttributeString("ShowCheckBox", "true");
1292
1293 xmlWriter.WriteAttributeString("Expanded", "true");
1294 }
1295
1296 DataView view = new DataView(this.Table);
1297 view.RowFilter = this.GetRecordFilter( recordFilter);
1298 view.Sort = this.Dictionary.Sort;
1299
1300 foreach (DataRowView row in view)
1301 {
1302 WriteRowToXML(xmlWriter, row.Row, nodeName,isShowCheckBox);
1303 }
1304
1305
1306 if (Id == "-1")
1307 {
1308
1309 xmlWriter.WriteFullEndElement();
1310 }
1311
1312 }
1313 }
1314
1315 /// <summary>
1316 /// 将数据列数据写入XML节点
1317 /// </summary>
1318 /// <param name="xmlWriter">XML写入器</param>
1319 /// <param name="row">数据列</param>
1320 /// <param name="nodeName">节点名称</param>
1321 public virtual void WriteRowToXML(System.Xml.XmlWriter xmlWriter, DataRow row,string nodeName,bool isShowCheckBox)
1322 {
1323 xmlWriter.WriteStartElement(nodeName);
1324
1325 xmlWriter.WriteAttributeString("ID", this.TableName + "&" + row[this.Dictionary.NameField].ToString());
1326 xmlWriter.WriteAttributeString("Text", row[this.Dictionary.TextField].ToString());
1327
1328 if (isShowCheckBox)
1329 xmlWriter.WriteAttributeString("ShowCheckBox", "true");
1330
1331 if (!String.IsNullOrEmpty(this.Dictionary.ParentField))
1332 {
1333 int recordCount = GetRecordCount(string.Format("{0}='{1}'", this.Dictionary.ParentField, row[this.Dictionary.NameField].ToString()));
1334
1335 if (recordCount > 0)
1336 xmlWriter.WriteAttributeString("ContentCallbackUrl", GetTreeXML.SetUrl(this.name, row[this.Dictionary.NameField].ToString(),isShowCheckBox));
1337 }
1338
1339 //进行数据列写入XML的事件处理
1340 OnWriteRowToXML(xmlWriter, row, nodeName,isShowCheckBox);
1341
1342 xmlWriter.WriteFullEndElement();
1343 }
1344
1345 /// <summary>
1346 /// 当数据列写入XML的事件处理
1347 /// </summary>
1348 /// <param name="xmlWriter">XML写入器</param>
1349 /// <param name="row">数据列</param>
1350 /// <param name="nodeName">节点名称</param>
1351 protected virtual void OnWriteRowToXML(System.Xml.XmlWriter xmlWriter, DataRow row, string nodeName,bool isShowCheckBox)
1352 {
1353 }
1354
1355 #endregion
1356
1357
1358
1359
1360 #region 数据委托
1361
1362
1363 /// <summary>
1364 /// 数据添加委托事件
1365 /// </summary>
1366 OnAddEvent onAdd;
1367
1368 /// <summary>
1369 /// 数据已经删除委托事件
1370 /// </summary>
1371 OnDeletedEvent onDeleted;
1372
1373 /// <summary>
1374 /// 数据删除委托事件
1375 /// </summary>
1376 OnDeletingEvent onDeleting;
1377
1378
1379 /// <summary>
1380 /// 数据编辑委托事件
1381 /// </summary>
1382 OnEditEvent onEdit;
1383
1384
1385 /// <summary>
1386 /// 数据已经保存委托事件
1387 /// </summary>
1388 OnSavedEvent onSaved;
1389
1390 /// <summary>
1391 /// 数据保存委托事件
1392 /// </summary>
1393 OnSavingEvent onSaving;
1394
1395 /// <summary>
1396 /// 数据加载委托事件
1397 /// </summary>
1398 OnLoadEvent onLoad;
1399
1400
1401 /// <summary>
1402 /// 数据添加委托事件
1403 /// </summary>
1404 public OnAddEvent OnAdd
1405 {
1406 get
1407 {
1408 return onAdd;
1409 }
1410 set
1411 {
1412 onAdd = value;
1413 }
1414 }
1415
1416 /// <summary>
1417 /// 数据已经删除委托事件
1418 /// </summary>
1419 public OnDeletedEvent OnDeleted
1420 {
1421 get
1422 {
1423 return onDeleted;
1424 }
1425 set
1426 {
1427 onDeleted = value;
1428 }
1429 }
1430
1431
1432 /// <summary>
1433 /// 数据删除委托事件
1434 /// </summary>
1435 public OnDeletingEvent OnDeleting
1436 {
1437 get
1438 {
1439 return onDeleting;
1440 }
1441 set
1442 {
1443 onDeleting = value;
1444 }
1445 }
1446
1447
1448
1449 /// <summary>
1450 /// 数据编辑委托事件
1451 /// </summary>
1452 public OnEditEvent OnEdit
1453 {
1454 get
1455 {
1456 return onEdit;
1457 }
1458 set
1459 {
1460 onEdit = value;
1461 }
1462 }
1463
1464
1465
1466 /// <summary>
1467 /// 数据已经保存委托事件
1468 /// </summary>
1469 public OnSavedEvent OnSaved
1470 {
1471 get
1472 {
1473 return onSaved;
1474 }
1475 set
1476 {
1477 onSaved = value;
1478 }
1479 }
1480
1481
1482 /// <summary>
1483 /// 数据保存委托事件
1484 /// </summary>
1485 public OnSavingEvent OnSaving
1486 {
1487 get
1488 {
1489 return onSaving;
1490 }
1491 set
1492 {
1493 onSaving = value;
1494 }
1495 }
1496
1497 /// <summary>
1498 /// 数据加载委托事件
1499 /// </summary>
1500 public OnLoadEvent OnLoad
1501 {
1502 get
1503 {
1504 return onLoad;
1505 }
1506 set
1507 {
1508 onLoad = value;
1509 }
1510 }
1511
1512 #endregion
1513
1514
1515 }
1516 }
1517
动态数据对象(数据非持久化模式)用于管理数据量较大,不适宜一次性全部加载到内存的业务数据,采用采用数据缓存机制,只加载指定范围的数据,比如销售单、订单等等;

2 {
3 using ObjectService.UI;
4
5 using Microsoft.Practices.EnterpriseLibrary.Caching;
6 using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
7 using Microsoft.Practices.EnterpriseLibrary.Data;
8 using ObjectService;
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Data;
13 using System.Data.Common;
14 using System.Text;
15 using ObjectService.Events;
16 using DataLayer;
17 using ObjectService.Trigger;
18 using ObjectService.DataEntity;
19 using ObjectService.Relation;
20
21
22 /// <summary>
23 /// 动态数据管理对象
24 /// </summary>
25 /// <example>
26 /// <code source="..\FormHelper\DataEntity.cs"></code>
27 /// </example>
28 /// <remarks>
29 /// (1)实现IDataEntityProvider 数据对象提供者接口;<br/>
30 /// (2)用于管理数据量较大,不适宜一次性全部加载到内存的业务数据,只加载指定范围的数据,比如销售单、订单等等;<br/>
31 /// (3)采用数据缓存机制;<br/>
32 /// (4)可以附加数据的增、删、改、加载的事件<br/>
33 /// </remarks>
34 public class DataEntity : IDataEntityProvider, IHasTrigger,ObjectService.Childs.IHasChildDataEntity
35 {
36
37 /// <summary>
38 /// 数据缓存名称列表
39 /// </summary>
40 protected ArrayList cacheNameList = new ArrayList();
41
42 /// <summary>
43 /// 从数据视图中获得记录数的标志
44 /// </summary>
45 protected bool isGetRecordCountByView = false;
46
47 /// <summary>
48 /// 允许最多加载的记录总数(超过此数,自动清空加载数据)
49 /// </summary>
50 protected int maxRecordCount = 0x3e8;
51
52 /// <summary>
53 /// 管理对象名称
54 /// </summary>
55 private string name;
56
57 /// <summary>
58 /// 对象触发器
59 /// </summary>
60 private TriggerOfObjectEntity Trigger;
61
62 /// <summary>
63 /// 数据视图表
64 /// </summary>
65 protected DataTable viewTable;
66
67
68 /// <summary>
69 /// 数据存取对象
70 /// </summary>
71 protected ObjectService.DataAccess.IDataDriver dataAccess;
72
73
74 /// <summary>
75 /// 数据表
76 /// </summary>
77 DataTable table;
78
79 /// <summary>
80 /// 子实体参数表
81 /// </summary>
82 Dictionary<string, string> childNameAndFields = new Dictionary<string, string>();
83
84
85
86 /// <summary>
87 /// 数据实体缓存Cache
88 /// </summary>
89 protected EntityCache entityCache = new EntityCache();
90
91
92 /// <summary>
93 /// 构造函数,
94 /// </summary>
95 /// <param name="initName">对象数据表名称</param>
96 public DataEntity(string initName)
97 {
98
99 this.name = initName;
100 this.Trigger = TriggerOfObjectHandler.Current[this.name];
101
102
103 if(this.TableName != this.ViewName && !isGetRecordCountByView)
104 isGetRecordCountByView = true;
105 }
106
107
108
109 #region IHasChildDataEntity 成员
110
111
112 /// <summary>
113 /// 子实体参数表
114 /// </summary>
115 public Dictionary<string, string> ChildNameAndFields
116 {
117 get
118 {
119 return childNameAndFields;
120 }
121 }
122
123 #endregion
124
125
126
127 /// <summary>
128 /// 参数对象
129 /// </summary>
130 public DataEntityParameter Parameter
131 {
132 get
133 {
134 return new DataEntityParameter(TableName);
135 }
136 }
137
138
139
140 /// <summary>
141 /// 数据存取对象
142 /// </summary>
143 public ObjectService.DataAccess.IDataDriver DataAccess
144 {
145 get
146 {
147 return dataAccess;
148 }
149 set
150 {
151
152 bool isClear = (dataAccess != null);
153
154 dataAccess = value;
155
156 //第一次加载数据时,检查数据的一致性
157 if (isClear)
158 ClearData();
159 else
160 TableEntity.Check();
161 }
162 }
163
164 /// <summary>
165 /// 当前登录用户ID
166 /// </summary>
167 public string UserId
168 {
169 get
170 {
171 return WebControlLibrary.Globals.UserId;
172 }
173 }
174
175
176 /// <summary>
177 /// 当前登录用户
178 /// </summary>
179 public UserHelper.UserEntity User
180 {
181 get
182 {
183 return WebControlLibrary.Globals.User;
184 }
185 }
186
187
188 /// <summary>
189 /// 启用数据视图标志
190 /// </summary>
191 public bool IsUseView
192 {
193 get
194 {
195 return TableName != ViewName;
196 }
197 }
198
199
200 /// <summary>
201 /// 新增数据
202 /// </summary>
203 /// <returns>新增数据列</returns>
204 public virtual DataRow Add()
205 {
206 DataRow row = DataAccess.Add(this.Table, this.TableEntity.KeyType);
207
208
209 //设置字段的缺省默认值
210 this.TableEntity.SetDefaultValue(row);
211
212
213 if (this.OnAdd != null)
214 this.OnAdd(this, row);
215
216
217 if (this.Trigger != null)
218 {
219 this.Trigger.OnAdd(this, row);
220 }
221 return row;
222 }
223
224
225 /// <summary>
226 /// 检查对象触发器是否已经注册
227 /// </summary>
228 /// <param name="triggerId">触发器编号</param>
229 /// <param name="triggerType">触发器类型</param>
230 public void CheckTrigger(int triggerId, TriggerType triggerType)
231 {
232 TriggerOfObjectHandler.Current.Registr(this.name, triggerId, triggerType);
233 if (this.Trigger == null)
234 {
235 this.Trigger = TriggerOfObjectHandler.Current[this.name];
236 }
237
238 if (!this.Trigger[triggerType].Contains(TriggerHandler.Current[triggerId.ToString()]))
239 {
240 this.Trigger.Create();
241 }
242 }
243
244 /// <summary>
245 /// 清除数据缓存
246 /// </summary>
247 protected void ClearCache()
248 {
249 foreach (object cacheName in this.cacheNameList)
250 {
251 string CacheName = cacheName.ToString();
252
253 if (this.CacheManager.Contains(CacheName))
254 {
255 this.CacheManager.Remove(CacheName);
256 }
257 }
258
259 this.cacheNameList.Clear();
260 }
261
262 /// <summary>
263 /// 清除数据
264 /// </summary>
265 public void ClearData()
266 {
267 if (table != null)
268 {
269 table.Clear();
270
271 table = this.TableEntity.Table.Clone();
272
273 if (table.DataSet == null)
274 {
275 System.Data.DataSet ds = new System.Data.DataSet();
276 ds.Tables.Add(table);
277 }
278 }
279
280 ClearCache();
281 }
282
283 /// <summary>
284 /// 删除指定的数据列
285 /// </summary>
286 /// <param name="Rows">删除数据列</param>
287 public virtual void Delete(List<DataRow> Rows)
288 {
289 if ((Rows != null) && (Rows.Count != 0))
290 {
291 ArrayList Keys = new ArrayList();
292 foreach (DataRow row in Rows)
293 {
294 Keys.Add(row[this.KeyName]);
295 }
296 this.Delete(Keys);
297 }
298 }
299
300
301
302 /// <summary>
303 /// 删除指定的实体对象
304 /// </summary>
305 /// <param name="Entitys">删除实体对象</param>
306 public virtual void Delete(List<BaseEntity> entitys)
307 {
308 if ((entitys != null) && (entitys.Count != 0))
309 {
310 ArrayList Keys = new ArrayList();
311 foreach (BaseEntity enetity in entitys)
312 {
313 Keys.Add(enetity.Key);
314 }
315 this.Delete(Keys);
316 }
317 }
318
319 /// <summary>
320 /// 删除指定的数据列数组
321 /// </summary>
322 /// <param name="Rows">删除数据列数组</param>
323 public virtual void Delete(DataRow[] Rows)
324 {
325 if ((Rows != null) && (Rows.Length != 0))
326 {
327 ArrayList Keys = new ArrayList();
328 foreach (DataRow row in Rows)
329 {
330 Keys.Add(row[this.KeyName]);
331 }
332
333 this.Delete(Keys);
334 }
335 }
336
337
338 /// <summary>
339 /// 删除指定主键的数据
340 /// </summary>
341 /// <param name="Keys">删除主键数组</param>
342 public virtual void Delete(ArrayList Keys)
343 {
344 if (ObjectService.DataAccess.DriverManager.Current.ReadOnly) return;
345
346
347 if (this is ObjectService.Childs.IHasChildDataEntity)
348 ObjectService.Childs.ChildManager.Current.Registr(TableName);
349
350 if (this.OnDeleting != null)
351 this.OnDeleting(this, Keys);
352
353
354 if (this.Trigger != null)
355 {
356 this.Trigger.OnDeleting(this, Keys);
357 }
358
359 List<object> keys = new List<object>();
360
361 for (int i = 0; i < Keys.Count; i++)
362 {
363 if (Keys[i] != null && !String.IsNullOrEmpty(Keys[i].ToString()) && !keys.Contains(Keys[i]))
364 keys.Add(Keys[i]);
365
366 if (keys.Count > 0 && keys[i] != null)
367 entityCache.Remove(keys[i]);
368 }
369
370
371
372 RelationHandler.Current.OnDeleted(this.Name, Keys);
373
374 if (Factory.DataLog != null)
375 {
376 if (keys.Count > 0)
377 Factory.DataLog.LogOnDelete(this, Keys);
378 }
379
380
381 object[] deleteKeys = new object[keys.Count];
382 keys.CopyTo(deleteKeys);
383
384 DataAccess.Delete(this.Table, deleteKeys);
385
386 if (this.OnDeleted != null)
387 this.OnDeleted(this, Keys);
388
389
390 if (this.Trigger != null)
391 {
392 this.Trigger.OnDeleted(this, Keys);
393 }
394
395 this.ClearCache();
396 }
397
398
399 /// <summary>
400 /// 删除指定主键的数据
401 /// </summary>
402 /// <param name="Key">删除主键</param>
403 public virtual void Delete(object Key)
404 {
405 if (this is ObjectService.Childs.IHasChildDataEntity)
406 ObjectService.Childs.ChildManager.Current.Registr(TableName);
407
408
409 object[] Keys = new object[] { Key };
410 ArrayList deleteKeys = new ArrayList();
411 deleteKeys.Add(Key);
412
413
414 Delete(deleteKeys);
415 }
416
417
418 /// <summary>
419 /// 获得指定主键的数据列
420 /// </summary>
421 /// <param name="Key">主键值</param>
422 /// <returns>数据列</returns>
423 public virtual DataRow Edit(object Key)
424 {
425 if (Key == null)
426 {
427 return null;
428 }
429
430
431 if (string.IsNullOrEmpty(Key.ToString()))
432 {
433 return null;
434 }
435
436
437 if (Table.Rows.Count == this.maxRecordCount)
438 {
439 Table.Rows.Clear();
440 }
441
442 DataRow row = this.Table.Rows.Find(Key);
443 if (row == null)
444 {
445 string filter = Common.GetRecordFilter(this.Table, this.KeyName, "=", Key.ToString());
446 DataAccess.LoadByCondition(this.Table.DataSet, filter, this.TableName);
447 row = this.Table.Rows.Find(Key);
448
449 if (this.OnEdit != null)
450 this.OnEdit(this, row);
451
452 if (this.Trigger != null)
453 {
454 this.Trigger.OnEdit(this, row);
455 }
456 }
457
458
459 return row;
460 }
461
462
463 /// <summary>
464 /// 获得符合数据筛选条件的DataView对象
465 /// </summary>
466 /// <param name="recordFilter">数据筛选条件</param>
467 /// <returns>DataView对象</returns>
468 public virtual DataView GetDataView(string recordFilter)
469 {
470 System.Data.DataSet dataSet = new System.Data.DataSet();
471
472 DataAccess.LoadByCondition(dataSet, GetRecordFilter(recordFilter), ViewName);
473
474 return new DataView(dataSet.Tables[this.ViewName]);
475 }
476
477 /// <summary>
478 /// 获得符合数据筛选条件的DataView对象,并排序
479 /// </summary>
480 /// <param name="recordFilter">数据筛选条件</param>
481 /// <param name="order">排序条件</param>
482 /// <returns>DataView对象</returns>
483 public virtual DataView GetDataView(string recordFilter, string order)
484 {
485 System.Data.DataSet dataSet = new System.Data.DataSet();
486
487 DataAccess.LoadByCondition(dataSet,GetRecordFilter(recordFilter),order, ViewName);
488
489 return new DataView(dataSet.Tables[this.ViewName]);
490 }
491
492 /// <summary>
493 /// 获得指定范围、符合数据筛选条件的DataView对象,并排序
494 /// </summary>
495 /// <param name="startRecord">开始记录位置</param>
496 /// <param name="maxRecord">记录数</param>
497 /// <param name="recordFilter">数据筛选条件</param>
498 /// <param name="order">排序条件</param>
499 /// <returns>DataView对象</returns>
500 public virtual DataView GetDataView(int startRecord, int maxRecord, string recordFilter, string order)
501 {
502 System.Data.DataSet dataSet = new System.Data.DataSet();
503
504 this.LoadData(dataSet, startRecord, maxRecord, recordFilter, order, false);
505
506 return new DataView(dataSet.Tables[this.ViewName]);
507 }
508
509 /// <summary>
510 /// 获得指定范围和数据筛选条件的数据视图,并排序,同时指明是否从缓存中
511 /// </summary>
512 /// <param name="startRecord">开始记录位置</param>
513 /// <param name="maxRecord">记录数</param>
514 /// <param name="recordFilter">数据筛选条件</param>
515 /// <param name="order">排序条件</param>
516 /// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
517 /// <returns>数据视图</returns>
518 public virtual DataView GetDataView(int startRecord, int maxRecord, string recordFilter, string order, bool isLoad)
519 {
520 System.Data.DataSet dataSet = new System.Data.DataSet();
521
522 this.LoadData(dataSet, startRecord, maxRecord, recordFilter, order, isLoad);
523
524 return new DataView(dataSet.Tables[this.ViewName]);
525 }
526
527 /// <summary>
528 /// 获得编辑页面Url
529 /// </summary>
530 /// <returns>编辑页面Url</returns>
531 public virtual string GetEditLogic()
532 {
533 return this.TableEntity.GetUILogic(UIStatus.Edit);
534 }
535
536
537 /// <summary>
538 /// 根据主键值,获得对应数据列的指定字段的值
539 /// </summary>
540 /// <param name="FieldName">字段名称</param>
541 /// <param name="key">主键值</param>
542 /// <returns>字段值</returns>
543 public virtual string GetNameById(string FieldName, object Id)
544 {
545 if(Table.Columns[FieldName] == null) return String.Empty;
546
547 DataRow row = Edit(Id);
548
549 return row != null ? row[FieldName].ToString() : String.Empty ;
550 }
551
552 /// <summary>
553 /// 根据数据筛选条件,获得对应的记录数
554 /// </summary>
555 /// <param name="recordFilter">数据筛选条件</param>
556 /// <returns>记录数</returns>
557 public virtual int GetRecordCount(string recordFilter)
558 {
559 string table = this.TableName;
560
561 if (this.isGetRecordCountByView)
562 {
563 table = this.ViewName;
564 }
565
566 return DataAccess.GetRecordCount(table, this.GetRecordFilter( recordFilter));
567 }
568
569 /// <summary>
570 /// 获得对应的数据筛选条件
571 /// </summary>
572 /// <param name="recordFilter">数据筛选条件</param>
573 /// <returns>数据筛选条件</returns>
574 protected virtual string GetRecordFilter( string recordFilter)
575 {
576 return ObjectService.Factory.GetRecordFilter(TableName, recordFilter);
577 }
578
579
580
581 /// <summary>
582 /// 获得列表页面Url
583 /// </summary>
584 /// <returns>列表页面Url</returns>
585 public virtual string GetTableLogic()
586 {
587 return this.TableEntity.GetUILogic(UIStatus.Save);
588 }
589
590 /// <summary>
591 /// 根据编辑状态获得对应的页面Url
592 /// </summary>
593 /// <param name="status">编辑状态</param>
594 /// <returns>页面Url</returns>
595 public virtual string GetUILogic(UIStatus status)
596 {
597 return this.TableEntity.GetUILogic(status);
598 }
599
600
601 /// <summary>
602 /// 将数据加载到DataSet
603 /// </summary>
604 /// <param name="dataSet">数据集</param>
605 /// <param name="recordFilter">数据筛选条件</param>
606 /// <param name="order">排序防守</param>
607 /// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
608 /// <returns>符合条件的记录数</returns>
609 public virtual int LoadData(System.Data.DataSet dataSet, string recordFilter, string order, bool isLoad)
610 {
611 string filter = this.GetRecordFilter( recordFilter);
612
613 DataCache saveData = null;
614
615 string CacheName = string.Concat(new object[] { filter, ":", order, ":" });
616
617 if (this.CacheManager.Contains(CacheName))
618 saveData = (DataCache)this.CacheManager[CacheName];
619
620 if (!(saveData != null && saveData.DataSet != null) || isLoad)
621 {
622 if (this.CacheManager.Contains(CacheName))
623 {
624 this.CacheManager.Remove(CacheName);
625 }
626
627 saveData = new DataCache();
628
629 DataAccess.LoadByCondition(dataSet, filter,order, ViewName);
630
631 if (this.Trigger != null && this.TableName == this.ViewName)
632 {
633 this.Trigger.OnLoad(this, dataSet);
634 }
635
636 saveData.DataSet = dataSet;
637 this.CacheManager.Add(CacheName, saveData, CacheItemPriority.Normal, null, new ICacheItemExpiration[] { new SlidingTime(TimeSpan.FromMinutes(1)) });
638 this.cacheNameList.Add(CacheName);
639
640 saveData.RecordCount = GetRecordCount(filter);
641
642 this.CacheManager.Add(CacheName, saveData, CacheItemPriority.Normal, null, new ICacheItemExpiration[] { new SlidingTime(TimeSpan.FromMinutes(1)) });
643 this.cacheNameList.Add(CacheName);
644 }
645 else
646 {
647 dataSet.Merge(saveData.DataSet);
648 }
649
650 return saveData.RecordCount;
651 }
652
653
654 /// <summary>
655 /// 分页加载数据到数据集
656 /// </summary>
657 /// <param name="dataSet">数据集</param>
658 /// <param name="pageIndex">页编号</param>
659 /// <param name="pageSize">页记录数</param>
660 /// <param name="recordFilter">数据筛选条件</param>
661 /// <param name="order">排序条件</param>
662 /// <param name="isLoad">重新加载数据标志(True:直接加载数据,False:从缓存中获得数据)</param>
663 /// <returns>符合条件的记录数</returns>
664 public virtual int LoadData(System.Data.DataSet dataSet, int pageIndex, int pageSize, string recordFilter, string order, bool isLoad)
665 {
666 string filter = this.GetRecordFilter( recordFilter);
667
668 DataCache saveData = null;
669
670 string CacheName = string.Concat(new object[] { filter, ":", order, ":", pageIndex, ":", pageSize });
671
672 if (this.CacheManager.Contains(CacheName))
673 saveData = (DataCache)this.CacheManager[CacheName];
674
675 if (!(saveData != null && saveData.DataSet != null) || isLoad)
676 {
677 if (this.CacheManager.Contains(CacheName))
678 {
679 this.CacheManager.Remove(CacheName);
680 }
681
682 if (pageIndex == 0)
683 pageIndex = 1;
684
685 saveData = new DataCache();
686 saveData.RecordCount = DataAccess.LoadByCondition(dataSet, filter, order, this.ViewName, this.KeyName, pageIndex, pageSize);
687
688 if (this.Trigger != null )
689 {
690 this.Trigger.OnLoad(this, dataSet);
691 }
692
693 saveData.DataSet = dataSet.Clone();
694 saveData.DataSet.Merge(dataSet);
695
696 this.CacheManager.Add(CacheName, saveData, CacheItemPriority.Normal, null, new ICacheItemExpiration[] { new SlidingTime(TimeSpan.FromMinutes(1)) });
697 this.cacheNameList.Add(CacheName);
698 }
699 else
700 {
701
702 dataSet.Merge(saveData.DataSet);
703 }
704
705 return saveData.RecordCount;
706 }
707
708
709 /// <summary>
710 /// 保存数据
711 /// </summary>
712 /// <param name="Row">数据列</param>
713 public void Save(DataRow row)
714 {
715 Save(new DataRow[] { row });
716 }
717
718 /// <summary>
719 /// 保存数据
720 /// </summary>
721 /// <param name="Rows">数据列数组</param>
722 public virtual void Save(DataRow[] Rows)
723 {
724 if (ObjectService.DataAccess.DriverManager.Current.ReadOnly) return;
725
726 try
727 {
728 if (this.Trigger != null)
729 {
730 this.Trigger.OnSaving(this, Rows);
731 }
732
733 List<DataRow> addRows = new List<DataRow>();
734 List<DataRow> changeRows = new List<DataRow>();
735 List<DataRow> oldRows = new List<DataRow>();
736 foreach (DataRow row in Rows)
737 {
738 if (row != null)
739 {
740 if (Builders.ItemHandler.Current != null)
741 {
742 Builders.ItemHandler.Current.Generate(row);
743 }
744
745 switch (row.RowState)
746 {
747 case DataRowState.Detached:
748
749 DataRow oldRow = Table.Rows.Find(row[KeyName]);
750 if (oldRow != null)
751 {
752 oldRow.ItemArray = row.ItemArray;
753 changeRows.Add(row);
754 }
755 else
756 {
757 this.Table.Rows.Add(row);
758 addRows.Add(row);
759 }
760
761 break;
762
763 case DataRowState.Modified:
764 changeRows.Add(row);
765 break;
766 }
767 }
768
769
770 }
771
772
773 System.Data.DataSet ds = new System.Data.DataSet();
774 StringBuilder filter = new StringBuilder();
775 int i = 0;
776 foreach (DataRow row in changeRows)
777 {
778 if (i > 0)
779 {
780 filter.Append(" and ");
781 }
782 filter.Append(Common.GetRecordFilter(this.Table, this.KeyName, "=", row[this.KeyName].ToString()));
783 i++;
784 }
785
786 DataAccess.LoadByCondition(ds, filter.ToString(), this.TableName);
787 foreach (DataRow row in changeRows)
788 {
789 DataRow oldrow = ds.Tables[this.TableName].Rows.Find(row[this.KeyName]);
790 if (oldrow != null)
791 {
792 oldRows.Add(oldrow);
793 }
794 }
795
796
797 RelationHandler.Current.OnAdded(this.name, addRows);
798 RelationHandler.Current.OnChanged(this.name, oldRows, changeRows);
799
800 DataAccess.Update(Rows);
801
802 if (this.Trigger != null)
803 {
804 this.Trigger.OnSaved(this, Rows);
805 }
806
807
808 if (Factory.DataLog != null)
809 {
810 if (addRows.Count > 0)
811 Factory.DataLog.LogOnAdd(this, addRows);
812
813 if (oldRows.Count > 0)
814 Factory.DataLog.LogOnSave(this, changeRows, oldRows);
815 }
816
817
818
819 foreach (DataRow row in Rows)
820 {
821 if (row != null)
822 {
823 DataRow oldRow = Table.Rows.Find(row[KeyName]);
824 if (oldRow != null)
825 {
826 oldRow.ItemArray = row.ItemArray;
827 }
828 else
829 {
830 this.Table.Rows.Add(row.ItemArray);
831 }
832 }
833
834 }
835
836
837
838 this.ClearCache();
839 }
840 catch (Exception error)
841 {
842 //记录错误信息
843 Factory.ErrorLog.Log(error);
844
845 throw new ApplicationException(error.Message);
846 }
847 }
848
849 /// <summary>
850 /// 选择出指定范围的数据列
851 /// </summary>
852 /// <param name="recordFilter">数据筛选条件</param>
853 /// <returns>符合条件的数据列数组</returns>
854 public void Save(List<DataRow> Rows)
855 {
856 if ((Rows != null) && (Rows.Count > 0))
857 {
858 DataRow[] saveRows = new DataRow[Rows.Count];
859 Rows.CopyTo(saveRows);
860 this.Save(saveRows);
861 }
862 }
863
864
865
866 /// <summary>
867 /// 选择出指定范围的数据列
868 /// </summary>
869 /// <param name="recordFilter">数据筛选条件</param>
870 /// <returns>符合条件的数据列数组</returns>
871 public DataRow[] Select(string recordFilter)
872 {
873 string Sql;
874
875 if(String.IsNullOrEmpty(recordFilter))
876 Sql = string.Format("Select * from {0} ", this.TableName);
877 else
878 Sql = string.Format("Select * from {0} where {1}", this.TableName, recordFilter);
879
880 System.Data.DataSet ds = new System.Data.DataSet();
881 DataAccess.Load(ds, Sql,this.TableName);
882
883 DataRow[] rows = new DataRow[ds.Tables[TableName].Rows.Count];
884 ds.Tables[TableName].Rows.CopyTo(rows, 0);
885
886 return rows ;
887 }
888
889
890
891
892 /// <summary>
893 /// 选择出指定范围的数据列,并排序
894 /// </summary>
895 /// <param name="recordFilter">数据筛选条件</param>
896 /// <param name="order">排序条件</param>
897 /// <returns>符合条件并已经排序的数据列数组</returns>
898 public DataRow[] Select(string recordFilter, string order)
899 {
900 string Sql;
901
902 if (String.IsNullOrEmpty(order))
903 Sql = string.Format("Select * from {0} where {1} ", this.TableName, recordFilter);
904 else
905 Sql = string.Format("Select * from {0} where {1} order by {2} ", this.TableName, recordFilter, order);
906
907
908 System.Data.DataSet ds = new System.Data.DataSet();
909 DataAccess.Load(ds, Sql, this.TableName);
910
911
912 DataView view = new DataView(ds.Tables[TableName]);
913 view.Sort = order;
914
915 DataRow[] rows = new DataRow[view.Count];
916 for (int i = 0; i < view.Count; i += 1)
917 {
918 rows[i] = view[i].Row;
919 }
920
921 return rows;
922 }
923
924
925
926 /// <summary>
927 /// 选择出指定范围的数据列,并放入泛型数组中
928 /// </summary>
929 /// <param name="selectRows"></param>
930 /// <param name="recordFilter"></param>
931 public void Select(List<DataRow> selectRows, string recordFilter)
932 {
933 DataRow[] rows = Select(recordFilter);
934
935 foreach (DataRow row in rows)
936 {
937 if (!selectRows.Contains(row))
938 selectRows.Add(row);
939 }
940 }
941
942 /// <summary>
943 /// 合并数据
944 /// </summary>
945 /// <param name="DataSet">数据集</param>
946 public virtual void Merge(System.Data.DataSet DataSet)
947 {
948 if (DataSet.Tables[TableName] == null) return;
949
950 Common.SetPrimaryKey(DataSet.Tables[TableName], this.KeyName);
951
952
953 List<DataRow> addRows = new List<DataRow>();
954 foreach (DataRow row in DataSet.Tables[TableName].Rows)
955 {
956 DataRow addRow = Edit(row[KeyName]);
957 if (addRow == null)
958 addRow = Add();
959
960 foreach (System.Data.DataColumn column in Table.Columns)
961 {
962 addRow[column.ColumnName] = row[column.ColumnName];
963 }
964
965
966 addRows.Add(addRow);
967 }
968
969 Save(addRows);
970 }
971
972 /// <summary>
973 /// 获得字段的数据筛选表达式
974 /// </summary>
975 /// <param name="fieldName">字段名</param>
976 /// <param name="fieldValue">字段值</param>
977 /// <returns>数据筛选表达式</returns>
978 public virtual string GetFilter(string fieldName, string fieldValue)
979 {
980 return String.Format("{0}='{1}'", fieldName, fieldValue);
981 }
982
983
984
985 /// <summary>
986 /// 缓存对象
987 /// </summary>
988 protected Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager CacheManager
989 {
990 get
991 {
992 return (Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager)CacheFactory.GetCacheManager();
993 }
994 }
995
996
997
998
999 /// <summary>
1000 /// 静态数据对象标志
1001 /// </summary>
1002 public bool IsStatic
1003 {
1004 get
1005 {
1006 return false;
1007 }
1008 }
1009
1010 /// <summary>
1011 /// 主键名称
1012 /// </summary>
1013 public string KeyName
1014 {
1015 get
1016 {
1017 return this.TableEntity.KeyName;
1018 }
1019 }
1020
1021 /// <summary>
1022 /// 名称
1023 /// </summary>
1024 public string Name
1025 {
1026 get
1027 {
1028 return this.name;
1029 }
1030 }
1031
1032 /// <summary>
1033 /// 数据表对象
1034 /// </summary>
1035 public DataTable Table
1036 {
1037 get
1038 {
1039
1040 if (table == null )
1041 {
1042 table = this.TableEntity.Table.Clone();
1043
1044 System.Data.DataSet ds = new System.Data.DataSet();
1045 ds.Tables.Add(table);
1046 }
1047
1048 return table;
1049 }
1050 }
1051
1052
1053 /// <summary>
1054 /// 数据表信息实体对象
1055 /// </summary>
1056 protected AspxWork.FormHelper.TableEntity TableEntity
1057 {
1058 get
1059 {
1060 return AspxWork.FormHelper.TableHandler.Current[this.name];
1061 }
1062 }
1063
1064 /// <summary>
1065 /// 数据表名称
1066 /// </summary>
1067 public string TableName
1068 {
1069 get
1070 {
1071 return this.TableEntity.GetTableName();
1072 }
1073 }
1074
1075
1076 /// <summary>
1077 /// 数据视图表
1078 /// </summary>
1079 public DataTable View
1080 {
1081 get
1082 {
1083 if (this.viewTable == null)
1084 {
1085 this.viewTable = DataAccess.GetTable(this.ViewName, "select * from " + this.ViewName + " where 1=2");
1086 }
1087 return this.viewTable;
1088 }
1089 }
1090
1091
1092 /// <summary>
1093 /// 数据视图名称
1094 /// </summary>
1095 public string ViewName
1096 {
1097 get
1098 {
1099 return this.TableEntity.GetViewName();
1100 }
1101 }
1102
1103
1104
1105 /// <summary>
1106 /// 获得分组数据
1107 /// </summary>
1108 /// <param name="groupColumn">分组栏目</param>
1109 /// <param name="filter">数据筛选条件</param>
1110 /// <param name="order">数据排序条件</param>
1111 /// <returns>分组数据集</returns>
1112 public System.Data.DataSet GetGroupCount(string groupColumn, string filter, string order)
1113 {
1114 string sql = string.Format("SELECT {0},Count(*) FROM {3} {2} GROUP BY {0} ORDER BY {1}",
1115 groupColumn, order, (String.IsNullOrEmpty(filter) ? "" : "WHERE " + filter), ViewName);
1116
1117 System.Data.DataSet ds = DataAccess.ExecuteDataSet(sql);
1118
1119 DataTable table = new DataTable();
1120
1121 DataColumn column = new DataColumn(groupColumn, typeof(String));
1122 table.Columns.Add(column);
1123 table.PrimaryKey = new DataColumn[] { column };
1124
1125 table.Columns.Add("Count", typeof(int));
1126
1127
1128
1129 foreach (DataRow row in ds.Tables[0].Rows)
1130 {
1131 DataRow addRow = table.NewRow();
1132 addRow[0] = row[0];
1133 addRow[1] = row[1];
1134
1135 table.Rows.Add(addRow);
1136
1137 if (Trigger != null)
1138 {
1139 Trigger.OnLoaded(this, addRow);
1140 }
1141 }
1142
1143
1144 System.Data.DataSet newDs = new System.Data.DataSet();
1145 newDs.Tables.Add(table);
1146
1147 return newDs;
1148
1149 }
1150
1151 #region 数据委托
1152
1153
1154 /// <summary>
1155 /// 数据添加委托事件
1156 /// </summary>
1157 OnAddEvent onAdd;
1158
1159 /// <summary>
1160 /// 数据已经删除委托事件
1161 /// </summary>
1162 OnDeletedEvent onDeleted;
1163
1164 /// <summary>
1165 /// 数据删除委托事件
1166 /// </summary>
1167 OnDeletingEvent onDeleting;
1168
1169
1170 /// <summary>
1171 /// 数据编辑委托事件
1172 /// </summary>
1173 OnEditEvent onEdit;
1174
1175
1176 /// <summary>
1177 /// 数据已经保存委托事件
1178 /// </summary>
1179 OnSavedEvent onSaved;
1180
1181 /// <summary>
1182 /// 数据保存委托事件
1183 /// </summary>
1184 OnSavingEvent onSaving;
1185
1186 /// <summary>
1187 /// 数据加载委托事件
1188 /// </summary>
1189 OnLoadEvent onLoad;
1190
1191
1192 /// <summary>
1193 /// 数据添加事件
1194 /// </summary>
1195 public OnAddEvent OnAdd
1196 {
1197 get
1198 {
1199 return onAdd;
1200 }
1201 set
1202 {
1203 onAdd = value;
1204 }
1205 }
1206
1207 /// <summary>
1208 /// 数据已经删除事件
1209 /// </summary>
1210 public OnDeletedEvent OnDeleted
1211 {
1212 get
1213 {
1214 return onDeleted;
1215 }
1216 set
1217 {
1218 onDeleted = value;
1219 }
1220 }
1221
1222
1223 /// <summary>
1224 /// 数据删除时事件
1225 /// </summary>
1226 public OnDeletingEvent OnDeleting
1227 {
1228 get
1229 {
1230 return onDeleting;
1231 }
1232 set
1233 {
1234 onDeleting = value;
1235 }
1236 }
1237
1238
1239
1240 /// <summary>
1241 /// 数据编辑事件
1242 /// </summary>
1243 public OnEditEvent OnEdit
1244 {
1245 get
1246 {
1247 return onEdit;
1248 }
1249 set
1250 {
1251 onEdit = value;
1252 }
1253 }
1254
1255
1256
1257 /// <summary>
1258 /// 数据已经保存事件
1259 /// </summary>
1260 public OnSavedEvent OnSaved
1261 {
1262 get
1263 {
1264 return onSaved;
1265 }
1266 set
1267 {
1268 onSaved = value;
1269 }
1270 }
1271
1272
1273 /// <summary>
1274 /// 数据保存时事件
1275 /// </summary>
1276 public OnSavingEvent OnSaving
1277 {
1278 get
1279 {
1280 return onSaving;
1281 }
1282 set
1283 {
1284 onSaving = value;
1285 }
1286 }
1287
1288 /// <summary>
1289 /// 数据加载时事件
1290 /// </summary>
1291 public OnLoadEvent OnLoad
1292 {
1293 get
1294 {
1295 return onLoad;
1296 }
1297 set
1298 {
1299 onLoad = value;
1300 }
1301 }
1302
1303 #endregion
1304
1305 }
1306 }
1307
通常情况下,如果不需要特殊的业务逻辑处理,直接将构件的数据对象定义为上述数据对象即可,如果需要加入特殊的业务处理逻辑,只需继承对象,加入业务处理代码即可。
在下一篇文章中,我将详细讲解 !