Hi!
I would like to create a custom NHibernate type called Rectangle reflecting 4 int columns in a table. I read DoubleStringType.cs and followed it's implementation. According to Hibernate's cut example, I have two files called Rectangle.cs and RectangleType.cs. Is this correct for NHibernate as well? I'm currently using a types.hbm.xml file for connecting the Rectangle class and the persister class RectangleType though this is not mentioned in the NHibernate docs.
Could you tell me how to get the custom type working?
The files are:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<typedef name="Rectangle" class="MyApp.Persistence.RectangleType, MyApp.Persistence"/>
</hibernate-mapping>
My implementation looks as follows (RectangleType.cs):
Code:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
namespace MyApp
{
namespace Persistence
{
public class RectangleType : NHibernate.ICompositeUserType
{
public System.Type ReturnedClass
{
get { return typeof(int[]); }
}
public bool IsMutable
{
get { return true; }
}
public String[] PropertyNames
{
get
{
return new String[] { "x", "y", "w", "h" };
}
}
public NHibernate.Type.IType[] PropertyTypes
{
get
{
return new NHibernate.Type.IType[] { NHibernateUtil.Int32, NHibernateUtil.Int32,
NHibernateUtil.Int32, NHibernateUtil.Int32 };
}
}
public object Assemble(object cached, NHibernate.Engine.ISessionImplementor session, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(Object value, NHibernate.Engine.ISessionImplementor session)
{
return DeepCopy(value);
}
public new bool Equals(object a, object b)
{
if (a==b) return true;
if (a==null || b==null) return false;
int[] lhs = (int[])a;
int[] rhs = (int[])b;
return lhs[0].Equals(rhs[0]) && lhs[1].Equals(rhs[1]) && lhs[2].Equals(rhs[2]) && lhs[3].Equals(rhs[3]);
}
public Object DeepCopy(Object a)
{
if (a==null) return null;
int[] result = new int[4];
int[] input = (int[])a;
result[0] = input[0];
result[1] = input[1];
result[2] = input[2];
result[3] = input[3];
return result;
}
public Object NullSafeGet(System.Data.IDataReader rs, String[] names, NHibernate.Engine.ISessionImplementor session, Object owner)
{
int x = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[0], session, owner);
int y = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[1], session, owner);
int w = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[2], session, owner);
int h = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[3], session, owner);
return (x==null && y==null && w==null && h==null) ? null : new Rectangle(x, y, w, h);
}
public void NullSafeSet(System.Data.IDbCommand st, Object value, int index, NHibernate.Engine.ISessionImplementor session)
{
Rectangle rect = (value==null) ? new Rectangle() : (Rectangle)value;
NHibernateUtil.Int32.NullSafeSet(st, rect.x, index, session);
NHibernateUtil.Int32.NullSafeSet(st, rect.y, index+1, session);
NHibernateUtil.Int32.NullSafeSet(st, rect.w, index+2, session);
NHibernateUtil.Int32.NullSafeSet(st, rect.h, index+3, session);
}
public Object GetPropertyValue(Object component, int property)
{
Rectangle rect = (Rectangle)component;
switch (property)
{
case 0:
{
return rect.x;
break;
}
case 1:
{
return rect.y;
break;
}
case 2:
{
return rect.w;
break;
}
case 3:
{
return rect.h;
break;
}
}
return null;
}
public void SetPropertyValue(Object component, int property, Object value)
{
Rectangle rect = (Rectangle)component;
switch (property)
{
case 0:
{
rect.x = System.Convert.ToInt32(value);
break;
}
case 1:
{
rect.y = System.Convert.ToInt32(value);
break;
}
case 2:
{
rect.w = System.Convert.ToInt32(value);
break;
}
case 3:
{
rect.h = System.Convert.ToInt32(value);
break;
}
}
}
}
}
}