In Hibernate they've got a plugin mechanism where you can specify the persister for a specific property to convert value->db and db->value. I haven't found the same thing in NHibernate, so I made an ugly hack in my domain object. The SerializedValue property is only used by NHibernate to map to a binary(x) column:
Code:
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
public object Value
{
get { return value; }
set { this.value = value; }
}
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
private byte[] SerializedValue
{
get
{
if (Value == null)
return null;
BinaryFormatter frmt = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
frmt.Serialize(ms, Value);
ms.Close();
return ms.GetBuffer();
}
}
set
{
if (value == null)
Value = null;
BinaryFormatter frmt = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(value))
{
Value = frmt.Deserialize(ms);
}
}
}