【问题标题】:Pivoting rows into properties/columns with NHibernate使用 NHibernate 将行转换为属性/列
【发布时间】:2012-09-18 03:13:57
【问题描述】:

使用 NHibernate 将下表结构建模为下面的“透视”域模型的最佳方法是什么?

映射属性是微不足道的;它确实是我不确定的行到属性/虚拟列的枢纽。

数据库结构

属性表

Id      Name                Type
Attr1   Some Attribute 1    Bool
Attr2   Some Attribute 2    Bool
Attr3   Some Attribute 3    Bool

广告表

Id  Name
Ad1 Some ad 1

广告属性表

Id    AdId  AttributeId Value
100   Ad1   Attr1       true
101   Ad1   Attr2       false
102   Ad1   Attr3       true

所需的 NHibernate 对象模型

class Attribute{
    public int Id {get;set;}
    public Type TheType {get;set;}
    public string Name {get;set;}
}

class AdAttribute{
    public int Id{get;set;}
    public Ad Ad{get;set;}
    public Attribute Attribute{get;set;}

    public bool Value{get;set;}
}

class Ad{
    ...

    class Attributes{
        public AdAttribute Attr1{get;set;}
        public AdAttribute Attr2{get;set;}
        public AdAttribute Attr3{get;set;}
    }

    Attributes TheAttributes{get; protected set;}
}

表结构不是一成不变的,但目标是不将属性存储为广告上的列。不同的广告类型会有不同的属性,但它们不是动态变化的,即广告类型 1 总是有 10 个属性,广告类型 2 总是有 5 个属性,等等。

首选原始 NHibernate HBM 答案,但流畅的映射也可以

【问题讨论】:

  • 你考虑过IDictionary<string, object>吗? NHibernate 可以将对象序列化为 2 列,但我从未尝试过
  • 我认为这可能是我必须走的路线,但我不太确定它是否适用于上述结构
  • 当每个广告类型的属性都已修复时,为什么不为每个广告类型提供一个表格来保存特定属性? (每个类的表继承)

标签: c# .net nhibernate fluent-nhibernate nhibernate-mapping


【解决方案1】:

最终我们决定使用不同的设计来消除这种不必要的复杂性。

Firo 关于 NHibernate 的 IDictionary 支持的观点是一个很好的观点,但是很可能是解决方案

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2021-11-02
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    相关资源
    最近更新 更多