using System.Reflection;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
namespace FNHProblem
{
class Program
{
static void Main(string[] args)
{
Fluently .Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration .MsSql2008.ConnectionString("Server=(local);Integrated Security=SSPI;" ))
.Mappings(mappings => mappings.AutoMappings.Add(AutoMap .Assemblies(Assembly .GetAssembly(typeof (Program )))
.OverrideAll(p => p.IgnoreProperties(ShouldIgnoreMember)))
.ExportTo("." ))
.BuildConfiguration()
.BuildSessionFactory();
}
public static bool ShouldIgnoreMember(Member member)
{
return member.Name.StartsWith("Display");
}
}
public abstract class BaseEntity
{
public virtual int Id { get; set; }
public abstract string DisplayString { get; }
}
public class EntityA : BaseEntity
{
#region Overrides of BaseEntity
public override string DisplayString
{
get { return "I am EntityA"; }
}
#endregion
public virtual string DisplaySomethingElse { get; set; }
}
}
But then switching to using a newer API fixes my problem straight away, so here is the solution:
using System.Reflection;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
namespace FNHProblem
{
class Program
{
static void Main(string [] args)
{
Fluently .Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration .MsSql2008.ConnectionString("Server=(local);Integrated Security=SSPI;" ))
.Mappings(mappings => mappings.AutoMappings.Add(AutoMap .Assembly(Assembly .GetAssembly(typeof (Program )), new CustomDefaultAutoMappingConfiguration ()))
.ExportTo("." ))
.BuildConfiguration()
.BuildSessionFactory();
}
}
class CustomDefaultAutoMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
if (member.Name.StartsWith("Display" ))
{
return false ;
}
return base .ShouldMap(member);
}
}
public abstract class BaseEntity
{
public virtual int Id { get ; set ; }
public abstract string DisplayIShouldBeIgnored { get ; }
}
public class EntityA : BaseEntity
{
public override string DisplayIShouldBeIgnored
{
get { return "I am EntityA" ; }
}
}
}
No comments:
Post a Comment