fluent-nhibernate icon indicating copy to clipboard operation
fluent-nhibernate copied to clipboard

Automapper tries to map private class

Open doerig opened this issue 13 years ago • 8 comments

After Upgrading from FNH 1.2 to 1.3 my project threw some runtime exceptions. It turned out that fnh 1.3 tries to map a private nested class. Is this intended behaviour or a bug?

I fixed the problem within the AutomappingConfiguration (checking for IsPublic).

    public class AutomappingConfiguration : DefaultAutomappingConfiguration
    {
        public override bool ShouldMap(Type type)
        {
            return type.IsPublic && type.IsSubclassOf(typeof(ModelBase));
        }
    }

Simplified class-setup:

public class MappedClass : ModelBase
{
}

public class NotMappedClass
{
    private class WhyDoesThisClassGetsMapped : MappedClass
    {
    }
}

doerig avatar Oct 18 '11 09:10 doerig

I guess that this particular scenario just wasn't tested. But you're making a point, I think it would be a good default to use only public classes out of the box and be able to override this behaviour at will. I'll take this one in, unless someone has other suggestions

chester89 avatar Aug 17 '12 21:08 chester89

I agree, I can't see why a private nested class should be mapped by default. @chester89 if you're happy to work on this, that's great; I think it should just be a case of pulling @doerig's changes into the DefaultAutomappingConfiguration.

jagregory avatar Aug 17 '12 22:08 jagregory

I'll get to it

chester89 avatar Sep 03 '12 21:09 chester89

it seems to me that ability to map private non-nested classes was intended, but private nested classes wasn't

chester89 avatar Sep 03 '12 21:09 chester89

I've got a question - does this change need to be reflected in documentation somewhere?

chester89 avatar Sep 03 '12 21:09 chester89

I think I got it working - but one test fails, it says public nested class shouldn't be automapped by default

chester89 avatar Sep 03 '12 22:09 chester89

So with this change, no nested classes would be mapped by default?

As for docs, there isn't any mention of nested classes in the docs currently; it might be worth adding a note to the automapping wiki page when this is resolved. Just so we've got the behaviour explicitly stated somewhere.

Thanks for your help on this.

jagregory avatar Sep 03 '12 23:09 jagregory

What I did is completely ignored only private nested classes when doing automapping. Public and protected nested classes are being mapped. Is this the desired behaviour? May be we should exclude protected classes also

chester89 avatar Sep 04 '12 06:09 chester89