有时候在NHibernate中的基本数据类型可能不够好用,可以考虑自定义一个数据类型。
可以通过实现IUserType或者ICompositeUserType接口来实现这一功能。 ICompositeUserType较IUserType而言可以提供更多的控制。一般情况我们实现IUserType即可。
IUserType Members
IsMutable |
Are objects of this type mutable? |
ReturnedType |
The type returned by NullSafeGet() |
SqlTypes |
The SQL types for the columns mapped by this type. |
DeepCopy |
Return a deep copy of the persistent state, stopping at entities and at collections. |
Equals |
Compare two instances of the class mapped by this type for persistent "equality" ie. equality of persistent state |
NullSafeGet |
Retrieve an instance of the mapped class from a JDBC resultset. Implementors should handle possibility of null values. |
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等数据。
自定义数据类:
在映射文件中如此设置
在实体类中直接使用IList映射即可。