NHibernate实现自定义类型IUserType

这个我做的毕业设计中遇到的问题,在单选题表里存储多个选项内容!

下面是我的单选类和选项类
     public   class  Choice
    
{
        
private int index;

        
public virtual int Index
        
{
            
get return index; }
            
set { index = value; }
        }

        
private string value;

        
public virtual string Value
        
{
            
get return this.value; }
            
set this.value = value; }
        }

        
public string GetXML()
        
{
            
return "<Choice index=\""+Index+"\"><![CDATA["+Value+"]]></Choice>";
        }

    }

     public   class  DanXuan
    
{
        
private long id;

        
public long ID
        
{
            
get return id; }
            
set { id = value; }
        }

        
private string content;

        
public virtual string Content
        
{
            
get return content; }
            
set { content = value; }
        }

        
private IList<Choice> choices=new List<Choice>();

        
public virtual IList<Choice> Choices
        
{
            
get return choices; }
            
set { choices = value; }
        }

    }
下面是用来从一个字段读写选项集合的自定义类型类
     public   class  ChoiceList : IUserType
    
{
 
        
private static SqlType[] TYPES = new SqlType[] new SqlType(DbType.String) };

        
public bool IsMutable
        
{
            
get return false; }
        }

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


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

        }


        
public object DeepCopy(object value)
        
{
            IList
<Choice> sourceList = (IList<Choice>)value;
            IList
<Choice> targetList = new List<Choice>();
            
foreach (Choice choice in sourceList)
                targetList.Add(choice);
            
return targetList;
        }


        
public new bool Equals(object x, object y)
        
{
            
if (x == y) return true;
            
if (x != null && y != null)
            
{
                IList
<Choice> listX = (IList<Choice>)x;
                IList
<Choice> listY = (IList<Choice>)y;
                
if (listX.Count != listY.Count) return false;
                
for (int i = 0; i < listX.Count; i++)
                
{
                    
if (listX[i] != listY[i]) return false;
                }

                
return true;
            }

            
return true;
        }


        
public int GetHashCode(object x)
        
{
            
return x.GetHashCode();
        }


        
public object Assemble(object cached, object owner)
        
{
            
return DeepCopy(cached);
        }


        
public object Disassemble(object value)
        
{
            
return DeepCopy(value);
        }


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


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

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


        
public object Replace(object original, object target, object owner)
        
{
            
return original;
        }


        
private IList<Choice> ParseChoices(string value)
        
{
            XmlTextReader tr 
= new XmlTextReader(value, XmlNodeType.Element, null);
            IList
<Choice> choiceList = new List<Choice>();
            
while (tr.Read())
            
{
                
if (tr.NodeType == XmlNodeType.Element)
                
{
                    Choice item 
= new Choice();
                    item.Index 
= int.Parse(tr.GetAttribute(0));

                    tr.Read();
            
                    item.Value 
= tr.Value;

                    choiceList.Add(item);
                }

            }



            
return choiceList;
        }

        
private string AssembleChoices(IList<Choice> choices)
        
{
            
string str="";
            
foreach (Choice choice in choices)
                str 
+= choice.GetXML();
            
return str;
        }


    }

配置文件
<? xml version="1.0" encoding="utf-8"  ?>
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2"  assembly ="Test"  namespace ="Test" >
  
< class  name ="DanXuan"  table ="DanXuan"  lazy ="false" >

    
< id  name ="ID"  column ="ID" >
      
< generator  class ="identity"   />
    
</ id >

    
< property  name ="Content"  column ="QContent" />
    
< property  name ="Choices"  column ="Choices"  type ="Test.ChoiceList,Test" />

  
</ class >
</ hibernate-mapping >

转载于:https://www.cnblogs.com/xhan/archive/2008/03/18/1111834.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值