NHibernate之旅(14):探索NHibernate中使用視圖

來源:互聯網
上載者:User

標籤:post   out   order   lin   size   hbm.xml   sans   view   編寫   

本節內容

  • 引入
  • 1.持久化類
  • 2.對應檔
  • 3.測試
  • 結語
引入

在資料庫操作中,我們除了對錶操作,還有視圖、預存程序等操作,這一篇和下篇來學習這些內容。這篇我們來學習怎樣在NHibernate中使用視圖。首先,我們在資料庫中建立一個名為viewCustomer視圖。選中CustomerId、Firstname、Lastname、OrderId、OrderDate項。

以下我們依次為這個視圖編寫持久化類和映射吧。

1.持久化類

同持久化資料庫中的表類似,我們須要對視圖持久化,定義視圖中的每一個屬性,由於視圖是僅僅讀的,所以在這裡我們僅僅要把屬性的Setter設定為private訪問許可權。

詳細做法例如以下:

在項目DomainModel層的Entities目錄中建立CustomerView.cs類,編寫代碼例如以下:

namespace DomainModel.Entities{    public class CustomerView    {        public virtual int CustomerId { get; private set; }        public virtual string Firstname { get; private set; }        public virtual string Lastname { get; private set; }        public virtual int OrderId { get; private set; }        public virtual DateTime OrderDate { get; private set; }    }}
2.對應檔

在項目DomainModel層的Mappings目錄中建立CustomerView.hbm.xml檔案。與映射資料庫表類似。編寫代碼例如以下:

<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"                    assembly="DomainModel" namespace="DomainModel">    <class name="DomainModel.Entities.CustomerView,DomainModel"          table="viewCustomer" mutable="false" >    <id name="CustomerId" column="CustomerId" type="Int32">      <generator class="native" />    </id>    <property name="Firstname" column="Firstname" type="string" />    <property name="Lastname" column="Lastname" type="string" />    <property name="OrderId" column="OrderId" type="Int32" />    <property name="OrderDate" column="OrderDate" type="DateTime" />  </class></hibernate-mapping>

好了,到這裡我們準備工作就做完了,即完畢了持久化和映射。

以下我們能夠使用視圖了。

3.測試

在資料訪問層(DAL)中編寫一個方法擷取訂單時間在orderDate之後的顧客列表,方法例如以下:

public IList<CustomerView> GetCustomerView(DateTime orderDate){    return _session.CreateCriteria(typeof(CustomerView))        .Add(Restrictions.Gt("OrderDate", orderDate))        .List<CustomerView>();}

在資料訪問測試層(DAL.Test)中編寫一個方法因為測試上面的方法。首先調用這種方法查詢出訂單時間在2008年10月1日之後的顧客列表。斷言其訂單時間是否大於2008年10月1日。

[Test]public void GetCustomerViewTest(){    DateTime testorderDate = new DateTime(2008, 10, 1);    IList<CustomerView> customers =        _relation.GetCustomerView(testorderDate);    foreach (CustomerView view in customers)    {        Assert.GreaterOrEqual(view.OrderDate, testorderDate);    }}

OK!

測試通過。NHibernate產生SQL語句例如以下:

SELECT this_.CustomerId as CustomerId0_0_,       this_.Firstname as Firstname0_0_,       this_.Lastname as Lastname0_0_,       this_.OrderId as OrderId0_0_,       this_.OrderDate as OrderDate0_0_FROM viewCustomer this_ WHERE this_.OrderDate > @p0; @p0 = ‘2008/10/1 0:00:00‘

好了。到此我們學會了在NHibernate中怎樣使用視圖,是不是非常easy啊。

結語

通過這篇文章的展示,我們學習了在NHibernate中怎樣使用視圖,同表類似,僅僅是屬性訪問許可權不同罷了,假設你原來不知道怎樣使用視圖,網上到如今也沒有相關資料認為無從下手,通過這篇文章的高速閱讀,是不是使用視圖非常easy,豁然開朗的樣子(視圖原來這樣啊,沒有什麼神奇之處~~)。

下篇我們來看看NHibernate中使用儲存過程,用過預存程序的朋友都知道。真是煩人,這個預存程序我真是弄了非常長時間。在實際運用中錯誤不斷,我把它一一化解,請徵集意見,大家說下篇是寫一一化解的整個具體過程(涉及錯誤資訊。怎樣改動,2篇樣子)還是直接講正確方案(1篇搞定)。由你做主!

NHibernate之旅(14):探索NHibernate中使用視圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.