在代码中建模多对多关系

发布于 2024-12-25 16:57:24 字数 1069 浏览 4 评论 0原文

使用 C#4.0

继我的问题 如何为许多配置 nHibernate -很多映射?我很困惑,这可能是因为我的类设计是错误的。

市场对金融市场进行建模。经纪人模拟可以与许多市场互动的金融经纪人。市场可以与许多经纪人互动。请参阅下面的 BrokerMarket 类。

public class Market
{
    public int Id { get; set; }
    public string Symbol { get; set; }
    public string Description { get; set; }
}

public class Broker
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDefault { get; set; }
    public bool IsActive { get; set; }
    public IList<Account> Accounts { get; set; }
}

我在多对多关系中有一个名为 MinIncrement 的额外属性,它对于 MarketBroker 组合来说是唯一的。

对此进行建模的最佳方法是什么?我是否需要创建第三个类,或者我可以将 MinIncrement 放入现有的类之一中吗?我应该创建第三个类并继承现有的类之一吗?我不太确定如何以面向对象的方式对此进行建模。

在我的数据库中这很容易。我有三个表:

  • 经纪人(PK:brokerId)
  • 市场(PK:marketId)
  • brokerMarkets(PK:brokerId,marketId),MinIncrement列位于此处

Using C#4.0

Following on from my question How to configure nHibernate for many-many mapping? I'm getting confused and it may be because my class design is wrong.

A Market models a financial market. A Broker models a financial broker who can interact with many markets. A Market can interact with many Brokers. See the Broker and Market classes below.

public class Market
{
    public int Id { get; set; }
    public string Symbol { get; set; }
    public string Description { get; set; }
}

public class Broker
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsDefault { get; set; }
    public bool IsActive { get; set; }
    public IList<Account> Accounts { get; set; }
}

I have an extra attribute in the many-to-many relationship called MinIncrement that is unique to a Market and a Broker combination.

What's the best way to model this? Do I need to create a third class or can I put MinIncrement in one of my existing classes? Should I create a third class and inherit from one of my existing ones? I'm not really sure how to model this in an OO way.

In my database it's easy. I have three tables:

  • brokers (PK: brokerId)
  • markets (PK: marketId)
  • brokerMarkets (PK: brokerId, marketId), MinIncrement column goes in here

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

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

发布评论

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

评论(1

樱娆 2025-01-01 16:57:24

是的,我会创建第三个实体:

public class BrokerMarketRelationship
{
    public int Id { get; set; }
    public Broker Broker { get; set; }
    public Market Market { get; set; }
    public int MinIncrement { get; set; }
}

Yes, I would create a third entity:

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