在代码中建模多对多关系
使用 C#4.0
继我的问题 如何为许多配置 nHibernate -很多映射?我很困惑,这可能是因为我的类设计是错误的。
市场对金融市场进行建模。经纪人模拟可以与许多市场互动的金融经纪人。市场可以与许多经纪人互动。请参阅下面的 Broker
和 Market
类。
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
的额外属性,它对于 Market
和 Broker
组合来说是唯一的。
对此进行建模的最佳方法是什么?我是否需要创建第三个类,或者我可以将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我会创建第三个实体:
Yes, I would create a third entity: