Mapping by code maps property from base type when mapped in inherited class

24 views
Skip to first unread message

Sly

unread,
May 14, 2013, 3:06:40 PM5/14/13
to nhu...@googlegroups.com
Hi, I'm struggling with the default behavior of mappings by code that do map property of parent if it is mapped in inherited class. The problem is following:

This test fails:

public class InheritedPropertyMappingTests
{
private class Foo
{
public virtual int Id { get; set; }

public virtual string ShouldNotBeMapped
{
get { return ""; }
set { throw new NotSupportedException();}
}
}

private class Bar : Foo
{
public override string ShouldNotBeMapped { get; set; }
}

[Test]
public void WhenPropertyIsNotMapped_ItShouldNotBePresentInResultingMappings()
{
var mapper = new ModelMapper(new SimpleModelInspector());
mapper.Class<Foo>(map => { });
mapper.Subclass<Bar>(map => map.Property(x => x.ShouldNotBeMapped));

var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

var hbmClass = hbmMapping.RootClasses[0];
Assert.That(hbmClass.Name, Is.StringContaining("Foo"));
Assert.That(hbmClass.Properties.Where(x => x.Name == "ShouldNotBeMapped"), Is.Empty, "Property is mapped only in inherited class, it should not be mapped in base class");
}
}

It fails as intended by test WhenMapPropertiesInTheInheritedThenMapInBase. Comment there says that it can be overridden with he help of SimpleModelInspector and examining ReflectedType and DeclaringType. But when I try to add my custom Inspector:

private class MyInspector : SimpleModelInspector
{
protected override bool DeclaredPolymorphicMatch(System.Reflection.MemberInfo member, Func<System.Reflection.MemberInfo, bool> declaredMatch)
{
return base.DeclaredPolymorphicMatch(member, declaredMatch);
}
}

then both member.ReflectedType and member.DeclaringType are equal to Foo. How do I map property only in inherited class? It wan't even an issue with XML mappings.
Reply all
Reply to author
Forward
0 new messages