Chapter 2 Nullables
Directory
-
How to use it?
What is Nullables?
Nullables is an additional software for nhib.pdf. It is Donald L Mull Jr. (akaLuggage) Contribution.Most database systems allow basic types (suchintOrbool) Is null. This means that a boolean column may have 0, 1 or null values. null and 0 have different meanings. However, this cannot be implemented in. NET 1. x. If a bool is neither true nor false.
Nullables makes it possible to use the basic nullable type in NHibernate. Note that. NET 2.0 already has this feature.
How to use it?
Here is a simple example, which usesNullables.NullableDateTimeTo (optional) save a person (Person.
public class Person{ int _id; string _name; Nullables.NullableDateTime _dateOfBirth; public Person() { } public int Id { get { return this._id; } } public string Name { get { return this._name; } set { this._name = value; } } public Nullables.NullableDateTime DateOfBirth { get { return this._dateOfBirth; } set { this._dateOfBirth = value; } }}
As you can see, DateOfBirth isNullables.NullableDateTimeType (insteadSystem.DateTime).
Here is the ing
<?xml version="1.0" encoding="utf-8" ?> Important
In this ing, the DateOfBirth typeRequiredYesNullables.NHibernate.NullableDateTimeType. Note that NHibernate. Mapping. Attributes will automatically process it.
Nullables.NHibernate.NullableXXXTypeIs the packaging class used to convert the Nullables type to the database.
Here is part of the code for this example:
Person per = new Person();textBox1.Text = per.DateOfBirth.Value.ToString() // will throw an exception when there is no value.textBox1.Text = per.DateOfBirth.ToString() // will work. it will return an empty string if there is no value.textBox1.Text = (per.DateOfBirth.HasValue ? per.DateOfBirth.Value.ToShortDateString() : "Unknown") // friendly messageper.DateOfBirth = new System.DateTime(1979, 11, 8); // implicit cast from the "plain" System.DateTime.per.DateOfBirth = new NullableDateTime(new System.DateTime(1979, 11, 8)); // the long way.per.DateOfBirth = null; // this works.per.DateOfBirth = NullableDateTime.Default; // this is more correct.