XCodeFactory疑问--生成的数据对象类中的const字段有什么用啊?
2005-09-15 11:21
357 查看
比如在XCodeFactory3.0完全攻略--简单示例中的生成的Student代码中有如下const字段:
#region FieldName ,用于强化静态检查
public const string _ID = "ID" ;
public const string _Name = "Name" ;
public const string _Age = "Age" ;
public const string _ClassGrade = "ClassGrade" ;
public const string _MentorID = "MentorID" ;
public const string _Description = "Description" ;
#endregion
正如注释说的那样,这些字段的存在是为了强化静态检查。IDBAccesser接口和DataEntrance中的很多方法都要求传入where字句或数据库表的字段名称,比如,获取编号为1001的学生的年龄,使用DataEntrance是这样做的:
int age = (int)DataEntrance.GetFieldValue(typeof(Student) ,"1001" ,"Age") ;
如果哪天,Age字段改变了名字或者被删除了,上面的代码依然能编译通过,但在运行时就会抛出异常。我们需要一种方法来在编译时期就检查出这种错误。于是我想到了使用const成员来保存每个字段的名称,它们是一一对应的关系,使用const成员后,上面的代码变成这样:
int age = (int)DataEntrance.GetFieldValue(typeof(Student) ,"1001" ,Student._Age) ;
使用这样的方式,如果Age字段改变了名字或者被删除了,Student便没有了_Age这个const字段,所以编译就会发生错误,这样就把运行时发生的错误提前到了编译期,这对系统的稳定是大大有利的!
所以,在需要where字句的地方,我们经常这样用const字段:
string whereStr = string.Format("where {0} >'25' '" ,Student._Age) ;
Student[] students = (Student[])DataEntrance.GetObjects(typeof(Student) ,whereStr) ;
#region FieldName ,用于强化静态检查
public const string _ID = "ID" ;
public const string _Name = "Name" ;
public const string _Age = "Age" ;
public const string _ClassGrade = "ClassGrade" ;
public const string _MentorID = "MentorID" ;
public const string _Description = "Description" ;
#endregion
正如注释说的那样,这些字段的存在是为了强化静态检查。IDBAccesser接口和DataEntrance中的很多方法都要求传入where字句或数据库表的字段名称,比如,获取编号为1001的学生的年龄,使用DataEntrance是这样做的:
int age = (int)DataEntrance.GetFieldValue(typeof(Student) ,"1001" ,"Age") ;
如果哪天,Age字段改变了名字或者被删除了,上面的代码依然能编译通过,但在运行时就会抛出异常。我们需要一种方法来在编译时期就检查出这种错误。于是我想到了使用const成员来保存每个字段的名称,它们是一一对应的关系,使用const成员后,上面的代码变成这样:
int age = (int)DataEntrance.GetFieldValue(typeof(Student) ,"1001" ,Student._Age) ;
使用这样的方式,如果Age字段改变了名字或者被删除了,Student便没有了_Age这个const字段,所以编译就会发生错误,这样就把运行时发生的错误提前到了编译期,这对系统的稳定是大大有利的!
所以,在需要where字句的地方,我们经常这样用const字段:
string whereStr = string.Format("where {0} >'25' '" ,Student._Age) ;
Student[] students = (Student[])DataEntrance.GetObjects(typeof(Student) ,whereStr) ;
相关文章推荐
- 如何高效地使用XCodeFactory自动生成的数据层代码(一)
- 如何高效地使用XCodeFactory自动生成的数据层代码(二)
- 通过Spring.net来使用XCodeFactory生成的数据层!
- StringUtils.leftPad();//生成长度为多少位的数据,以什么进行补位
- 表里有一些默认的数据 用什么方法生成一条一条的insert into语句
- 如何使用XCodeFactory自动生成XML配置文件和对应的解析类?
- cognos类别生成不了,数据能够刷新,请问高手是什么原因啊?
- Laravel 5使用faker Model Factory生成网站模拟演示数据
- XCodeFactory 数据访问,再进一步简化!
- 考虑下列生成二进制的过程,编译器被用来生成单个单元的目标代码,链接器被用来将多个目标单元合并成一个程序二进制,链接器如何改变指令和数据到内存地址的绑定?需要什么信息从编译器传递给链接器,以协助完成链接
- 转载:如何使用XCodeFactory自动生成XML配置文件和对应的解析类
- Laravel 5使用faker Model Factory生成网站模拟演示数据
- 数据对象类代码的生成小工具
- XCodeFactory可以自动生成WebForm了!
- PowerDesigner生成数据字典
- 网页信息抓取进阶 支持Js生成数据 Jsoup的不足之处
- 如何在React中使用数据动态生成DOM标签
- Flex根据后台的数据自动生成AdvancedDataGrid
- hibernate导入大量数据时,为了避免内存中产生大量对象,在编码时注意什么,如何去除?
- oracle 生成整时数据