无法扩展未映射的类:实体

时间:2012-07-09 20:20:53

标签: nhibernate nhibernate-mapping

我有一个名为Entity的基类:

public class Entity
{
    public int Id {get;set;}
}

假设我有一个名为Customer的课程:

public class Customer : Entity
{
    public string Name {get;set;}
}

现在,使用NHibernate 3.3.1中的代码使用基于约定的映射,我尝试以下方法:

public static class DataHelper
{
    private static HbmMapping GetMappings()
    {
        var mapper = new CustomModelMapper(typeof(Entity));           

        return mapper.CompileMappingFor(
            typeof(DataHelper).Assembly.GetExportedTypes()
                .Where(x => x.IsSubclassOf(typeof(Entity))));
    }
}

当我尝试运行我的应用程序时,出现错误“无法扩展未映射的类:实体”。我不想映射Entity类 - 它只是继承一些常见属性的基类。如何告诉NHibernate忽略未映射的类?作为参考,下面列出了我的CustomModelMapper类。

下面列出了我的CustomModelMapper类的代码以供参考

internal class CustomModelMapper : ConventionModelMapper
{
    private const int DEFAULT_STRING_LENGTH = 100;
    private Type baseType;

    public CustomModelMapper(Type baseType)
    {
        this.baseType = baseType;
    }

    public CustomModelMapper()
    {
        SetupInspectors();
    }

    protected override void AppendDefaultEvents()
    {
        base.AppendDefaultEvents();
        BeforeMapClass += OnBeforeMapClass;
        BeforeMapProperty += OnBeforeMapProperty;
        BeforeMapManyToOne += OnBeforeMapManyToOne;
        BeforeMapBag += OnBeforeMapBag;
        BeforeMapList += OnBeforeMapList;
        BeforeMapSet += OnBeforeMapSet;
    }

    protected void OnBeforeMapClass(IModelInspector modelInspector, Type type, IClassAttributesMapper classCustomizer)
    {
        classCustomizer.Id(type.GetProperty("Id"), m => m.Generator(Generators.Native));
    }

    protected void OnBeforeMapProperty(IModelInspector modelInspector, PropertyPath member, IPropertyMapper propertyCustomizer)
    {
        if (member.LocalMember.GetPropertyOrFieldType().IsEnum)
        {
            var type = member.LocalMember.GetPropertyOrFieldType();
            var genericType = typeof(EnumStringType<>).MakeGenericType(type);
            propertyCustomizer.Type(genericType, null);
        }

        if (member.LocalMember.GetPropertyOrFieldType() == typeof(string))
            propertyCustomizer.Length(DEFAULT_STRING_LENGTH);
    }

    protected void OnBeforeMapManyToOne(IModelInspector modelInspector, PropertyPath member, IManyToOneMapper propertyCustomizer)
    {
        propertyCustomizer.Cascade(Cascade.All);
        propertyCustomizer.Fetch(FetchKind.Join);
        propertyCustomizer.Lazy(LazyRelation.NoLazy);

        propertyCustomizer.Index(string.Format("IX{0}{1}",
            member.GetContainerEntity(modelInspector).Name,
            member.LocalMember.Name));
    }

    protected void OnBeforeMapBag(IModelInspector modelInspector, PropertyPath member, IBagPropertiesMapper propertyCustomizer)
    {
        propertyCustomizer.Cascade(Cascade.All);
        propertyCustomizer.Lazy(CollectionLazy.Extra);
        propertyCustomizer.Fetch(CollectionFetchMode.Subselect);
    }

    protected void OnBeforeMapList(IModelInspector modelInspector, PropertyPath member, IListPropertiesMapper propertyCustomizer)
    {
        propertyCustomizer.Cascade(Cascade.All);
        propertyCustomizer.Lazy(CollectionLazy.Extra);
        propertyCustomizer.Fetch(CollectionFetchMode.Subselect);
    }

    protected void OnBeforeMapSet(IModelInspector modelInspector, PropertyPath member, ISetPropertiesMapper propertyCustomizer)
    {
        propertyCustomizer.Cascade(Cascade.All);
        propertyCustomizer.Lazy(CollectionLazy.Extra);
        propertyCustomizer.Fetch(CollectionFetchMode.Subselect);
    }

    protected void SetupInspectors()
    {
        IsRootEntity((type, declared) =>
        {
            return baseType.Equals(type.BaseType);
        });

        IsEntity((type, declared) =>
        {
            return baseType.IsAssignableFrom(type) && !type.IsInterface;
        });

        IsVersion((member, declared) =>
        {
            return
                member.Name == "Version" &&
                member.MemberType == MemberTypes.Property &&
                member.GetPropertyOrFieldType() == typeof(int);
        });


        IsBag((member, declared) =>
        {
            if (member.GetPropertyOrFieldType().IsGenericType)
                return IsGenericType(member, typeof(ICollection<>));

            return false;
        });

        IsList((member, declared) =>
        {
            if (member.GetPropertyOrFieldType().IsGenericType)
                return IsGenericType(member, typeof(IList<>));

            return false;
        });

        IsSet((member, declared) =>
        {
            if (member.GetPropertyOrFieldType().IsGenericType)
                return IsGenericType(member, typeof(ICG.ISet<>));

            return false;
        });
    }

    protected static bool IsGenericType(MemberInfo member, Type targetType)
    {
        var type = member.GetPropertyOrFieldType();
        var generics = type.GetGenericInterfaceTypeDefinitions();
        return generics.Contains(targetType);
    }
}

1 个答案:

答案 0 :(得分:3)

问题可能在于您的IsEntity约定。目前,它将为Entity类本身返回true。只需添加另一张支票:

IsEntity((type, declared) =>
{
    return baseType.IsAssignableFrom(type) && !type.IsInterface &&
           type != typeof(Entity); // <- skip Entity class
});

修改

此外,您的CustomModelMapper课程中有两个构造函数。其中一个接受基类型,另一个是默认值并调用SetupInspectors()。正如我所看到的,你的默认构造函数将永远不会被调用,因为你正在调用接受基类型的那个,并且它不会调用默认的构造函数...

结果就是......你的SetupInspectors()方法也永远不会被调用。