Fluent NHibernate - 映射多级类层次结构
我有一个遗留数据库,它使用每个类层次结构继承策略的一个表,该策略使用两个数据库列作为区分符,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到了使用字符串连接的 2 个选项:
为字符串连接生成一个存储过程,可以在每个数据库系统中重新实现并使用公式来调用它
使类映射
UnknownMap
数据库感知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