Why is Fluent NHibernate ignoring my convention?
-
28-09-2019 - |
Question
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?
Solution
For now I have solved this with:
public class MyEnumUserTypeConvention : UserTypeConvention<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.
OTHER TIPS
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
}
}