• 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>
  • 相关阅读:
    接口的应用(入门级)—— 插件式开发
    什么是接口(入门篇)——使你的程序功能丰富
    什么是接口(入门篇)
    【搞定Jvm面试】 面试官:谈谈 JVM 类文件结构的认识
    【搞定Jvm面试】 JDK监控和故障处理工具揭秘
    【搞定Jvm面试】 JVM 垃圾回收揭秘附常见面试题解析
    【搞定Jvm面试】 Java 内存区域揭秘附常见面试题解析
    【真实面试经历】我和阿里面试官的一次“邂逅”(附问题详解)
    【原创!推荐!】不了解布隆过滤器?一文给你整的明明白白!
    【搞定 Java 并发面试】面试最常问的 Java 并发进阶常见面试题总结!
  • 原文地址:https://www.cnblogs.com/xhan/p/1111834.html
Copyright © 2020-2023  润新知