Mapping an IUserType to a component property in fluent NHibernate - nhibernate

I'm trying to implement an IUserType for states and country codes that will allow me to access both the two-letter code (what's stored in the database) as well as the full name. I'm following the example in the NHibernate 3.0 Cookbook (p. 225), but my problem is that my StreetAddress class is currently mapped as a component in my automapping configuration:
public override bool IsComponent(Type type)
{
return type == typeof(StreetAddress);
}
With this class identified as a component, I don't know how I can use an IUserType for the component class's property, since that class isn't explicitly mapped. There's nowhere that I could tell fluent NHibernate to use the IUserType specification.

#Firo was close, but there turned out to be a much easier solution. There were two steps here. First, I had to tell Fluent NHibernate not to map the State and Country classes, which reside in my domain layer:
public override bool ShouldMap(Type type)
{
return type.Name != "State" && type.Name != "Country";
}
Next, I simply had to create the conventions for the IUserType classes. This turned out to be easier than #Firo suggested:
public class CountryUserTypeConvention : UserTypeConvention<CountryType>
{
}
public class StateUserTypeConvention : UserTypeConvention<StateType>
{
}
The definition of those IUserTypes was pulled out of the cookbook referenced in the original question, but in case you don't want to read it:
public class CountryType : GenericWellKnownInstanceType<Country, string>
{
// The StateType is pretty much the same thing, only it uses "StateCode" instead of "CountryCode"
private static readonly SqlType[] sqlTypes =
new[] {SqlTypeFactory.GetString(2)};
public CountryType()
: base(new Countries(),
(entity, id) => entity.CountryCode == id,
entity => entity.CountryCode)
{
}
public override SqlType[] SqlTypes
{
get { return sqlTypes; }
}
}
And that derives from GenericWellKnownInstanceType:
[Serializable]
public abstract class GenericWellKnownInstanceType<T, TId> :
IUserType where T : class
{
private Func<T, TId, bool> findPredicate;
private Func<T, TId> idGetter;
private IEnumerable<T> repository;
protected GenericWellKnownInstanceType(
IEnumerable<T> repository,
Func<T, TId, bool> findPredicate,
Func<T, TId> idGetter)
{
this.repository = repository;
this.findPredicate = findPredicate;
this.idGetter = idGetter;
}
public Type ReturnedType
{
get { return typeof (T); }
}
public bool IsMutable
{
get { return false; }
}
public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (ReferenceEquals(null, x) ||
ReferenceEquals(null, y))
{
return false;
}
return x.Equals(y);
}
public int GetHashCode(object x)
{
return (x == null) ? 0 : x.GetHashCode();
}
public object NullSafeGet(IDataReader rs,
string[] names, object owner)
{
int index0 = rs.GetOrdinal(names[0]);
if (rs.IsDBNull(index0))
{
return null;
}
var value = (TId) rs.GetValue(index0);
return repository.FirstOrDefault(x =>
findPredicate(x, value));
}
public void NullSafeSet(IDbCommand cmd,
object value, int index)
{
if (value == null)
{
((IDbDataParameter) cmd.Parameters[index])
.Value = DBNull.Value;
}
else
{
((IDbDataParameter) cmd.Parameters[index])
.Value = idGetter((T) value);
}
}
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;
}
/// <summary>
/// The SQL types for the columns
/// mapped by this type.
/// </summary>
public abstract SqlType[] SqlTypes { get; }
}
The repositories for these classes are just a pair of ReadOnlyCollection of the State and Country objects. Again, from the cookbook:
public class States : ReadOnlyCollection<State>
{
// Truncated in the interest of brevity
public static State Arizona = new State("AZ", "Arizona");
public static State Florida = new State("FL", "Florida");
public static State California = new State("CA", "California");
public static State Colorado = new State("CO", "Colorado");
public static State Oklahoma = new State("OK", "Oklahoma");
public static State NewMexico = new State("NM", "New Mexico");
public static State Nevada = new State("NV", "Nevada");
public static State Texas = new State("TX", "Texas");
public static State Utah = new State("UT", "Utah");
public States() : base(new State[]
{
Arizona, Florida, California, Colorado,
Oklahoma, NewMexico, Nevada, Texas, Utah
}
)
{
}
}
Hopefully this helps someone out there.

