描述一个数据表
1

/// <summary>
2
/// 描述一个数据表
3
/// </summary>
4
public class Table
5

{
6
7
protected Table(string name, ColumnCollection cols)
8
9
TableName = name;
10
Columns = cols;
11
Rows = new RowCollection();
12
}
13
14
15
/// 获取或设置表的列架构
16
17
protected readonly ColumnCollection Columns;
18
19
/// 获取或设置表的数据行
20
21
protected RowCollection Rows;
22
23
/// 获取或设置表的名称
24
25
protected readonly string TableName;
26
27
28
/// 将该行数据状态改为RowState.Unchanged
29
30
protected void AcceptChanges()
31
32
for (int i = 0; i <= Rows.Count - 1; i++)
33
34
Rows[i].AcceptChanges();
35
36
37
38
39
/// 移除所有数据行
40
41
protected void Clear()
42
43
this.Rows.Clear();
44
45
46
47
/// 返回状态匹配的数据行,并以新数据表的形式体现
48
49
/// <param name="rowState"></param>
50
/// <returns></returns>
51
protected Table GetChanges(RowState rowState)
52
53
Table tmpTable = new Table(rowState.ToString(), this.Columns);
54
foreach (Row row in Rows)
55
56
if (row.RowState == rowState)
57
58
tmpTable.Rows.Add(row);
59
60
61
return tmpTable;
62
63
64
65
66
/// 依据现有的数据表的架构,生成新的数据行
67
68
69
protected Row NewRow()
70
71
return new Row(Columns, this.TableName);
72
73
74
75
/// 按关键字(数据行的第一列)匹配,并返回符合的数据行
76
77
/// <param name="filterKey"></param>
78
79
protected Row[] Select(string filterKey)
80
81
System.Collections.ArrayList tmpRows = new System.Collections.ArrayList();
82
83
84
if (row[0].ToString() == filterKey)
85
86
tmpRows.Add(row);
87
88
89
return (Row[])tmpRows.ToArray(typeof(Row));
90
91
92
93
/// 打印Table的数据
94
95
public virtual void Print()
96
97
System.Text.StringBuilder sb = new StringBuilder();
98
99
foreach (Column col in this.Columns)
100
101
sb.AppendFormat("{0}/t", col.ColumnName);
102
103
sb.AppendLine();
104
System.Console.WriteLine(sb.ToString());
105
foreach (Row row in this.Rows)
106
107
foreach (object obj in row.ItemArray)
108
109
System.Console.Write("{0}/t", obj);
110
111
System.Console.WriteLine();
112
113
114
好,到此Table所涉及的5个类都定义实现完毕了。
我们来回顾一下,Table中什么是最基础的,什么是最重要的?
按我们一般来看,表中最重要基础的实行,最重要的是数据。其实表最基础是的架构,架构就是列的定义,而行只不过是列的数据体现形式。
所以Table在构造的时候,必须先定义列的集合,而Row的构造函数是protected internal的,意义是不能在外面被构造,必须通过Table来依据列的定义来构造。
下篇,我们将看到我们对Table的继承,发挥更多OO体现。
本文转自shyleoking 51CTO博客,原文链接:http://blog.51cto.com/shyleoking/806271