Fluent NHibernate Custom Type convention for bools

 ̄綄美尐妖づ 提交于 2020-01-13 20:17:16

问题


Trying to create a convention that applies to all bool properties.

Given this class:

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

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

I have a mapping like:

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

I would like to have a convention that does this. So far I have:

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

I am adding the convention as such:

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

But the convention is is not being applied. I think the problem is the Expect criteria in the Apply method. I have tried different Property types, like DeclaringType and PropertyType.

Which property of the IPropertyInsepector should I be looking at to see if it is a boolean?

Thanks, Joe


回答1:


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

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


来源:https://stackoverflow.com/questions/8393439/fluent-nhibernate-custom-type-convention-for-bools

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!