i couldnt test it, but it should be possible using a convention
public class ComponentConvention : IComponentConvention, IComponentConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IComponentInspector> criteria)
{
criteria.Expect(x => x.Type == typeof(StreetAddress);
}
public void Apply(IComponentInstance instance)
{
instance.Properties.First(p => p.Name == "CountrCode").CustomType<MyUserType>();
}
}

Related

Automapping a Composite Model with Composite Iteration with FluentNhibernate

I have a tree structured model and designed it with composite Pattern. for iterating through the entire hierachy Im using Composite Iteration.
I have used this tutorial:
http://www.blackwasp.co.uk/Composite.aspx
but when I want to AutoMap the model, I encounter this problem:
{"The entity '<GetEnumerator>d__0' doesn't have an Id mapped. Use the
Id method to map your identity property. For example: Id(x => x.Id)."}
but getEnumerator is a method. I don't know why handle this like an Entity!!
public IEnumerator<MenuComponent> GetEnumerator()
{
foreach (MenuComponent child in menuComponents)
yield return this;
}
here is my AutoMapping Configuration :
public class AutomappingConfiguration: DefaultAutomappingConfiguration
{
//As we do not explicitly map entities or value objects, we define conventions or exceptions
//for the AutoMapper. We do this by implementing a configuration class.
//this method instructs the AutoMapper to consider only those classes for mapping
//which reside in the same namespace as the Employeeentity.
public override bool ShouldMap(Type type)
{
return type.Namespace == typeof(Menu).Namespace;
}
}
Uploaded the sample code:
public abstract class CombatElement
{
public virtual string Name { get; set; }
public virtual Guid Id { get; set; }
public virtual void Add(
CombatElement element)
{
throw new NotImplementedException();
}
public virtual void
Remove(CombatElement element)
{
throw new NotImplementedException();
}
public virtual
IEnumerable<CombatElement>
GetElements()
{
throw new NotImplementedException();
}
public abstract void Fight();
public abstract void Move();
}
//////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
namespace FluentNHibernateMvc3.Models
{
public class Formation : CombatElement
{
private List<CombatElement> _elements;
public virtual IEnumerable<CombatElement> Elements { get { return _elements; } }
public Formation()
{
_elements = new List<CombatElement>();
}
public override void Add(
CombatElement element)
{
_elements.Add(element);
}
public override void
Remove(CombatElement element)
{
_elements.Remove(element);
}
public override void Fight()
{
Debug.WriteLine(this.Name + " Formation is fighting");
}
public override void Move()
{
Debug.WriteLine(this.Name + " Formation is moving");
}
public override
IEnumerable<CombatElement>
GetElements()
{
// yield up this current object first
yield return this;
// iterate through all child elements
foreach (CombatElement fe in
_elements)
{
// + iterate through each of its elements
foreach (CombatElement feInner
in fe.GetElements())
yield return feInner;
}
}
}
}
/////////
public class Soldier : CombatElement
{
public virtual int Rank { get; set; }
public override void Fight()
{
Debug.WriteLine(this.Name + " soldier is fighting");
}
public override void Move()
{
Debug.WriteLine(this.Name + " soldier is fighting");
}
public override
IEnumerable<CombatElement>
GetElements()
{
yield return this;
}
}
and here how I create session factory
// Returns our session factory
private static ISessionFactory CreateSessionFactory()
{
//m => m.FluentMappings.AddFromAssemblyOf<FormationMap>()
return Fluently.Configure()
.Database( CreateDbConfig )
.Mappings(m => m.AutoMappings.Add(CreateMappings()))
.ExposeConfiguration( UpdateSchema )
.CurrentSessionContext<WebSessionContext>()
.BuildSessionFactory();
}
// Returns our database configuration
private static MsSqlConfiguration CreateDbConfig()
{
return MsSqlConfiguration
.MsSql2008
.ConnectionString( c => c.FromConnectionStringWithKey( "testConn" ) );
}
// Returns our mappings
private static AutoPersistenceModel CreateMappings()
{
var cfg = new AutomappingConfiguration();
return AutoMap
.Assemblies(cfg,System.Reflection.Assembly.GetCallingAssembly()).IncludeBase<CombatElement>()
.Conventions.Setup( c => c.Add( DefaultCascade.SaveUpdate() ) );
}
// Updates the database schema if there are any changes to the model,
// or drops and creates it if it doesn't exist
private static void UpdateSchema( Configuration cfg )
{
new SchemaUpdate( cfg )
.Execute( false, true );
}
Does anyone has any idea?

How do I map an Enumeration class without the discriminator being passed into the constructor?

Based on Jimmy's Enumeration classes idea I am wanting to see if I can avoid using the constructor to instantiate my type (which I assume is happening with the discriminator-value) but rather use a "factory method"-esque way of getting my instance mapped from the db.
Here is my type:
public class Impact : Enumeration
{
public static readonly Impact Carbon
= new Impact(1, "Carbon dioxide equivalent", CommonUnit.CO2e);
public static readonly Impact Energy
= new Impact(2, "Energy", CommonUnit.MJ);
public static readonly Impact Cost
= new Impact(3, "Cost", CommonUnit.Dollars);
public Impact(int index, string name, CommonUnit unit)
: base(index, name)
{
this.Unit = unit;
}
public CommonUnit Unit { get; private set; }
}
And here is the definition for Enumeration:
public class Enumeration : ValueObject
{
public Enumeration(int index, string displayName)
{
this.Index = index;
this.DisplayName = displayName;
}
public int Index { get; private set; }
public string DisplayName { get; private set; }
public override string ToString()
{
return this.DisplayName;
}
public static IEnumerable<T> GetAllFor<T>() where T : Enumeration
{
foreach (var publicStatic in typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
{
Enumeration item = null;
item = (Enumeration)publicStatic.GetValue(null);
yield return item as T;
}
}
public static T With<T>(int index) where T : Enumeration
{
return GetAllFor<T>().SingleOrDefault(i => i.Index == index);
}
}
ValueObject simply covers off Equality functionality.
Elsewhere I use the static methods to get items from this enum (kinda like how you could use the core Enumeration static methods):
impact = Impact.With<Impact>(index.ImpactId.Value);
This is pretty handy but I want to know if I can get NHibernate to do this too when rehydrating objects.
Can it be done and how?
With an NHibernate Custom Type:
public class EnumerationType<T> : PrimitiveType where T : Enumeration
{
public EnumerationType()
: base(new SqlType(DbType.Int32))
{
}
public override object Get(IDataReader rs, int index)
{
object o = rs[index];
var value = Convert.ToInt32(o);
return Enumeration.With<T>(value);
}
public override object Get(IDataReader rs, string name)
{
int ordinal = rs.GetOrdinal(name);
return Get(rs, ordinal);
}
public override Type ReturnedClass
{
get { return typeof(T); }
}
public override object FromStringValue(string xml)
{
return int.Parse(xml);
}
public override string Name
{
get { return "Enumeration"; }
}
public override void Set(IDbCommand cmd, object value, int index)
{
var parameter = (IDataParameter)cmd.Parameters[index];
var val = (Enumeration)value;
parameter.Value = val.Value;
}
public override string ObjectToSQLString(object value, Dialect dialect)
{
return value.ToString();
}
public override Type PrimitiveClass
{
get { return typeof(int); }
}
public override object DefaultValue
{
get { return 0; }
}
}
If you're doing an HBM.xml-based mapping, you can set the custom type like this:
<property name="Impact" column="Impact" type="Namespace.To.EnumerationType`1[[Impact, AssemblyWithDomainEnum]], AssemblyWithNHibCustomType"/>
Alternatively, if you're using Fluent NHibernate, you can create a convention to map all enumeration types without having to configure each one individually:
public class EnumerationTypeConvention : IPropertyConvention, IPropertyConventionAcceptance
{
private static readonly Type _openType = typeof(EnumerationType<>);
public void Apply(IPropertyInstance instance)
{
var closedType = _openType.MakeGenericType(instance.Property.PropertyType);
instance.CustomType(closedType);
}
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => typeof(Enumeration).IsAssignableFrom(x.Property.PropertyType));
}
}
And then add that convention however you like in your Fluent NHibernate configuration.
This seemed to work too, but perhaps Jimmy's way seems easier:
public class ImpactEnumType : IUserType
{
public SqlType[] SqlTypes
{
get
{
//We store our Impact in a single column in the database that can contain a int (for the index value)
SqlType[] types = new SqlType[1];
types[0] = new SqlType(DbType.Int32);
return types;
}
}
public Type ReturnedType
{
get { return typeof(Impact); }
}
public bool Equals(object x, object y)
{
// Impact is derived from ValueObject which implements Equals
return x.Equals(y);
}
public int GetHashCode(object x)
{
// as above
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
//We get the string from the database using the NullSafeGet used to get ints
int impactIndex = (int)NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
// then pull the instance from the Enumeration type using the static helpers
return Impact.With<Impact>(impactIndex);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
//Set the value using the NullSafeSet implementation for int from NHibernateUtil
if (value == null)
{
NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
return;
}
value = (value as Impact).Index;
NHibernateUtil.Int32.NullSafeSet(cmd, value, index);
}
public object DeepCopy(object value)
{
//We deep copy the Impact by creating a new instance with the same contents
if (value == null) return null;
return Impact.With<Impact>((value as Impact).Index);
}
public bool IsMutable
{
get { return false; }
}
public object Replace(object original, object target, object owner)
{
//As our object is immutable we can just return the original
return original;
}
public object Assemble(object cached, object owner)
{
//Used for casching, as our object is immutable we can just return it as is
return cached;
}
public object Disassemble(object value)
{
//Used for casching, as our object is immutable we can just return it as is
return value;
}
}
My HBM XML:
<property name="Impact" column="ImpactIndex" type="namespace.childnamespace.ImpactEnumType, namespace.childnamespace" />

