手把手教你寫ORM(六)

來源:互聯網
上載者:User

最近越來越不知道該吃什麼了。唉。

現在到了比較激動的地方了,ORM,說白了最主要的工作就是將對象和資料庫表格的映射建立起來。
這裡我們就不用XML檔案來配置了,第一會使設定檔結構變複雜加大解析難度,第二我來看看另外一種做映射的方法。

這裡介紹一下.NET的Attribute,在早些時候被翻譯作屬性,和property混淆了。現在一般都翻譯為特性或特徵,它可以給一個類或者類的成員附加一些額外的特徵或者功能。在.NET裡面的System.Arribute作為其基類,所有繼承自他的,並且類名以Attribute結尾的類型都可以作為Attribute實用,這裡有一個約束,定義的時侯類名要以Attribute結尾,但是使用的時候要去掉Attibute。.NET類庫預定義了很多的特性來實現很多的功能,這裡我們通過Attribute類來標示類成員的特性,並且通過反射來擷取來標示類成員與資料庫的映射。

首先我們通過一個例子來看看Attribute的特性

執行結果自然是一個異常,異常的message是error:can not run this method!

ok,現在是否明白了它的工作原理?還有點暈?暈不要緊,做了再說。

class TestAttribute:System.Attribute
{
    public TestAttribute(string message)
    {
        throw new Exception("error:"+message);
    }
}

class Tester
{
    [Test("Can not run this method!")]
    public void Cannotrun()
    {
    }

    public static Main(sting[] args)
    {
        Tester t=new Tester();
        t.Cannotrun();
    }
}

我們現在就是來構造一個Attribute的類來儲存一個屬性的類型,長度,映射欄位等資料

 

namespace Alexander.Xbase.Interface
{
    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
    public class ParamAttribute:Attribute
    {
        private string _parameterType;

        public string ParameterType
        {
            get { return _parameterType; }
            set { _parameterType = value; }
        }
        private int _parameterLength;

        public int ParameterLength
        {
            get { return _parameterLength; }
            set { _parameterLength = value; }
        }
        private string _srccolumn;

        public string Srccolumn
        {
            get { return _srccolumn; }
            set { _srccolumn = value; }
        }

        public ParamAttribute(string ptype, int len)
        {
            _parameterType = ptype;
            _parameterLength = len;
            //throw new Exception("can not use");
        }
        public ParamAttribute(string ptype, int len,string src)
        {
            _parameterType = ptype;
            _parameterLength = len;
            _srccolumn = src;
            //throw new Exception("can not use");
        }

    }
}

使用的時候
定義一個實體類:class tb
    {
        private string _aaa;

        [Param("NChar",10)]
        public string aaa
        {
            get { return _aaa; }
            set { _aaa = value; }
        }
        private string _bbb;

        [Param("NChar", 10)]
        public string bbb
        {
            get { return _bbb; }
            set { _bbb = value; }
        }
    }

這樣子就把映射的類型,長度都儲存到特徵裡面。

To be continue  太累了,今晚休息了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.