[NHibernate] Nullables

Source: Internet
Author: User

Series Articles

[Nhibernate] Architecture

[NHibernate] Isessionfactory Configuration

[NHibernate] Persistence class (persistent Classes)

[NHibernate] O/R Mapping Basics

[NHibernate] Collection Class (collections) mappings

[NHibernate] Association mappings

[NHibernate] Parent/child

[NHibernate] Cache (nhibernate.caches)

[NHibernate] NHibernate.Tool.hbm2net

What is Nullables?

Nullables is an add-on software for Nhibrnate, which is a contribution of Donald L Mull Jr (aka luggage), and most database systems allow the base type (int or bool) to be null. This means that a boolean column may have 0, 1, or null values, and null and 0 have different meanings. But in. NET 1.x this is not possible; a bool is either TRUE or false.

Nullables makes possible the basic types of nullable in Nhibernae. Attention. NET2.0 already has this feature.

How to use?

This is a simple example, it uses Nullables.nullabledatetime to (optionally) save a person's birthday.

1  Public class Person2 {3     int_id;4     string_name;5 nullables.nullabledatetime _dateofbirth;6      PublicPerson ()7     {8     }9      Public intIdTen     { One         Get{return  This. _id;} A     } -      Public stringName -     { the         Get{return  This. _name;} -         Set{ This. _name =value;} -     } -      Publicnullables.nullabledatetime dateOfBirth +     { -         Get{return  This. _dateofbirth;} +         Set{ This. _dateofbirth =value;} A     } at}

As you can see, dateOfBirth is the Nullables.nullabledatetime type (not System.DateTime). Here is the map

1 <?XML version= "1.0" encoding= "Utf-8"?> 2 <hibernate-mappingxmlns= "urn:nhibernate-mapping-2.0">3     <classname= "Example.person, Example"Table= "Person">4         <IDname= "Id"Access= "Field.camelcase-underscore"Unsaved-value= "0">5             <Generatorclass= "Native" />6         </ID>7         < Propertyname= "Name"type= "String"length= "$" />8         < Propertyname= "dateOfBirth"type= "Nullables.NHibernate.NullableDateTimeType, nullables.nhibernate" />9     </class>Ten </hibernate-mapping>

Focus

In this mapping, the type of dateOfBirth must be Nullables.NHibernate.NullableDateTimeType. Note that NHibernate.Mapping.Attributes will handle it automatically.
Nullables.NHibernate.NullableXXXType is the wrapper class used to convert the Nullables type to the database.

Here is a partial code for this example:

1Person per =NewPerson ();2TextBox1.Text = per. DateOfBirth.Value.ToString ()//Would throw a exception when there is no value.3TextBox1.Text = per. Dateofbirth.tostring ()//would work . It'll return an empty string if there is no value.4TextBox1.Text = (per. Dateofbirth.hasvalue? Per. DateOfBirth.Value.ToShortDateString ():"Unknown")//Friendly Message5Per. dateOfBirth =NewSystem.DateTime (1979, One,8);//implicit cast from the "plain" System.DateTime.6Per. dateOfBirth =NewNullabledatetime (NewSystem.DateTime (1979, One,8));//The long.7Per. dateOfBirth =NULL;//This works.8Per. dateOfBirth = Nullabledatetime.default;//This is more correct.

This article is from "NHibernate Chinese document"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.