Today Menu: NHibernate User Type

Ok, today I faced a really simple problem, I have a domain class with a property of type uri, that seems to be not natively supported by NHibernate. Whenever you face a problem of this type the solution is IUserType, here is the full file, it is so simple to write a user type that I really does not check with google, after I write my class I found that other people had already created it, but it is worth to take a look to some particular piece of code.

public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner) { Int32 index = rs.GetOrdinal(names[0]); if (rs.IsDBNull(index)) { return null; } try { return new Uri(rs[index].ToString()); } catch (FormatException) { //The uri is malformed, maybe it is worth to doing something else. return null; } }

The NullSafeGet is called to retrieve the value from database columns in a null safe way, first of all get the index of the data into resultset, then check if it is null, (if is null return null is ok) then if some data is contained into the resultset you can try to create the uri trapping the FormatException. If you prefer you can call Uri.TryCreate that probably is a better solution respect trapping the exception :D

public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index) { if (value == null || value == DBNull.Value) { NHibernateUtil.String.NullSafeSet(cmd, null, index); } Uri uri = (Uri)value; NHibernateUtil.String.Set(cmd, uri.AbsoluteUri, index); }

The NullSafeSet has the duty to transform data from the original .net type (Uri) to something that can be stored in database, since I need to work only with AbsoluteUri, I store into the database the AbsoluteUri value or the Uri. Then we need to tell nhibernate the types involved

public Type ReturnedType { get { return typeof(Uri); } } public NHibernate.SqlTypes.SqlType[] SqlTypes { get { return new SqlType[] { NHibernateUtil.String.SqlType }; } }

The ReturnedType tells nhibernate the .Net type handled by this UserType, the SqlTypes is used to tell nhibernate the structure of the data in the table, in this example a string is sufficient to store the AbsoluteUri and so I returned an array with only a string type.



Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

One thought on “Today Menu: NHibernate User Type”

Comments are closed.