这个我做的毕业设计中遇到的问题,在单选题表里存储多个选项内容!
下面是我的单选类和选项类
配置文件
下面是我的单选类和选项类
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>";
}
}
{
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; }
}
}
下面是用来从一个字段读写选项集合的自定义类型类{
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;
}
}
{
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>
<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>