Problem with NHibernate mapping when Id is in abstract base class

I'm quite new to nhibernate, I was doing all right until I face this problem, It looks like a NHibernate bug, but being a newbie with it, it can certainly be my fault
Having this base class to do all the Id and equality stuff
public abstract class ObjetoConId
{
public ObjetoConId()
{
Id=NewId();
}
public virtual Guid Id {get;private set;}
public override bool Equals(object o)
{
if (Object.ReferenceEquals(this,o))
return true;
if (o==null) return false;
ObjetoConId oId;
oId= o as ObjetoConId;
if (!Object.ReferenceEquals(oId,null))
return (Id.Equals(oId.Id));
return (base.Equals(o));
}
public override int GetHashCode()
{
byte[] bId;
bId=Id.ToByteArray();
return ((Int32)(bId[8]^bId[12])<<24) +
((Int32)(bId[9]^bId[13])<<16) +
((Int32)(bId[10]^bId[14])<<8) +
((Int32)(bId[11]^bId[15]));
}
public virtual bool Equals(ObjetoConId o)
{
if (Object.ReferenceEquals(this,o))
return true;
if (Object.ReferenceEquals(o,null)) return false;
return (Id.Equals(o.Id));
}
public virtual string toString()
{
return this.GetType().FullName
+ "[id=" + Id + "]";
}
protected virtual Guid NewId()
{
return GuidComb.NewGuid();
}
public static bool operator == (ObjetoConId x,ObjetoConId y)
{
if(Object.ReferenceEquals(x,y))
return true;
if(Object.ReferenceEquals(x,null))
return false;
return x.Equals(y);
}
public static bool operator != (ObjetoConId x,ObjetoConId y)
{
return !(x==y);
}
/// <summary>
/// Metodo interno para permitir el testing
/// </summary>
/// <param name="id"></param>
internal void setId(Guid id)
{
Id=id;
}
}
and this entity
public class Propiedad : ObjetoConId,IPropiedad
{
[Obsolete("Persistance Constructor only")]
public Propiedad ()
{
}
public Propiedad (IList<IDescripcionCalificada> descripciones)
{
Descripciones=new Dictionary<string,IDescripcionCalificada>(descripciones.Count);
foreach(IDescripcionCalificada d in descripciones)
Descripciones.Add(d.Nombre,d);
}
#region IPropiedad implementation
public virtual IDictionary<string, IDescripcionCalificada> Descripciones {get;private set;}
#endregion
}
and this mapping
public class MapeoPropiedad : ClassMap<Propiedad>
{
public MapeoPropiedad()
{
Id(x => x.Id).Column("pro_id").GeneratedBy.Assigned();
HasMany<DescripcionCalificada>(x => x.Descripciones)
.Cascade.SaveUpdate()
.AsMap<string>(index => index.Nombre)
;
}
}
The test for it is
[TestFixture]
public class TestPropiedadPersistencia
{
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
string connectionString="Server=127.0.0.1;Database=Ana;User ID=dev-test;Password=dev-test;";
fcfg=Fluently.Configure()
.Database(PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MapeoPropiedad>());
fcfg.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true));
sessions=fcfg.BuildSessionFactory();
}
ISessionFactory sessions;
FluentConfiguration fcfg;
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
fcfg.ExposeConfiguration(cfg => new SchemaExport(cfg).Drop(false, true));
sessions.Close();
sessions = null;
fcfg = null;
}
[Test]
public void CanCorrectlyMapPropiedad()
{
DescripcionCalificada descri1=new DescripcionCalificada("descri",new Descripcion("Esta es la descri"));
DescripcionCalificada descri2=new DescripcionCalificada("descriLarga",new Descripcion("Esta es la descriLarga"));
Dictionary<string,IDescripcionCalificada> descris=new Dictionary<string, IDescripcionCalificada>(2);
descris.Add(descri1.Nombre,descri1);
descris.Add(descri2.Nombre,descri2);
new PersistenceSpecification<Propiedad>(sessions.OpenSession(),new CustomEqualityComparer() )
.CheckProperty(c => c.Descripciones,descris)
.VerifyTheMappings();
}
}
The thing is that the test fails unless I put Not.LazyLoad() in the mapping
It gives a mapping error
Ana.Nucleo.Lenguaje.Test.TestDescripcionCalificadaPersistencia (TestFixtureSetUp):
FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
----> NHibernate.InvalidProxyTypeException : The following types may not be used as proxies:
Ana.Catalogacion.Implementacion.Propiedad: method setId should be 'public/protected virtual' or 'protected internal virtual'
without lazy loading it pass, and if I put the Id property in the Propiedad class and not inherit from ObjetoConID it also pass, with and without the Not.LazyLoad().
Anyone can confirm this is a NH bug, or any help will be appreciated
EDIT:
I've found the problem, my fault. I missed the setId internal function not being virtual protected and confused with the setter of the Id property, and thus missunderstood the execption
Fer
I've found the problem, my fault. I missed the setId internal function not being virtual protected and confused with the setter of the Id property, and thus missunderstood the execption

