简体   繁体   English

为什么Fluent NHibernate无视我的惯例?

[英]Why is Fluent NHibernate ignoring my convention?

I have a convention UserTypeConvention<MyUserType> where MyUserType : IUserType where MyUserType handles an enum type MyEnum . 我有一个约定UserTypeConvention<MyUserType>其中MyUserType : IUserType ,其中MyUserType处理枚举类型MyEnum I have configured Fluent NHibernate thusly 我已经配置了Fluent NHibernate

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 . 其中AUserTypeConvention<MyUserType>MyUserType在同一程序UserTypeConvention<MyUserType> However, Fluent NHibernate is not applying MyUserType to properties of type MyEnum on my domain objects. 但是,功能NHibernate不适用MyUserType到类型的属性MyEnum在我的域对象。 Instead, it is applying FluentNHibernate.Mapping.GenericEnumMapper<MyEnumType> to these properties. 相反,它将FluentNHibernate.Mapping.GenericEnumMapper<MyEnumType>应用于这些属性。

What is going on? 到底是怎么回事?

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. 如果有人告诉我这是Fluent NHibernate的一个错误,那就没事了。 If someone gave me a good reason why Fluent NHibernate should be so eager in applying GenericEnumMapper that would be acceptable too. 如果有人给了我一个很好的理由,为什么Fluent NHibernate应该如此渴望应用GenericEnumMapper ,这也是可以接受的。

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: 只是覆盖MyEnumUserTypeConvention类中的Accept方法,并在其中不执行任何操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM