Skip to content

Instantly share code, notes, and snippets.

@gshutler
Created April 2, 2010 21:31
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gshutler/353739 to your computer and use it in GitHub Desktop.
Demonstration of how to back a GUID with a string column with Fluent NHibernate
using FluentNHibernate.Mapping;
using Custom.Domain;
using Custom.Mappings.UserTypes;
namespace Custom.Mappings
{
public abstract class EntityMap<T> : ClassMap<T> where T : Entity
{
protected EntityMap()
{
Id(entity => entity.Id)
.GeneratedBy.Assigned().Unique()
.CustomType<StringBackedGuidUserType>();
}
}
}
using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
namespace Custom.Mappings.UserTypes
{
public class StringBackedGuidUserType : IUserType
{
public bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
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[] {SqlTypeFactory.GetString(36)}; }
}
public Type ReturnedType
{
get { return typeof (Guid); }
}
public bool IsMutable
{
get { return false; }
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
NHibernateUtil.String.NullSafeSet(cmd, value.ToString(), index);
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
return new Guid(NHibernateUtil.String.NullSafeGet(rs, names[0]).ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment