views:

142

answers:

2

Here is a small VS 2010 solution with a single failing test that replicates the following issue.

I have a convention UserTypeConvention<MyUserType> where MyUserType : IUserType where MyUserType handles an enum type MyEnum. I have configured Fluent NHibernate thusly

sessionFactory = Fluently
                .Configure()
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(
                    c => c.Is(connectionString))
                )
                .Mappings(
                    m => m
                            .FluentMappings
                                .AddFromAssemblyOf<A>()
                            .Conventions
                                .AddFromAssemblyOf<A>()
                )
                .BuildSessionFactory();

where A is a type in the same assembly as UserTypeConvention<MyUserType> and MyUserType. However, Fluent NHibernate is not applying MyUserType to properties of type MyEnum on my domain objects. Instead, it is applying FluentNHibernate.Mapping.GenericEnumMapper<MyEnumType> to these properties.

What is going on?

A: 

Ok i tried the following and I think it will works for you :
just overriede the Accept method in MyEnumUserTypeConvention class and do nothing inside it:

  public class MyEnumUserTypeConvention : UserTypeConvention<MyEnumUserType>
  {
    public override void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria<FluentNHibernate.Conventions.Inspections.IPropertyInspector> criteria)
    {
       ///Do nothing
    }
  }
Nour Sabouny
This didn't work, my test still fails. The point is this _should_ work without having to override anything in `UserTypeConvention`.
Jason
your test fails because you are checking that propertyType (status) should be equal to MyEnumUserType, but if you manually checked the type of propertyType, you see it is of type Nhibernate CustomType and its name is 'MyEnumUserType', and i think that what should be.
Nour Sabouny
@Nour Sabouny: Okay, that's fine. You're right about that. But my point that this should work without having to override `UserTypeConvention.Accept` still remains.
Jason
@Nour Sabouny: This will not work. Because there is no acceptance criteria, all properties are accepted which is clearly not desirable.
Jason
A: 

For now I have solved this with:

public class MyEnumUserTypeConvention : MyEnumUserConvention<MyEnumUserType> {
    public override void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) {
        // Fluent NHibernate is too eager in applying GenericEnumMapper
        // so our criteria is that it is already applied this type
        criteria.Expect(x => x.Type == typeof(GenericEnumMapper<MyEnum>));
    }

    public override void Apply(IPropertyInstance instance) {
        // we override Fluent NHibernate's application of GenericEnumMapper
        instance.CustomType<MyEnumUserType>();
    }
}

I think this should be thoroughly unnecessary. If someone told me this were a bug in Fluent NHibernate, that'd be fine. If someone gave me a good reason why Fluent NHibernate should be so eager in applying GenericEnumMapper that would be acceptable too.

Jason