NHibernate自定义数据类型

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

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

IUserType Members

Public Instance Properties
IsMutableAre objects of this type mutable?
ReturnedTypeThe type returned by NullSafeGet()
SqlTypesThe SQL types for the columns mapped by this type.
Public Instance Methods
DeepCopyReturn a deep copy of the persistent state, stopping at entities and at collections.
EqualsCompare two instances of the class mapped by this type for persistent "equality" ie. equality of persistent state
NullSafeGetRetrieve an instance of the mapped class from a JDBC resultset. Implementors should handle possibility of null values.
NullSafeSetWrite 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等数据。

自定义数据类:

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Data;
None.gif
using  System.Text;
None.gif
using  NHibernate;
None.gif
using  NHibernate.SqlTypes;
None.gif
None.gif
namespace  Index.Data.NHibernateHelper
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 自定义的NH数据类型,使用;分隔存储多个数据
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DDLList : IUserType
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static readonly char SPLITTER = ';';
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private static readonly SqlType[] TYPES = new SqlType[] dot.gif{NHibernateUtil.String.SqlType};
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IUserType 成员#region IUserType 成员
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 提供自定义的完全复制方法
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="value"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public object DeepCopy(object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (value == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IList sourceList 
= (IList) value;
InBlock.gif            IList targetList 
= new ArrayList();
InBlock.gif
InBlock.gif            
for (int i = 0; i < sourceList.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                targetList.Add(sourceList[i]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return targetList;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 本类型实例是否可变
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsMutable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn false; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="rs"></param>
InBlock.gif        
/// <param name="names"></param>
InBlock.gif        
/// <param name="owner"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public object NullSafeGet(IDataReader rs, string[] names, object owner)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string value = (string) NHibernateUtil.String.NullSafeGet(rs, names[0]);
InBlock.gif            
if (value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return StringToList(value);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设置数据
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmd"></param>
InBlock.gif        
/// <param name="value"></param>
ExpandedSubBlockEnd.gif        
/// <param name="index"></param>

InBlock.gif        public void NullSafeSet(IDbCommand cmd, object value, int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string str = ListToString((IList) value);
InBlock.gif                NHibernateUtil.String.NullSafeSet(cmd, str, index);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NHibernateUtil.String.NullSafeSet(cmd, value, index);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回的Sql
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Type ReturnedType
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn typeof (IList); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回的Sql类型
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public SqlType[] SqlTypes
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn TYPES; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public new bool Equals(object x, object y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (x == y)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (x != null && y != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IList xList 
= (IList)x;
InBlock.gif                IList yList 
= (IList)y;
InBlock.gif
InBlock.gif                
if (xList.Count != yList.Count)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
for (int i = 0; i < xList.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string str1 = (string)xList[i];
InBlock.gif                    
string str2 = (string)yList[i];
InBlock.gif                    
if (str1 != str2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return false;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将string拼装成一个字符串,以“;”分隔
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="list"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string ListToString(IList list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (list.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            StringBuilder sb 
= new StringBuilder();
InBlock.gif            
for (int i = 0; i < list.Count - 1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(list[i]).Append(SPLITTER);
ExpandedSubBlockEnd.gif            }

InBlock.gif            sb.Append(list[list.Count 
- 1]);
InBlock.gif            
return sb.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将“;”分隔的字符串解析为数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="value"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private IList StringToList(string value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] strs = value.Split(SPLITTER);
InBlock.gif            IList emailList 
= new ArrayList();
InBlock.gif            
for (int i = 0; i < strs.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                emailList.Add(strs[i]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return emailList;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

在映射文件中如此设置
None.gif      < property  name ="Email1"  type ="Index.Data.NHibernateHelper.DDLList,Index.Data.NHibernateHelper"  column ="Email1"   />
None.gif

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



转载于:https://www.cnblogs.com/renrenqq/archive/2006/07/24/458575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值