[NHibernate]Nullables

來源:互聯網
上載者:User

標籤:style   blog   class   code   c   java   

系列文章

[Nhibernate]體繫結構

[NHibernate]ISessionFactory配置

[NHibernate]持久化類(Persistent Classes)

[NHibernate]O/R Mapping基礎

[NHibernate]集合類(Collections)映射 

[NHibernate]關聯映射

[NHibernate]Parent/Child

[NHibernate]緩衝(NHibernate.Caches)

[NHibernate]NHibernate.Tool.hbm2net

什麼是Nullables?

Nullables是NHibrnate的附加軟體,它是Donald L Mull Jr.(aka luggage)貢獻的,大部分資料庫系統允許基本類型(int或bool)為null。這意味著一個boolean列可能有0,1或者是null值,null和0有不同的含義。但是在.NET 1.x這是不能實現的;一個bool不是true就是false。

Nullables使得在NHibernae中nullable的基本類型成為可能。注意,.NET2.0已經有了這個特性。

如何使用?

這是一個簡單的例子,它使用了Nullables.NullableDateTime來(可選擇)儲存一個人的生日。

 1 public class Person 2 { 3     int _id; 4     string _name; 5     Nullables.NullableDateTime _dateOfBirth; 6     public Person() 7     { 8     } 9     public int Id10     {11         get { return this._id; }12     }13     public string Name14     {15         get { return this._name; }16         set { this._name = value; }17     }18     public Nullables.NullableDateTime DateOfBirth19     {20         get { return this._dateOfBirth; }21         set { this._dateOfBirth = value; }22     }23 }

如你所見,DateOfBirth是Nullables.NullableDateTime類型(而不是System.DateTime)。這裡是映射

 1 <?xml version="1.0" encoding="utf-8" ?>  2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"> 3     <class name="Example.Person, Example" table="Person"> 4         <id name="Id" access="field.camelcase-underscore" unsaved-value="0"> 5             <generator class="native" /> 6         </id> 7         <property name="Name" type="String" length="200" /> 8         <property name="DateOfBirth" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" /> 9     </class>10 </hibernate-mapping>

重點

在這個映射中,DateOfBirth的類型必須是Nullables.NHibernate.NullableDateTimeType。注意NHibernate.Mapping.Attributes會自動處理它。
Nullables.NHibernate.NullableXXXType是用來轉換Nullables 類型到資料庫的封裝類。

這裡是這個例子的部分代碼:

1 Person per = new Person();2 textBox1.Text = per.DateOfBirth.Value.ToString() // will throw an exception when there is no value.3 textBox1.Text = per.DateOfBirth.ToString() // will work. it will return an empty string if there is no value.4 textBox1.Text = (per.DateOfBirth.HasValue ? per.DateOfBirth.Value.ToShortDateString() : "Unknown") // friendly message5 per.DateOfBirth = new System.DateTime(1979, 11, 8); // implicit cast from the "plain" System.DateTime.6 per.DateOfBirth = new NullableDateTime(new System.DateTime(1979, 11, 8)); // the long way.7 per.DateOfBirth = null; // this works.8 per.DateOfBirth = NullableDateTime.Default; // this is more correct.

本文來自《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.