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