Fluent NHibernate - 映射多级类层次结构

发布于 2024-11-24 22:50:09 字数 2210 浏览 2 评论 0原文

我有一个遗留数据库,它使用每个类层次结构继承策略的一个表,该策略使用两个数据库列作为区分符,usetype 和 formattype。 UseType 为您提供顶层,formattype 为第二层。

本质上,我希望能够填充类层次结构,如:

public abstract class Unknown {}

public abstract class Animal : Unknown { }
public class Lion : Animal {}
public class Lamb : Animal {}

public class Mineral : Unknown { }

public abstract class Vegetable : Unknown {}

public class Rose : Vegetable { }
public class Cabbage : Vegetable {}

从数据中,如:

| UseType   | FormatType |
--------------------------
| Animal    | Lion       |
| Animal    | Lamb       |
| Mineral   | null       |
| Vegetable | Rose       |
| Vegetable | Cabbage    |

我想我可以使用流畅的映射,如:

public sealed class UnknownMap : ClassMap<Unknown>
{
    public UnknownMap()
    {
        DiscriminateSubClassesOnColumn("UseType");
    }
}

public sealed class AnimalMap : SubclassMap<Animal>
{
    public AnimalMap()
    {
        DiscriminatorValue("Animal");
        DiscriminateSubClassesOnColumn("FormatType");
    }
}

public sealed class LionMap : SubclassMap<Lion>
{
    public LionMap()
    {
        DiscriminatorValue("Lion");
    }
}

public sealed class LambMap : SubclassMap<Lamb>
{
    public LambMap()
    {
        DiscriminatorValue("Lamb");
    }
}

public sealed class MineralMap : SubclassMap<Mineral>
{
    public MineralMap()
    {
        DiscriminatorValue("Mineral");
    }
}

public sealed class VegetableMap : SubclassMap<Vegetable>
{
    public VegetableMap()
    {
        DiscriminatorValue("Vegetable");
        DiscriminateSubClassesOnColumn("FormatType");
    }
}


public sealed class RoseMap : SubclassMap<Rose>
{
    public RoseMap()
    {
        DiscriminatorValue("Rose");
    }
}

public sealed class CabbageMap : SubclassMap<Cabbage>
{
    public CabbageMap()
    {
        DiscriminatorValue("Cabbage");
    }
}

但不幸的是 SubclassMap 不支持 DiscrimminateSubClassesOnColumn。

根据 Fluent NHibernate Wiki,通过使用自定义 sql 语句支持多列鉴别器。

不幸的是,我必须支持相当多的子类,并且我真的更喜欢使用用代码编译的东西。毕竟,这就是我首先使用 Fluent NHibernate 的原因。

I have a legacy database that uses a table per class hierarchy inheritance strategy that uses two database columns as discriminators, usetype and formattype. UseType gives you the top level, formattype the second.

Essentially I would like to be able to populate a class hierarchy like:

public abstract class Unknown {}

public abstract class Animal : Unknown { }
public class Lion : Animal {}
public class Lamb : Animal {}

public class Mineral : Unknown { }

public abstract class Vegetable : Unknown {}

public class Rose : Vegetable { }
public class Cabbage : Vegetable {}

From data like:

| UseType   | FormatType |
--------------------------
| Animal    | Lion       |
| Animal    | Lamb       |
| Mineral   | null       |
| Vegetable | Rose       |
| Vegetable | Cabbage    |

I thought I could user Fluent mappings like:

public sealed class UnknownMap : ClassMap<Unknown>
{
    public UnknownMap()
    {
        DiscriminateSubClassesOnColumn("UseType");
    }
}

public sealed class AnimalMap : SubclassMap<Animal>
{
    public AnimalMap()
    {
        DiscriminatorValue("Animal");
        DiscriminateSubClassesOnColumn("FormatType");
    }
}

public sealed class LionMap : SubclassMap<Lion>
{
    public LionMap()
    {
        DiscriminatorValue("Lion");
    }
}

public sealed class LambMap : SubclassMap<Lamb>
{
    public LambMap()
    {
        DiscriminatorValue("Lamb");
    }
}

public sealed class MineralMap : SubclassMap<Mineral>
{
    public MineralMap()
    {
        DiscriminatorValue("Mineral");
    }
}

public sealed class VegetableMap : SubclassMap<Vegetable>
{
    public VegetableMap()
    {
        DiscriminatorValue("Vegetable");
        DiscriminateSubClassesOnColumn("FormatType");
    }
}


public sealed class RoseMap : SubclassMap<Rose>
{
    public RoseMap()
    {
        DiscriminatorValue("Rose");
    }
}

public sealed class CabbageMap : SubclassMap<Cabbage>
{
    public CabbageMap()
    {
        DiscriminatorValue("Cabbage");
    }
}

But unfortunately DiscriminateSubClassesOnColumn isn't supported in SubclassMap.

According to the Fluent NHibernate Wiki, multi column discriminators are supported through the use of a custom sql statement.

Unfortunately, I have to support a rather large number of subclasses, and I'd really prefer to use something that's compiled with the code. After all, that's why I'm using Fluent NHibernate in the first place.

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

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

发布评论

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

评论(1

入怼 2024-12-01 22:50:09

我想到了使用字符串连接的 2 个选项:

  • 为字符串连接生成一个存储过程,可以在每个数据库系统中重新实现并使用公式来调用它

  • 使类映射UnknownMap数据库感知

    public UnknownMap()
    {
        字符串公式;
        开关 (Configuration.GetDbType())
        {
            案例 DBType.SQLServer:
                公式 = "GROUP_CONCAT(UseType, FormatType´)";
                休息;
            ...
        }
    
        DiscrimminateSubClassesOnColumn("")
            .公式(公式);
    }
    

2 options for using string concatenation come to my mind:

  • generate a storedProcedure for stringconcatenation which can be reimplemented in each DB-System and use a formula to call it

  • make the classmap UnknownMap database aware

    public UnknownMap()
    {
        string formula;
        switch (Configuration.GetDbType())
        {
            case DBType.SQLServer:
                formula = "GROUP_CONCAT(UseType, FormatType´)";
                break;
            ...
        }
    
        DiscriminateSubClassesOnColumn("")
            .Formula(formula);
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文