Mark Allison
Mark Allison

Reputation: 7228

Modeling many-to-many relationship in code

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:

Upvotes: 1

Views: 127

Answers (1)

cbp
cbp

Reputation: 25638

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; }
}

Upvotes: 1

Related Questions