国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#根據反射和特性實現ORM映射實例分析

2020-01-24 02:03:12
字體:
來源:轉載
供稿:網友

本文實例講述了C#根據反射和特性實現ORM 映射的方法。分享給大家供大家參考。具體如下:

(一)關于反射

什么是反射?

反射就是在運行時,動態獲取對象信息的方法。比如:運行時獲得對象有哪些屬性,方法,委托等。

反射的作用?

能夠實現運行時,動態調用對象的方法,以及動態設置、獲取屬性值等。

反射的示例:

using System;using System.Reflection;namespace CS_Test{  public class MyStudent  {    private string sName;    public string SName    {      get { return sName; }      set { sName = value; }    }    private int sAge;    public int SAge    {      get { return sAge; }      set { sAge = value; }    }  }  class Test_Attribute  {    [STAThread]    static void Main(string[] args)    {      MyStudent stu1 = new MyStudent();      stu1.SName = "劉德華";      stu1.SAge = 40;      // 獲得所有屬性      PropertyInfo[] pros = stu1.GetType().GetProperties();      // 顯示所有屬性值      foreach (PropertyInfo pro in pros)       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));      //更改stu1對象的SAge值      stu1.GetType().GetProperty("SAge").SetValue(stu1, 30, null);      // 顯示所有屬性值      foreach (PropertyInfo pro in pros)       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1, null));    }  }}

(二)關于特性

什么是 Attribute?

它是對運行時的對象或對象的屬性、方法、委托等進行描述的類。
Attribute 的作用?
用于在運行時,描述你的代碼或者影響你的程序的行為。

注意:

既然Attribute 是類,那么它的定義與定義類一樣。
唯一不同的就是,自定義Attribute 類必須繼承于System.Attribute 空間。
特性的示例:

//描述數據庫字段的 Attributepublic class DataFieldAttribute : Attribute{    public DataFieldAttribute(string fieldName,string fieldType)    {      this._fieldName = fieldName;      this._fieldType = fieldType;    }    // 數據庫字段名    private string _fieldName;    public string FieldName    {      get { return _fieldName; }      set { _fieldName = value; }    }    // 數據庫字段類型    private string _fieldType;    public string FieldType    {      get { return _fieldType; }      set { _fieldType = value; }    }}

通過自定義Attribute,我們定義了類屬性和數據庫字段的一一對應關系,于是對MyStudent 類的Name、Age 屬性都加上Attribute 的描述,指定他們對應的數據庫字段名以及類型。
代碼更改如下:

public class MyStudent{    private string _name;    //使用“特性”描述對應的數據庫字段名、類型    [DataFieldAttribute("SName", "varchar")]    public string Name    {      get { return _name; }      set { _name = value; }    }    private int _age;    [DataFieldAttribute("SAge", "int")]    public int Age    {      get { return _age; }      set { _age = value; }    }}

(三)ORM 映射規則的定義

給實體類增加DataFieldAttribute 的描述,其實就是增加了O(對象)/ R(關系數據庫)的映射規則。
下面我們就通過反射的方法實現:在運行時動態獲取O/R  Mapping 的映射規則:

static void Main(string[] args){  MyStudent stu1 = new MyStudent();  stu1.Name = "劉德華";  stu1.Age = 40;  //通過反射的方法來動態獲取此映射規則  PropertyInfo[] infos = stu1.GetType().GetProperties();  object[] objs_fieldAttr = null;  foreach (PropertyInfo info in infos)  {    // 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(如果有的話)怎么表示?

完整代碼如下:

using System;using System.Reflection;namespace CS_Test{  public class MyStudent  {    private string _name;    //使用“特性”描述對應的數據庫字段名、類型    [DataFieldAttribute("SName", "varchar")]    public string Name    {      get { return _name; }      set { _name = value; }    }    private int _age;    [DataFieldAttribute("SAge", "int")]    public int Age    {      get { return _age; }      set { _age = value; }    }  }  //描述數據庫字段的 Attribute  public class DataFieldAttribute : Attribute  {    public DataFieldAttribute(string fieldName,string fieldType)    {      this._fieldName = fieldName;      this._fieldType = fieldType;    }    // 數據庫字段名    private string _fieldName;    public string FieldName    {      get { return _fieldName; }      set { _fieldName = value; }    }    // 數據庫字段類型    private string _fieldType;    public string FieldType    {      get { return _fieldType; }      set { _fieldType = value; }    }  }  class Test_Attribute  {    [STAThread]    static void Main(string[] args)    {      MyStudent stu1 = new MyStudent();      stu1.Name = "劉德華";      stu1.Age = 40;      // 獲得所有屬性      PropertyInfo[] pros = stu1.GetType().GetProperties();      // 顯示所有屬性值      foreach (PropertyInfo pro in pros)       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));      //更改stu1對象的SAge值      stu1.GetType().GetProperty("Age").SetValue(stu1, 30, null);      // 顯示所有屬性值      foreach (PropertyInfo pro in pros)       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1, null));      //通過反射的方法來動態獲取此映射規則      PropertyInfo[] infos = stu1.GetType().GetProperties();      object[] objs_fieldAttr = null;      foreach (PropertyInfo info in infos)      {        // 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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定西市| 洞口县| 青浦区| 渝北区| 长岭县| 郎溪县| 项城市| 西城区| 绵竹市| 马山县| 舟曲县| 鄂州市| 府谷县| 三穗县| 敦煌市| 斗六市| 上栗县| 张家港市| 靖西县| 永顺县| 洞口县| 侯马市| 牟定县| 曲阳县| 柘城县| 夏河县| 左云县| 贞丰县| 武乡县| 兰西县| 江孜县| 莱州市| 保靖县| 大安市| 肥乡县| 和顺县| 奈曼旗| 通榆县| 扬中市| 靖安县| 桂平市|