最近在写一个小程序的时候碰到一个问题,在此提出来,希望有哪位朋友给些指教。
组件名称:Produce.DataBase,其中使用了NHibernate库来进行相关业务表的持久化操作。所有的业务表对应的类都从一个名叫TableBase的基类继承而来。TableBase是一个抽象类,包含了绝大部分的单表所支持的操作,例如添加一条记录,更新某一条记录,获取所有记录集合,获取记录数等,其代码如下:
TableBase
其中一个业务表对应的持久化类定义如下:
1using System;
2using System.Collections;
3using System.Xml.Serialization;
4using ICSharpCode.Core;
5
6namespace Produce.DataBase
7{
8 /// <summary>
9 /// 设备类型表
10 /// </summary>
11 [Serializable]
12 public class DeviceTypeTable : TableBase
13 {
14 私有变量 -- 对应数据表字段
20
21 公开属性
38
39 构造方法
52 }
53}
2using System.Collections;
3using System.Xml.Serialization;
4using ICSharpCode.Core;
5
6namespace Produce.DataBase
7{
8 /// <summary>
9 /// 设备类型表
10 /// </summary>
11 [Serializable]
12 public class DeviceTypeTable : TableBase
13 {
14 私有变量 -- 对应数据表字段
20
21 公开属性
38
39 构造方法
52 }
53}
对应的测试类如下:
1using System;
2using System.Collections.Generic;
3using System.Text;
4using NUnit.Framework;
5using NHibernate;
6using Produce.DataBase;
7using System.Diagnostics;
8using System.Collections;
9
10namespace 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()为例,2using System.Collections.Generic;
3using System.Text;
4using NUnit.Framework;
5using NHibernate;
6using Produce.DataBase;
7using System.Diagnostics;
8using System.Collections;
9
10namespace 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类的静态构造方法,也就是一个类调用它的父类的静态方法的时候只会触发父类的静态构造方法,不会触发它自己的静态构造方法,除非调用它自己的静态方法,才会触发执行它的静态构造方法,那么这个问题如何解决哪?