NHibernate ConventionModelMapper; What is the difference between IsRootEntity & IsEntity
Asked Answered
C

1

6

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.

Crabwise answered 22/10, 2012 at 16:19 Comment(0)
A
1

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

Auster answered 23/10, 2012 at 9:2 Comment(1)
For your case, IsEntity() should return true for A and B, while IsRootEntity should return true for only B. IsRootEntity() should be a sub set of IsEntity() and is only used to control mapping of class inheritance. For example, if you set IsRootEntity to true for both A and B it would map all their properties as two separate classes.Branle

© 2022 - 2024 — McMap. All rights reserved.