Fool NHibernate自定义类型约定为bools [英] Fluent NHibernate Custom Type convention for bools

查看:156
本文介绍了Fool NHibernate自定义类型约定为bools的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

给定这个类:

{
public string Name {get;私人设置}
public bool IsActive {get;私人设置}

public Product(string name)
{
Name = name;


$ / code $ / pre

我有这样的映射:

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


$ / code $ / pre

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

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

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




$ b $ p
$ b我是这样添加约定:

  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标准。我已经尝试了不同的属性类型,如DeclaringType和PropertyType。



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


Joe

解决方案

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

//应该是
criteria.Expect(x => x.Property.PropertyType == typeof(bool));


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

解决方案

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

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

这篇关于Fool NHibernate自定义类型约定为bools的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