有时候在NHibernate中的基本数据类型可能不够好用,可以考虑自定义一个数据类型。

可以通过实现IUserType或者ICompositeUserType接口来实现这一功能。 ICompositeUserType较IUserType而言可以提供更多的控制。一般情况我们实现IUserType即可。

IUserType Members

Public Instance Properties

NHibernate自定义数据类型IsMutable Are objects of this type mutable?
NHibernate自定义数据类型ReturnedType The type returned by NullSafeGet()
NHibernate自定义数据类型SqlTypes The SQL types for the columns mapped by this type.

Public Instance Methods

NHibernate自定义数据类型DeepCopy Return a deep copy of the persistent state, stopping at entities and at collections.
NHibernate自定义数据类型Equals Compare two instances of the class mapped by this type for persistent "equality" ie. equality of persistent state
NHibernate自定义数据类型NullSafeGet Retrieve an instance of the mapped class from a JDBC resultset. Implementors should handle possibility of null values.
NHibernate自定义数据类型NullSafeSet Write an instance of the mapped class to a prepared statement. Implementors should handle possibility of null values. A multi-column type should be written to parameters starting from index.

 比如:我想实现在数据库里存储格式为“数据;数据;数据”,而在类中体现为IList。这样我们可以减少一些一对多的映射。我们可以用它来存放EMail等数据。

自定义数据类:

NHibernate自定义数据类型using System;
NHibernate自定义数据类型
using System.Collections;
NHibernate自定义数据类型
using System.Data;
NHibernate自定义数据类型
using System.Text;
NHibernate自定义数据类型
using NHibernate;
NHibernate自定义数据类型
using NHibernate.SqlTypes;
NHibernate自定义数据类型
NHibernate自定义数据类型
namespace Index.Data.NHibernateHelper
}

在映射文件中如此设置
NHibernate自定义数据类型    <property name="Email1" type="Index.Data.NHibernateHelper.DDLList,Index.Data.NHibernateHelper" column="Email1" />
NHibernate自定义数据类型

在实体类中直接使用IList映射即可。



相关文章: