1.2.0ga
Hi
I'm having a problem getting my mapping document to compile without error. I can't seem to figure out the proper synthax for the type attribute of the property element. I would really appreciate it if someone could help me with the following problem.
The following compiles correctly.
<property name="Layout" column="layout" type="Econcordia.Quiz.Types.XmlSerializableType`1[[Econcordia.Quiz.Layout, Econcordia.Quiz]], Econcordia.Quiz" not-null="true" />
But the following does not compile and this is what I would need help with. If someone could help me with the synthax I would really appreciate it.
<property name="Answers" column="answers" type="Econcordia.Quiz.Types.XmlSerializableType`1[[System.Collections.Generic.IList`1[[Econcordia.Quiz.MultipleChoiceAnswer, Econcordia.Quiz]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Econcordia.Quiz" not-null="true" />
Here's the class that implement the IUserType interface for reference
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
using System.Data;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace Econcordia.Quiz.Types
{
public class XmlSerializableType<T> : IUserType
where T : class
{
public new bool Equals(object x, object y)
{
return object.Equals(x, y);
}
public int GetHashCode(Object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
string data = NHibernateUtil.String.NullSafeGet(rs, names).ToString();
return this.Deserialize(data);
}
public void NullSafeSet(IDbCommand statement, object value, int index)
{
if (value == null)
{
((IDbDataParameter)statement.Parameters[index]).Value = "";
}
else
{
T t = value as T;
NHibernateUtil.String.Set(statement, this.Serialize(t), index);
}
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public SqlType[] SqlTypes
{
get { return new SqlType[] { NHibernateUtil.String.SqlType }; }
}
public System.Type ReturnedType
{
//get { return typeof(String); }
get { return typeof(T); }
}
public bool IsMutable
{
get { return NHibernateUtil.String.IsMutable; }
}
private string Serialize(T t)
{
UTF8Encoding encoding = new UTF8Encoding();
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(writer, t);
return encoding.GetString(stream.ToArray());
}
private T Deserialize(string data)
{
UTF8Encoding encoding = new UTF8Encoding();
XmlSerializer xs = new XmlSerializer(typeof(T));
byte[] array = encoding.GetBytes(data);
MemoryStream stream = new MemoryStream(array);
return xs.Deserialize(stream) as T;
}
}
}
|