为什么 Fluent NHibernate 忽略了我的约定?

发布于 2024-09-26 02:35:24 字数 1045 浏览 2 评论 0原文

我有一个约定 UserTypeConvention 其中 MyUserType : IUserType 其中 MyUserType 处理枚举类型 MyEnum。我因此配置了 Fluent NHibernate

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

,其中 A 是与 UserTypeConventionMyUserType 相同程序集中的类型。但是,Fluent NHibernate 不会将 MyUserType 应用于我的域对象上 MyEnum 类型的属性。相反,它将 FluentNHibernate.Mapping.GenericEnumMapper应用于这些属性。

到底是怎么回事?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-10-03 02:35:24

现在我已经解决了这个问题:

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>();
    }
}

我认为这应该是完全没有必要的。如果有人告诉我这是 Fluent NHibernate 中的一个错误,那就没问题了。如果有人给我一个很好的理由,为什么 Fluent NHibernate 应该如此热衷于应用 GenericEnumMapper,那也是可以接受的。

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.

橘虞初梦 2024-10-03 02:35:24

好的,我尝试了以下方法,我认为它对您有用:
只需覆盖 MyEnumUserTypeConvention 类中的 Accept 方法,并且在其中不执行任何操作:

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

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
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文