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等数据。

自定义数据类:

using  System;
using  System.Collections;
using  System.Data;
using  System.Text;
using  NHibernate;
using  NHibernate.SqlTypes;

namespace  Index.Data.NHibernateHelper
{
    
/// <summary>
    
/// 自定义的NH数据类型,使用;分隔存储多个数据
    
/// </summary>

    public class DDLList : IUserType
    
{
        
private static readonly char SPLITTER = ';';

        
private static readonly SqlType[] TYPES = new SqlType[] {NHibernateUtil.String.SqlType};

        
#region IUserType 成员
        
        
/// <summary>
        
/// 提供自定义的完全复制方法
        
/// </summary>
        
/// <param name="value"></param>
        
/// <returns></returns>

        public object DeepCopy(object value)
        
{
            
if (value == null)
            
{
                
return null;
            }


            IList sourceList 
= (IList) value;
            IList targetList 
= new ArrayList();

            
for (int i = 0; i < sourceList.Count; i++)
            
{
                targetList.Add(sourceList[i]);
            }

            
return targetList;
        }


        
/// <summary>
        
/// 本类型实例是否可变
        
/// </summary>

        public bool IsMutable
        
{
            
get return false; }
        }


        
/// <summary>
        
/// 返回数据
        
/// </summary>
        
/// <param name="rs"></param>
        
/// <param name="names"></param>
        
/// <param name="owner"></param>
        
/// <returns></returns>

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

            
else
            
{
                
return null;
            }

        }


        
/// <summary>
        
/// 设置数据
        
/// </summary>
        
/// <param name="cmd"></param>
        
/// <param name="value"></param>
        
/// <param name="index"></param>

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

            
else
            
{
                NHibernateUtil.String.NullSafeSet(cmd, value, index);
            }

        }


        
/// <summary>
        
/// 返回的Sql
        
/// </summary>

        public Type ReturnedType
        
{
            
get return typeof (IList); }
        }


        
/// <summary>
        
/// 返回的Sql类型
        
/// </summary>

        public SqlType[] SqlTypes
        
{
            
get return TYPES; }
        }


        
public new bool Equals(object x, object y)
        
{
            
if (x == y)
            
{
                
return true;
            }


            
if (x != null && y != null)
            
{
                IList xList 
= (IList)x;
                IList yList 
= (IList)y;

                
if (xList.Count != yList.Count)
                
{
                    
return false;
                }


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

                }

                
return true;
            }


            
return false;
        }


        
#endregion


        
/// <summary>
        
/// 将string拼装成一个字符串,以“;”分隔
        
/// </summary>
        
/// <param name="list"></param>
        
/// <returns></returns>

        private string ListToString(IList list)
        
{
            
if (list.Count == 0)
            
{
                
return null;
            }

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

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


        
/// <summary>
        
/// 将“;”分隔的字符串解析为数组
        
/// </summary>
        
/// <param name="value"></param>
        
/// <returns></returns>

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

            
return emailList;
        }

    }

}

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

在实体类中直接使用IList映射即可。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值