Created
November 11, 2011 17:14
-
-
Save chilversc/1358583 to your computer and use it in GitHub Desktop.
FNH - overiden methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> | |
<class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.ItemBase, ConsoleApplication1" table="`ItemBase`"> | |
<id name="Id"> | |
<generator class="identity" /> | |
</id> | |
<property name="Name" /> | |
<joined-subclass name="ConsoleApplication1.Item, ConsoleApplication1"> | |
<key> | |
<column name="ItemBase_id" /> | |
</key> | |
<property access="property" name="IsOverriden" /> | |
</joined-subclass> | |
</class> | |
</hibernate-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// uses NHibernate 3.2.0.4000 | |
// uses FluentNHibernate 1.3.0.717 | |
namespace ConsoleApplication1 | |
{ | |
using System; | |
using System.IO; | |
using FluentNHibernate; | |
using FluentNHibernate.Automapping; | |
using FluentNHibernate.Automapping.Alterations; | |
using FluentNHibernate.Cfg; | |
using FluentNHibernate.Cfg.Db; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var cfg = new AutoMappingConfiguration (); | |
var mapping = AutoMap | |
.AssemblyOf<AutoMappingConfiguration> (cfg) | |
.UseOverridesFromAssemblyOf<AutoMappingConfiguration> () | |
.Conventions.AddFromAssemblyOf<AutoMappingConfiguration> (); | |
using (var file = File.CreateText ("mapping.xml")) | |
Fluently.Configure () | |
.Database (SQLiteConfiguration.Standard.InMemory) | |
.ProxyFactoryFactory<NHibernate.Bytecode.DefaultProxyFactoryFactory> () | |
.Mappings (m => m.AutoMappings.Add (mapping).ExportTo (file)) | |
.BuildConfiguration (); | |
} | |
} | |
public class AutoMappingConfiguration : DefaultAutomappingConfiguration | |
{ | |
public override bool ShouldMap(Type type) | |
{ | |
return type == typeof (ItemBase) || type == typeof (Item); | |
} | |
} | |
public class ItemBaseOverride : IAutoMappingOverride<ItemBase> | |
{ | |
public void Override (AutoMapping<ItemBase> mapping) | |
{ | |
mapping.IgnoreProperty (x => x.IsOverriden); | |
} | |
} | |
public class ItemOverride : IAutoMappingOverride<Item> | |
{ | |
public void Override (AutoMapping<Item> mapping) | |
{ | |
// Fails: | |
mapping.IgnoreProperty (x => x.IsOverriden); | |
// Works: | |
// mapping.IgnoreProperty (Reveal.Member<Item> ("IsOverriden")); | |
} | |
} | |
public class ItemBase | |
{ | |
public long Id { get; set; } | |
public string Name { get; set; } | |
public virtual bool IsOverriden | |
{ | |
get { return false; } | |
} | |
} | |
public class Item : ItemBase | |
{ | |
public override bool IsOverriden | |
{ | |
get { return true; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment