问题
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