C#根据反射和特性实现ORM映射实例分析
本文实例讲述了C#根据反射和特性实现ORM映射的方法。分享给大家供大家参考。具体如下:
(一)关于反射
什么是反射?
反射就是在运行时,动态获取对象信息的方法。比如:运行时获得对象有哪些属性,方法,委托等。
反射的作用?
能够实现运行时,动态调用对象的方法,以及动态设置、获取属性值等。
反射的示例:
usingSystem;
usingSystem.Reflection;
namespaceCS_Test
{
publicclassMyStudent
{
privatestringsName;
publicstringSName
{
get{returnsName;}
set{sName=value;}
}
privateintsAge;
publicintSAge
{
get{returnsAge;}
set{sAge=value;}
}
}
classTest_Attribute
{
[STAThread]
staticvoidMain(string[]args)
{
MyStudentstu1=newMyStudent();
stu1.SName="刘德华";
stu1.SAge=40;
//获得所有属性
PropertyInfo[]pros=stu1.GetType().GetProperties();
//显示所有属性值
foreach(PropertyInfoproinpros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
//更改stu1对象的SAge值
stu1.GetType().GetProperty("SAge").SetValue(stu1,30,null);
//显示所有属性值
foreach(PropertyInfoproinpros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
}
}
}
(二)关于特性
什么是Attribute?
它是对运行时的对象或对象的属性、方法、委托等进行描述的类。
Attribute的作用?
用于在运行时,描述你的代码或者影响你的程序的行为。
注意:
既然Attribute是类,那么它的定义与定义类一样。
唯一不同的就是,自定义Attribute类必须继承于System.Attribute空间。
特性的示例:
//描述数据库字段的Attribute
publicclassDataFieldAttribute:Attribute
{
publicDataFieldAttribute(stringfieldName,stringfieldType)
{
this._fieldName=fieldName;
this._fieldType=fieldType;
}
//数据库字段名
privatestring_fieldName;
publicstringFieldName
{
get{return_fieldName;}
set{_fieldName=value;}
}
//数据库字段类型
privatestring_fieldType;
publicstringFieldType
{
get{return_fieldType;}
set{_fieldType=value;}
}
}
通过自定义Attribute,我们定义了类属性和数据库字段的一一对应关系,于是对MyStudent类的Name、Age属性都加上Attribute的描述,指定他们对应的数据库字段名以及类型。
代码更改如下:
publicclassMyStudent
{
privatestring_name;
//使用“特性”描述对应的数据库字段名、类型
[DataFieldAttribute("SName","varchar")]
publicstringName
{
get{return_name;}
set{_name=value;}
}
privateint_age;
[DataFieldAttribute("SAge","int")]
publicintAge
{
get{return_age;}
set{_age=value;}
}
}
(三)ORM映射规则的定义
给实体类增加DataFieldAttribute的描述,其实就是增加了O(对象)/R(关系数据库)的映射规则。
下面我们就通过反射的方法实现:在运行时动态获取O/R Mapping的映射规则:
staticvoidMain(string[]args)
{
MyStudentstu1=newMyStudent();
stu1.Name="刘德华";
stu1.Age=40;
//通过反射的方法来动态获取此映射规则
PropertyInfo[]infos=stu1.GetType().GetProperties();
object[]objs_fieldAttr=null;
foreach(PropertyInfoinfoininfos)
{
//GetCustomAttributes:
//返回实体对象中每个属性对应的“特性”信息(数据库字段名、类型)
objs_fieldAttr=info.GetCustomAttributes(
typeof(DataFieldAttribute),false);
if(objs_fieldAttr!=null)
{
Console.Write("实体类的属性名:"+info.Name);
Console.Write("->数据库字段名:");
Console.WriteLine(((DataFieldAttribute)objs_fieldAttr[0]).FieldName);
}
}
}
显示结果:
实体类的属性名:Name->数据库字段名:SName
实体类的属性名:Age->数据库字段名:SAge
接下来的工作就是:怎样根据这种方法动态地从对象中获取映射规则,然后动态构造Insert、Update、Delete等SQL语句。这就是实现自己的ORM的原理。
这里的代码仅仅是举例,而要真正实现一个ORM,我们还需要考虑的很多,比如:
1、实体类对应于哪张数据库表?
2、数据库表中的PK 和FK(如果有的话)怎么表示?
完整代码如下:
usingSystem;
usingSystem.Reflection;
namespaceCS_Test
{
publicclassMyStudent
{
privatestring_name;
//使用“特性”描述对应的数据库字段名、类型
[DataFieldAttribute("SName","varchar")]
publicstringName
{
get{return_name;}
set{_name=value;}
}
privateint_age;
[DataFieldAttribute("SAge","int")]
publicintAge
{
get{return_age;}
set{_age=value;}
}
}
//描述数据库字段的Attribute
publicclassDataFieldAttribute:Attribute
{
publicDataFieldAttribute(stringfieldName,stringfieldType)
{
this._fieldName=fieldName;
this._fieldType=fieldType;
}
//数据库字段名
privatestring_fieldName;
publicstringFieldName
{
get{return_fieldName;}
set{_fieldName=value;}
}
//数据库字段类型
privatestring_fieldType;
publicstringFieldType
{
get{return_fieldType;}
set{_fieldType=value;}
}
}
classTest_Attribute
{
[STAThread]
staticvoidMain(string[]args)
{
MyStudentstu1=newMyStudent();
stu1.Name="刘德华";
stu1.Age=40;
//获得所有属性
PropertyInfo[]pros=stu1.GetType().GetProperties();
//显示所有属性值
foreach(PropertyInfoproinpros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
//更改stu1对象的SAge值
stu1.GetType().GetProperty("Age").SetValue(stu1,30,null);
//显示所有属性值
foreach(PropertyInfoproinpros)
Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
//通过反射的方法来动态获取此映射规则
PropertyInfo[]infos=stu1.GetType().GetProperties();
object[]objs_fieldAttr=null;
foreach(PropertyInfoinfoininfos)
{
//GetCustomAttributes:
//返回实体中每个属性对应的“特性”信息(数据库字段名、类型)
objs_fieldAttr=info.GetCustomAttributes(
typeof(DataFieldAttribute),false);
if(objs_fieldAttr!=null)
{
Console.Write("实体类的属性名:"+info.Name);
Console.Write("->数据库字段名:");
Console.WriteLine(((DataFieldAttribute)objs_fieldAttr[0]).FieldName);
}
}
}
}
}
希望本文所述对大家的C#程序设计有所帮助。