Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Created February 7, 2011 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehadlow/814203 to your computer and use it in GitHub Desktop.
Save mikehadlow/814203 to your computer and use it in GitHub Desktop.
Fluent NHibernate failing test (can insert into FluentNHibernate.Testing and run)
using System;
using System.Data;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.SqlTypes;
using NHibernate.Tool.hbm2ddl;
using NHibernate.UserTypes;
using NUnit.Framework;
namespace FluentNHibernate.Testing.Spike
{
// Fails with:
// Test 'FluentNHibernate.Testing.Spike.UserTypeSpike.Should_be_able_to_user_custom_userType_with_NHibernate' failed: NHibernate.MappingException : Could not determine type for: FluentNHibernate.Testing.Spike.My, FluentNHibernate.Testing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(Name)
// at NHibernate.Mapping.SimpleValue.get_Type()
// at NHibernate.Mapping.SimpleValue.IsValid(IMapping mapping)
// at NHibernate.Mapping.PersistentClass.Validate(IMapping mapping)
// at NHibernate.Mapping.RootClass.Validate(IMapping mapping)
// at NHibernate.Cfg.Configuration.ValidateEntities()
// at NHibernate.Cfg.Configuration.Validate()
// at NHibernate.Cfg.Configuration.BuildSessionFactory()
// Spike\UserTypeSpike.cs(28,0): at FluentNHibernate.Testing.Spike.UserTypeSpike.Should_be_able_to_user_custom_userType_with_NHibernate()
[TestFixture]
public class UserTypeSpike
{
[Test]
public void Should_be_able_to_user_custom_userType_with_NHibernate()
{
var configuration = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(x => x.FluentMappings
.Add(typeof(WidgetMap))
.Conventions.Add(new MyUserTypeConvention()))
.BuildConfiguration();
var sessionFactory = configuration.BuildSessionFactory();
using (var session = sessionFactory.OpenSession())
{
BuildSchema(session, configuration);
var id = 0;
using (var transaction = session.BeginTransaction())
{
var widget = new Widget { Name = new My { Value = "Das Widget" } };
session.Save(widget);
transaction.Commit();
id = widget.Id;
Console.Out.WriteLine("id = {0}", id);
session.Evict(widget);
}
using (var transaction = session.BeginTransaction())
{
var widget = session.Load<Widget>(id);
Console.Out.WriteLine("widget.Name.Value = {0}", widget.Name.Value);
transaction.Commit();
}
}
}
private static void BuildSchema(ISession session, Configuration configuration)
{
var export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
}
}
public class Widget
{
public virtual int Id { get; set; }
public virtual My Name { get; set; }
}
public class WidgetMap : ClassMap<Widget>
{
public WidgetMap()
{
Id(x => x.Id);
Map(x => x.Name);
// works fine with an explicit type mapping
// Map(x => x.Name).CustomType<MyUserType>();
}
}
public class My
{
public string Value { get; set; }
}
public class MyUserTypeConvention : UserTypeConvention<MyUserType> { }
public class MyUserType : BaseImmutableUserType<MyUserType>
{
public override object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;
return new My { Value = value };
}
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
var my = value as My;
NHibernateUtil.String.NullSafeSet(cmd, my, index);
}
public override SqlType[] SqlTypes
{
get { return new SqlType[] { SqlTypeFactory.GetString(20) }; }
}
}
/// <summary>
/// Thanks to Darrell Mozingo
/// http://darrell.mozingo.net/2009/02/10/generic-nhibernate-user-type-base-class/
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseImmutableUserType<T> : IUserType
{
public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);
public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
public abstract SqlType[] SqlTypes { get; }
public new 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 DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public Type ReturnedType
{
get { return typeof(T); }
}
public bool IsMutable
{
get { return false; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment