2011-12-05 41 views
2

嘗試創建適用於所有bool屬性的約定。流利的NHibernate bools的自定義類型約定

鑑於這種類:

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的哪些屬性以查看它是否爲布爾值?

謝謝, 喬

回答

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

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

感謝那些做了。 –

相關問題