Fluent NHibernate Automapping with Abstract Base Class

Given the classes below:

public class Address : Place { public virtual string Street { get; set; } public virtual int Number { get; set; } public override string WhereAmI { get { string.Format("{0} {1}", Street , Number); } } } public abstract class Place : DomainEntity { public abstract string WhereAmI { get; } } 

When I use this mapping:

 var autoMap = AutoMap.AssemblyOf<Party>() .Override<Place>(map => map.IgnoreProperty(p => p.WhereAmI)) .Override<Address>(map => map.IgnoreProperty(p => p.WhereAmI)) .Where(type => type.Namespace != null && type.Namespace.Contains("Models")); 

I still get the error message: Could not find the installer for the "WhereAmI" property in the "Address" class

What I've done:

  • When I remove a property from the Address base class, it works.
  • When I use .OverrideAll (map => map.IgnoreProperty ("WhereAmI")) But I do not want it to be global, because in another class I could use the same property name in which I want to include this property

Is there a way to make this work and then use the interface?

+7
source share
1 answer

I tried tracking in FluentNHibernate's code exactly why IgnoreProperty seems to break when the ignored property comes from the base class, but the time runs out. It seems to work fine if the get-only property does not come from the base class.

In any case, the solution to your situation seems to be to create a custom IAutomappingConfiguration by inheriting from DefaultAutomappingConfiguration. See This Answer: How to create a Fluent NHibernate Convention that ignores properties that have no setters .

Here's a custom auto-configuration configuration that I have successfully used to automate the instance of the example you provided:

 protected class CustomConfiguration : DefaultAutomappingConfiguration { public override bool ShouldMap (Member member) { if (member.IsProperty && member.IsPublic && !member.CanWrite) { return false; } return base.ShouldMap(member); } public override bool ShouldMap(Type type) { return type.Namespace != null && type.Namespace.Contains("Models"); } } 

And then its use:

 var autoMap = AutoMap .AssemblyOf<DomainEntity>(new CustomConfiguration()); 

Note that the Where clause in your example should move to a custom configuration class, since it cannot be resolved if you use an instance of a custom configuration.

+4
source

All Articles