NHibernate: Using a custom type as a primary key

I have a legacy schema that contains tables with primary keys of type binary(16) -- its a MD5 hash of the other columns. NHibernate does not work with byte[] as a key since it does not implement Equals so I wrapped this in a custom type and provided NHibernate with an implementation of IUserType. Notice that MD5Hash is a struct and not a class.
public struct MD5Hash : IComparable, IComparable<MD5Hash>, IEquatable<MD5Hash> {
private readonly byte[] contents;
...
}
Everything worked fine until I created a many-to-one mapping to a type that uses MD5Hash as its key.
public class Referenced : IEquatable<Referenced> {
...
public virtual MD5Hash Id { get; set; }
public virtual string Name { get; set; } // must NOT be null
...
}
public class Referencer : IEquatable<Referencer> {
...
public virtual MD5Hash Id { get; set; }
public virtual Referenced Other { get; set } // may be null
...
}
When I attempt to load objects of type Referencer, NHibernate does not see a null value for the key when the row contains a NULL value so it attempts to instantiate an object of type
Referenced, assign it to Referencer, and update Referencer in the database. Since Referenced has a property, Name, which maps to a non-nullable column, NHibernate raises an exception. What I want is for NHibernate to set the Other property to null.
I could change the definition of MD5Hash to be a class instead of a struct but I have an unknown number of places in the code that probably assumes MD5Hash can never be null so I am looking for another solution.
The code for the custom type...
internal class MD5HashType : IUserType {
public SqlType[] SqlTypes {
get { return new[] { new SqlType(DbType.Binary, 16) }; }
}
public Type ReturnedType {
get { return typeof(MD5Hash); }
}
public new bool Equals(object x, object y) {
return Object.Equals(x, y);
}
public int GetHashCode(object x) {
return (null == x) ? 0 : x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner) {
var val = NHibernateUtil.Binary.NullSafeGet(rs, names[0]);
return (null == val || DBNull.Value == val) ? MD5Hash.Empty : new MD5Hash((byte[])val);
}
public void NullSafeSet(IDbCommand cmd, object value, int index) {
var val = (MD5Hash.Empty == ((MD5Hash)value)) ? null : ((MD5Hash)value).ToByteArray();
NHibernateUtil.Binary.NullSafeSet(cmd, val, index);
}
public object DeepCopy(object value) {
return value;
}
public bool IsMutable {
get { return false; }
}
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;
}
}
The problem appears to be that NHibernate can't tell that MD5Hash.Empty means no value. Have you tried creating custom event listeners such as the following to handle this? Something like:
public class CustomLoadListener : DefaultLoadEventListener {
public override void OnLoad(LoadEvent #event, LoadType loadType) {
if(#event.EntityId is MD5Hash) {
var id = (MD5Hash) #event.EntityId;
if(id == MD5Hash.Empty) {
#event.Result = new Referenced { Id = MD5Hash.Empty };
return;
}
}
base.OnLoad(#event, loadType);
}
}
public class CustomSaveOrUpdateListener : DefaultSaveOrUpdateEventListener {
public override void OnSaveOrUpdate(SaveOrUpdateEvent #event) {
var entity = #event.Entity as Referenced;
if(entity != null && entity.Id == MD5Hash.Empty) {
return;
}
base.OnSaveOrUpdate(#event);
}
}
You would then have to configure these listeners in your session factory via hibernate.cfg.xml:
<session-factory>
<!-- various properties -->
<listener type="load" class="NhHacking.CustomLoadListener, NhHacking"/>
<listener type="save-update" class="NhHacking.CustomSaveOrUpdateListener, NhHacking"/>
</session-factory>
If someone has a better idea of how to accomplish this, I would love to hear it.

SQL 2008 HierarchyID support in NHibernate

Searched various NHibernate lists and haven't come up with a definitive answer. The SQL2008 dialect doesn't appear to have support for the HierarchyID data type - new date and time types only.
Does anyone have a good implementation or an effective workaround? I'd really like to leverage HierarchyID in a new app of mine. Support for this interesting and powerful data type is sorely lacking in MS's own tools so I'm not shocked that NHibernate doesn't have support.
There are some approaches out there that I haven't delved into yet. Wondering if anyone has some experience in what works, what is more performant, etc.'
Full disclosure: I'm working with Castle ActiveRecord but this seems like an NHibernate issue.
I've given Needles' answer a test run. It's a very good answer but there are some changes needed to make it function (at least in .NET 4). Here's what I've come up with for my project:
Update: the following code can be download over at GitHub and will be updated there. NHiberntate.HierarchyId.UserType
SqlHierarchyId IUserType
namespace NHibernate.UserTypes
{
using SqlTypes;
using System;
using System.Data;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Types;
public class HierarchyId : IUserType
{
#region Properties
public SqlType[] SqlTypes
{
get { return new[] { NHibernateUtil.String.SqlType }; }
}
public Type ReturnedType
{
get { return typeof(SqlHierarchyId); }
}
public bool IsMutable
{
get { return true; }
}
#endregion Properties
#region Methods
new public bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if (prop1 == null) return null;
return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
else if (value is SqlHierarchyId)
((IDataParameter)cmd.Parameters[index]).Value = ((SqlHierarchyId)value).ToString();
}
public object DeepCopy(object value)
{
if (value == null) return null;
return SqlHierarchyId.Parse(((SqlHierarchyId)value).ToString());
}
public object Replace(object original, object target, object owner)
{
return DeepCopy(original);
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
#endregion Methods
}
}
Mapping
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataLayer" namespace="NHibernate.Map">
<class name="NHibernate.Map.OrganizationUnit, DataLayer" table="`orgunit`">
<property name="HierarchyId" column="`ou_hid`" type="NHibernate.UserTypes.HierarchyId, DataLayer" />
...
</class>
</hibernate-mapping>
Object with HierarchyId
namespace NHibernate.Map
{
using Microsoft.SqlServer.Types;
public class OrganizationUnit
{
#region Fields
private SqlHierarchyId _hierarchyId;
...
#endregion Fields
#region Properties
public virtual SqlHierarchyId HierarchyId
{
get { return _hierarchyId; }
set { _hierarchyId = value; }
}
...
#endregion Properties
}
}
Disclaimer: Im not an NHibernate expert, however, we are using it with Fluent in an upcoming project which uses SQL Server 2008 R2 and Hierarchy IDs. The code below is what we are using currently on our dev environment and is not fully tested/refined. I copied the majority of the code from elsewhere (sorry I lost the link!)
You need to create a User Defined Type and then use it in your mappings. The mapping below is Fluent, Im not aware how to do it using ActiveRecord but Im guessing it should be similar!
User Defined Type
namespace YourNamespace {
public class SqlHierarchyIdUserType : IUserType {
public bool Equals(object x, object y) {
if(ReferenceEquals(x, y))
return true;
if(x == null || y == null)
return false;
return x.Equals(y);
}
public int GetHashCode(object x) {
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner) {
object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if(prop1 == null)
return null;
return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
}
public void NullSafeSet(IDbCommand cmd, object value, int index) {
if(value == null) {
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
} else {
if(value is SqlHierarchyId) {
SqlHierarchyId hId = (SqlHierarchyId)value;
((IDataParameter)cmd.Parameters[index]).Value = hId.ToString();
}
}
}
public object DeepCopy(object value) {
if(value == null)
return null;
var sourceTarget = (SqlHierarchyId)value;
SqlHierarchyId copy = SqlHierarchyId.Parse(sourceTarget.ToString());
return copy;
}
public object Replace(object original, object target, object owner) {
return DeepCopy(original);
}
public object Assemble(object cached, object owner) {
return DeepCopy(cached);
}
public object Disassemble(object value) {
return DeepCopy(value);
}
public SqlType[] SqlTypes {
get { return new[] { NHibernateUtil.String.SqlType }; }
}
public Type ReturnedType {
get { return typeof(SqlHierarchyId); }
}
public bool IsMutable {
get { return true; }
}
}
}
Fluent Mapping
Map(e => e.YourSqlHierarchyIdProperty)
.Column("YourSqlHierarchyIdFieldName")
.CustomType<SqlHierarchyIdUserType>();
Reading this post:
Castle ActiveRecord: Map to IUserType wihtin Class in C#
ActiveRecord uses a [Property] attribute to map User Defined Types. So for you it would look something like this:
public class YourDataObject {
[Property(ColumnType="YourNamespace.SqlHierarchyIdUserType, YourNamespace")
public virtual SqlHierarchyId YourSqlHierarchyIdProperty;
}
Hope it helps!