有没有办法在 Fluent NHibernate 自动映射中按命名空间忽略基类型/类?

发布于 2024-10-03 01:47:41 字数 1009 浏览 0 评论 0原文

目前,我正在使用以下代码通过 Fluent NHibernate 自动映射来忽略基类型:

AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
            .Conventions.Setup(GetConventions())
            .IgnoreBase<MyCore.BaseTypes.AmazingBaseType>()
            .IgnoreBase<MyCore.BaseTypes.AnotherAmazingBaseType>()
            .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

有没有办法通过命名空间(即 MyCore.BaseTypes)忽略基类型,而不必使用 我拥有的每个基本类型的 IgnoreBase() 方法?

我尝试使用 DefaultAutomappingConfiguration 扩展类(即 MyDefaultAutomappingConfiguration)中的重写 ShouldMap(Type) 方法来完成此操作,但它仍然尝试映射基本类型:

public class MyDefaultAutomappingConfiguration: DefaultAutomappingConfiguration {
    public override bool ShouldMap(Type type) {
        return !IsBaseType(type);
    }

    private bool IsBaseType(Type type) {
        return type.Namespace == typeof(MyCore.BaseTypes).Namespace;
    }
}

Currently, I am using the following code to ignore base types with Fluent NHibernate automapping:

AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
            .Conventions.Setup(GetConventions())
            .IgnoreBase<MyCore.BaseTypes.AmazingBaseType>()
            .IgnoreBase<MyCore.BaseTypes.AnotherAmazingBaseType>()
            .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

Is there a way to ignore base types by a namespace (i.e. MyCore.BaseTypes) instead of having to use the IgnoreBase() method for every base type I have?

I tried accomplishing this using my overriden ShouldMap(Type) method in DefaultAutomappingConfiguration-extended class (i.e. MyDefaultAutomappingConfiguration) to accomplish this but it still tried to map the base types:

public class MyDefaultAutomappingConfiguration: DefaultAutomappingConfiguration {
    public override bool ShouldMap(Type type) {
        return !IsBaseType(type);
    }

    private bool IsBaseType(Type type) {
        return type.Namespace == typeof(MyCore.BaseTypes).Namespace;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Hello爱情风 2024-10-10 01:47:41

您的 MyAutomappingConfiguration 是什么样的?您可以使用以下内容覆盖 ShouldMap(Type)

public override bool ShouldMap(System.Type type)
{
  return 
    type.BaseType.Namespace == typeof (MyCore.BaseTypes.BaseIWant).Namespace;
}

What does your MyAutomappingConfiguration look like? You can override ShouldMap(Type) with something like:

public override bool ShouldMap(System.Type type)
{
  return 
    type.BaseType.Namespace == typeof (MyCore.BaseTypes.BaseIWant).Namespace;
}
活雷疯 2024-10-10 01:47:41

如果您的基本类型是抽象的,它们将不会包含在映射中。

If your base types are abstract they wont be included in the mapping.

独自唱情﹋歌 2024-10-10 01:47:41

您可以直接在配置中执行此操作,如下所示:

AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
            .Conventions.Setup(GetConventions())
            .Where(t => t.Namespace != "MyCore.BaseTypes")
            .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

You can do it directly in your configuration like this:

AutoMap.AssemblyOf<Class1>(new MyDefaultAutomappingConfiguration())
            .Conventions.Setup(GetConventions())
            .Where(t => t.Namespace != "MyCore.BaseTypes")
            .UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文