流畅的NHibernate自定义类型惯例bools

时间:2011-12-05 23:33:42

标签: fluent-nhibernate

尝试创建适用于所有bool属性的约定。

鉴于此课程:

public class Product
{
    public string Name { get; private set; }
    public bool IsActive { get; private set; }

    public Product(string name)
    {
        Name = name;
    }
}

我有一个映射:

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Map(x => x.Name);
        Map(x => x.IsActive).CustomType<YesNoType>();
    }
}

我想有一个这样做的约定。到目前为止,我有:

public class YesNoTypeConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<YesNoType>();
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Property.DeclaringType == typeof(bool));
    }
}

我正在添加这样的约定:

return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                            .ConnectionString(c => c.FromConnectionStringWithKey("dev"))
                            .AdoNetBatchSize(256)
                            .UseOuterJoin()
                            .QuerySubstitutions("true 1, false 0, yes 'Y', no 'N'"))
                .CurrentSessionContext<ThreadStaticSessionContext>()
                .Cache(cache => cache.ProviderClass<HashtableCacheProvider>())
                .ProxyFactoryFactory<ProxyFactoryFactory>()
                .Mappings(m => m.FluentMappings.AddFromAssembly(mappingAssembly)
                    .Conventions.Add(new ForeignKeyNameConvention(),
                                    new ForeignKeyContstraintNameConvention(),
                                    new TableNameConvention(),
                                    new YesNoTypeConvention(),
                                    new IdConvention()))
                .ExposeConfiguration(c => c.SetProperty("generate_statistics", "true"))
                .BuildSessionFactory();

但约定并没有得到应用。 我认为问题是Apply方法中的Expect标准。我尝试过不同的Property类型,比如DeclaringType和PropertyType。

我应该查看IPropertyInsepector的哪个属性以查看它是否是布尔值?

谢谢, 乔

1 个答案:

答案 0 :(得分:2)

criteria.Expect(x => x.Property.DeclaringType == typeof(bool));

// should be
criteria.Expect(x => x.Property.PropertyType == typeof(bool));