Why is Fluent NHibernate ignoring my convention?

I have an agreement UserTypeConvention<MyUserType>where MyUserType : IUserType, where it MyUserTypehandles an enumeration type MyEnum. I configured Fluent NHibernate this way

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

where A- type in the same assembly as the UserTypeConvention<MyUserType>and MyUserType. However, Fluent NHibernate does not apply MyUserTypeto type properties MyEnumon my domain objects. Instead, it applies to these properties FluentNHibernate.Mapping.GenericEnumMapper<MyEnumType>.

What's happening?

+3
source share
2 answers

Now I 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 application of GenericEnumMapper
        instance.CustomType<MyEnumUserType>();
    }
}

, . - , Fluent NHibernate, . - , Fluent NHibernate GenericEnumMapper, .

+1

, , , :
Accept MyEnumUserTypeConvention :

  public class MyEnumUserTypeConvention : UserTypeConvention<MyEnumUserType>
  {
    public override void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria<FluentNHibernate.Conventions.Inspections.IPropertyInspector> criteria)
    {
       ///Do nothing
    }
  }
0

Source: https://habr.com/ru/post/1768348/


All Articles