ActiveRecord/NHibernate 中多个类可以共享单个数据库表吗

发布于 2024-10-24 21:02:37 字数 141 浏览 2 评论 0原文

C# w/Castle ActiveRecord

我在 C# 中有两个(或更多)类,我想与同一个数据库表进行交互。这两个类不共享基类,因此我无法使用鉴别器。我只想将表列划分为两个(或更多)类。这与 JoinedTable 属性的目的相反。这可以做到吗?

C# w/Castle ActiveRecord

I've two (or more) classes in C# that I'd like to interact with the same database table. These two classes do NOT share a base class, so I can't use a Discriminator. I'd simply like to divide the table columns over two (or more) classes. Kind of the opposite of the purpose of the JoinedTable attribute. Can this be done?

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

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

发布评论

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

评论(2

苦妄 2024-10-31 21:02:37

为了扩展我的评论,您可以通过继承多个接口对活动记录对象中的列进行逻辑分组......

interface IPerson
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

interface IAddress
{
    string AddressLine1 { get; set; }
    string AddressLine2 { get; set; }
    string City { get; set; }
    string Province { get; set; }
    string Country { get; set; }
}

class CustomerRecord : IPerson, IAddress // ActiveRecord object map to table...
{  
    // IPerson members...
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // IAddress members...
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string Country { get; set; }
}

To expand on my comment, you can logically group the columns in the active record object by inheriting multiple interfaces...

interface IPerson
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

interface IAddress
{
    string AddressLine1 { get; set; }
    string AddressLine2 { get; set; }
    string City { get; set; }
    string Province { get; set; }
    string Country { get; set; }
}

class CustomerRecord : IPerson, IAddress // ActiveRecord object map to table...
{  
    // IPerson members...
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // IAddress members...
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string Province { get; set; }
    public string Country { get; set; }
}
拥醉 2024-10-31 21:02:37

有两种策略可以将多个类映射到一张表:

继承:是的,我知道你没有它,但你也可以使用接口作为“基类”。在同一个表中拥有多个没有任何共同点的(根)类是没有意义的。

组件:与连接实际上相反。您可以将表的某些部分放入单独的类中。在这种情况下,只有一个根实体聚合其他类。

There are two strategies to map several classes to one table:

Inheritance: yes, I know you don't have it, but you could also use an interface as "base class". It doesn't make sense to have several (root) classes in the same table witch don't have anything in common.

Components: are the actual opposite of join. You can put parts of the table to separate classes. In this case, there is only one root entity which aggregates other classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文