Demonstration of how to back a GUID with a string column with Fluent NHibernate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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