Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate ConventionModelMapper; What is the difference between IsRootEntity & IsEntity

I'm playing around with Sharp Architecture Lite, which emphasizes convention over configuration, and trying to understand how the NHibernate ConventionModelMapper works. Specifically, I can't tell the difference between the IsRootEntity & IsEntity methods below (BTW, Entity is an abstract class that ships with Sharp Arch):

     internal static class Conventions
        {
        public static void WithConventions(this ConventionModelMapper mapper, Configuration configuration) {
                Type baseEntityType = typeof(Entity);

                mapper.IsEntity((type, declared) => IsEntity(type));
                mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType));

        public static bool IsEntity(Type type) {
                return typeof(Entity).IsAssignableFrom(type)
                       && typeof(Entity) != type
                       && !type.IsInterface;
            }
    }

I gather that the IsEntity method is used to tell NHibernate which classes are eligible for mapping/persistence to the DB. However, I can't for the life of me figure out what the IsRootEntity method does. The documentation around ConventionModelMapper is woefully sparse.

like image 832
Mitch A Avatar asked Oct 22 '12 16:10

Mitch A


1 Answers

If you consider the case:

class B : Entity { ... }
class A : B { ... }

When mapping them, both A and B are entities (IsEntity should return true for them), and NHibernate will map A as a subclass of B. However, Entity itself should not be mapped because it's a base class for all the entities (typically you would not want this base class mapped), so IsRootEntity will return true for Entity, and false for all subclasses of it - thus indicating that Entity should not be mapped as it's a "root" class

like image 134
Martin Ernst Avatar answered Nov 15 '22 08:11

Martin Ernst