最近在写一个小程序的时候碰到一个问题,在此提出来,希望有哪位朋友给些指教。
组件名称:Produce.DataBase,其中使用了NHibernate库来进行相关业务表的持久化操作。所有的业务表对应的类都从一个名叫TableBase的基类继承而来。TableBase是一个抽象类,包含了绝大部分的单表所支持的操作,例如添加一条记录,更新某一条记录,获取所有记录集合,获取记录数等,其代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
TableBase
1
using System;
2
using System.Collections;
3
using System.Threading;
4
using System.Collections.Generic;
5
using System.Reflection;
6
using System.ComponentModel;
7
using ICSharpCode.Core;
8
using System.Text;
9
using System.IO;
10
using NHibernate.Metadata;
11
using NHibernate;
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
namespace Produce.DataBase
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
15
public delegate void FindEntitiesDelegate(IList aList);
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
public abstract class TableBase
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有变量#region 私有变量
20
21
protected Dictionary<string, object> _dictionary = new Dictionary<string, object>();
22
protected static string _orderBySentence;
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
#endregion
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
公开属性#region 公开属性
27
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
29
/// 当前表的全部记录数
30
/// </summary>
31
public virtual int RecordCount
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
get
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
Type type = this.GetType();
36
return NHibernateHelper.GetRecordCount(type.FullName, null);
37
}
38
}
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
41
/// 获取属性文字描述和值的键值对
42
/// </summary>
43
public virtual Dictionary<string, object> Dictionary
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
get
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
if (_dictionary.Count == 0)
48
GetDictionary();
49
return _dictionary;
50
}
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
#endregion
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造方法#region 构造方法
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
public TableBase()
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
}
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
public TableBase(object existingId)
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
NHibernateHelper.Load(this, existingId);
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
#endregion
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
公开方法#region 公开方法
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
72
/// 创建记录
73
/// </summary>
74
public virtual void Create()
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
NHibernateHelper.Create(this);
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
80
/// 更新记录
81
/// </summary>
82
public virtual void Update()
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
NHibernateHelper.Update(this);
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
88
/// 删除记录
89
/// </summary>
90
public virtual void Delete()
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
NHibernateHelper.Delete(this);
93
}
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
96
/// 清空当前表的所有记录
97
/// </summary>
98
public virtual void Clear()
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
Type type = this.GetType();
101
ISessionFactory sessionFactory = SessionFactory.Factory;
102
IClassMetadata classMetadata = sessionFactory.GetClassMetadata(type);
103
string tableName = ((NHibernate.Persister.Entity.SingleTableEntityPersister)(classMetadata)).TableName;
104
string sqlText = string.Format("Delete From [{0}]", tableName);
105
NHibernateHelper.ExecuteNonQuery(sqlText);
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
109
/// 查询 - 获得当前表里面的所有数据
110
/// </summary>
111
/// <param name="delege"></param>
112
public void GetAllEntities(FindEntitiesDelegate delege)
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
Type type = this.GetType();
115
IList list = NHibernateHelper.GetEntities(type.FullName, null, _orderBySentence);
116
if (delege != null)
117
delege(list);
118
}
119![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
121
/// 查询 - 获得当前表里面的所有数据
122
/// </summary>
123
/// <param name="delege"></param>
124
public IList GetAllEntities()
125![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
126
Type type = this.GetType();
127
IList list = NHibernateHelper.GetEntities(type.FullName, null, _orderBySentence);
128
return list;
129
}
130![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
131![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
132
/// 根据条件返回查询到的数据集
133
/// </summary>
134
/// <param name="aWhere"></param>
135
/// <returns></returns>
136
public IList GetEntities(string aWhere)
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
Type type = this.GetType();
139
IList list = NHibernateHelper.GetEntities(type.FullName, aWhere, _orderBySentence);
140
return list;
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
144
/// 获取对象所有属性的中文解释列表
145
/// </summary>
146
public virtual List<string> GetPropDescriptions()
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
148
List<string> list = new List<string>();
149
Dictionary<string, object>.KeyCollection keyColl = GetDictionary().Keys;
150
foreach (string caption in keyColl)
151
list.Add(caption);
152
return list;
153
}
154
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
156
/// 获取对象中所有属性的值列表
157
/// </summary>
158
public virtual List<object> GetPropValues()
159![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
160
List<object> list = new List<object>();
161
Dictionary<string, object>.ValueCollection valueColl = GetDictionary().Values;
162
foreach (object obj in valueColl)
163
list.Add(obj);
164
return list;
165
}
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
168
/// 获取对象中所有属性的中文解释和值的键值对列表
169
/// </summary>
170
public virtual Dictionary<string, object> GetDictionary()
171![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
172
try
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
_dictionary.Clear();
175
DescriptionAttribute description = null;
176
PropertyInfo[] properties = this.GetType().GetProperties();
177
foreach (PropertyInfo prop in properties)
178![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
179
object[] keys = prop.GetCustomAttributes(typeof(DescriptionAttribute), true);
180
if (keys.Length == 1)
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
description = keys[0] as DescriptionAttribute;
183
_dictionary.Add(description.Description, prop.GetValue(this, null));
184
}
185
}
186
return _dictionary;
187
}
188
catch (Exception ex)
189![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
190
LoggingService.Error("获取属性中文描述与值的列表出现错误", ex);
191
throw ex;
192
}
193
}
194![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
195![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
196
/// 另存为CSV文件
197
/// </summary>
198
public virtual void SaveAsCSV(string aFilePath, IList aRecords)
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
StreamWriter streamWriter = null;
201
try
202![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
203
StringBuilder csvData = new StringBuilder();
204
//获取列名
205
csvData.Append(GetCSVCaption());
206
//获取数据
207
csvData.Append(GetCSVData(aRecords));
208
//写入数据到文件
209
streamWriter = new StreamWriter(aFilePath, false, Encoding.GetEncoding("GB2312"));
210
streamWriter.WriteLine(csvData.ToString());
211
streamWriter.Close();
212
}
213
catch (Exception ex)
214![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
215
LoggingService.Error("另存为CSV文件方法出现错误:", ex);
216
throw ex;
217
}
218
finally
219![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
220
if (streamWriter != null)
221
streamWriter.Close();
222
}
223
}
224![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
226
/// 另存为CSV文件
227
/// </summary>
228
public virtual void SaveAsCSV(string aFilePath, string aWhereText)
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
Type type = this.GetType();
231
IList records = NHibernateHelper.GetEntities(type.FullName, aWhereText, _orderBySentence);
232
if (records.Count == 0)
233
return;
234
SaveAsCSV(aFilePath, records);
235
}
236![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
237![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
238
/// 保存表中全部记录为CSV文件
239
/// </summary>
240
public virtual void SaveAsCSV(string aFilePath)
241![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
242
SaveAsCSV(aFilePath, string.Empty);
243
}
244![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
245
public override string ToString()
246![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
247
StringBuilder result = new StringBuilder();
248
List<object> list = GetPropValues();
249
foreach (object obj in list)
250
result.AppendFormat("{0},", obj.ToString());
251
return result.ToString();
252
}
253![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
254
#endregion
255![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
256![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有方法#region 私有方法
257![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
258
//获取标题
259
private string GetCSVCaption()
260![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
261
List<string> captions = this.GetPropDescriptions();
262
captions.Insert(0, "序号");
263
StringBuilder csvCaptions = new StringBuilder();
264
//写入列名
265
foreach (string caption in captions)
266
csvCaptions.AppendFormat("{0},", caption);
267
csvCaptions.Append("\n");
268
return csvCaptions.ToString();
269
}
270![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
271
//获取数据
272
private string GetCSVData(IList records)
273![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
274
int index = 1;
275
StringBuilder csvData = new StringBuilder();
276
foreach (object obj in records)
277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
278
//为了取消科学计数法加入一个Tab字符
279
csvData.AppendFormat("{0},\t{1},\n", index.ToString(), obj.ToString());
280
index += 1;
281
}
282
return csvData.ToString();
283
}
284![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
285
#endregion
286
}
287
}
288![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
其中一个业务表对应的持久化类定义如下:
1
using System;
2
using System.Collections;
3
using System.Xml.Serialization;
4
using ICSharpCode.Core;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
namespace Produce.DataBase
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
9
/// 设备类型表
10
/// </summary>
11
[Serializable]
12
public class DeviceTypeTable : TableBase
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有变量 -- 对应数据表字段#region 私有变量 -- 对应数据表字段
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
protected string _id;
17
protected string _name;
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
#endregion
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
公开属性#region 公开属性
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
[XmlElement(ElementName = "Id")]
24
public virtual string Id
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _id = value; }
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
[XmlElement(ElementName = "Name")]
31
public virtual string Name
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value; }
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
#endregion
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
构造方法#region 构造方法
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
public DeviceTypeTable()
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
_orderBySentence = "name Asc";
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
public DeviceTypeTable(string ID): base(ID)
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
_orderBySentence = "name Asc";
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
#endregion
52
}
53
}
对应的测试类如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using NUnit.Framework;
5
using NHibernate;
6
using Produce.DataBase;
7
using System.Diagnostics;
8
using System.Collections;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace Test.DataBase
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
13
/// 设备类型类-测试类
14
/// </summary>
15
[TestFixture]
16
public class DeviceTypeTableTest
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// 添加一批记录
20
/// </summary>
21
[Test]
22
public void TestInsert()
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
DeviceTypeTable deviceTypeTable = null;
25
string id = string.Empty;
26
for (int i = 1; i < 100; i++)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
deviceTypeTable = new DeviceTypeTable();
29
id = i.ToString().PadLeft(4, '0');
30
deviceTypeTable.Id = id;
31
deviceTypeTable.Name = "GT-" + id;
32
deviceTypeTable.Create();
33
}
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
37
/// 添加一条记录
38
/// </summary>
39
[Test]
40
public void TestInsertOne()
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
DeviceTypeTable deviceTypeTable = new DeviceTypeTable();
43
deviceTypeTable.Id = "9999";
44
deviceTypeTable.Name = "GT-9999";
45
deviceTypeTable.Create();
46
}
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
49
/// 更新指定的记录
50
/// </summary>
51
[Test]
52
public void TestUpdate()
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
DeviceTypeTable deviceTypeTable = new DeviceTypeTable();
55
deviceTypeTable.Id = "0001";
56
deviceTypeTable.Name = "GT-2000";
57
deviceTypeTable.Update();
58
}
59![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
61
/// 删除指定的记录
62
/// </summary>
63
[Test]
64
public void TestDelete()
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
DeviceTypeTable deviceTypeTable = new DeviceTypeTable("0001");
67
deviceTypeTable.Delete();
68
}
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
71
/// 获取指定的记录
72
/// </summary>
73
[Test]
74
public void TestGetEntity()
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
DeviceTypeTable deviceTypeTable = new DeviceTypeTable("0001");
77
Trace.WriteLine("设备型号是-------" + deviceTypeTable.Name);
78
}
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
81
/// 获取全部记录
82
/// </summary>
83
[Test]
84
public void TestGetAllEntities()
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
DeviceTypeTable deviceTypeTable = new DeviceTypeTable();
87
deviceTypeTable.GetAllEntities(deviceType_OnFindEntities);
88
}
89![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
91
/// 获取全部记录数
92
/// </summary>
93
[Test]
94
public void TestGetRecordCount()
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
DeviceTypeTable deviceTypeTable = new DeviceTypeTable();
97
Trace.WriteLine("记录数是:" + deviceTypeTable.RecordCount.ToString());
98
}
99![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
101
/// 清空表内数据
102
/// </summary>
103
[Test]
104
public void TestClear()
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
DeviceTypeTable deviceTypeTable = new DeviceTypeTable();
107
deviceTypeTable.Clear();
108
}
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110
private void deviceType_OnFindEntities(IList aList)
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
DeviceTypeTable deviceTypeTable = null;
113
foreach (object var in aList)
114![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
115
deviceTypeTable = (DeviceTypeTable)var;
116
Trace.WriteLine(deviceTypeTable.Id + "----" + deviceTypeTable.Name);
117
}
118
}
119
}
120
}
按照我的理解,其中的获取数据记录方法:
GetAllEntities,获取记录数:RecordCount,清空表数据:Clear等方法应该是作为静态方法处理,但是,以目前的实现方式,需要传入具体类实例的type.FullName作为参数,以方法GetAllEntities()为例,
public IList GetAllEntities()
{
Type type = this.GetType();
IList list = NHibernateHelper.GetEntities(type.FullName, null, _orderBySentence);
return list;
}
在获取DeviceTypeTable类对应的表里面的所有数据时集合时,就要传入一个DeviceTypeTable类的实例(this)为参数,
但是我觉得这个GetAllEntities()方法应该是属于静态方法,但是如果把GetAllEntities修改为静态方法的话,GetEntities方法中所需要的Table名称参数不能再通过this.GetType()方式获得,目前想到的解决办法是在TableBase类中添加一个字段:
protected static string _classFullName;
然后在TableBase的子类的静态构造方法中进行初始化,比如,DeviceTypeTable类中的实现方式如下:
static DeviceTypeTable()
{
_classFullName = "Produce.DataBase.DeviceTypeTable";
}
然后在GetAllEntities()方法中把NHibernateHelper.GetEntities(type.FullName, null, _orderBySentence);方法中的
参数type.FullName修改为_classFullName就可以了,但是在实际调试中发现,在调用如下代码:
DeviceTypeTable.GetAllEntities();时,不会触发DeviceTypeTable类的静态构造方法,也就是一个类调用它的父类的静态方法的时候只会触发父类的静态构造方法,不会触发它自己的静态构造方法,除非调用它自己的静态方法,才会触发执行它的静态构造方法,那么这个问题如何解决哪?