SelfTaughtCodingIdiot
SelfTaughtCodingIdiot

Reputation: 109

What is the cleanest approach handling many to many relationships?

A little background on this question as I know most commonly using an assignment/junction table will address this...

I'm working on a C# MVC web application, and I have classes a through f. class a is a base class for classes b through g all have multiple properties in common. My issue lies in classes b and c have a many to many relation ship as well as b/d, e/c, and f/d.

I've looked through inheritance models, but I've not really found an example where subclasses have many to many relationships or how best to address them.

It looks like my options are either: A) Table/Class A as my base, with tables for the assignments b/c, b/d, e/c, and f/d or B)Throw the Base out the window and have tables b, c, d, e, f and still have the tables for the assignments b/c, b/d, e/c, and f/d

My question is am I missing an option, is there a preferred direction on this?

UPDATE: I'm looking for the best approach in both the DB and the application structure. Does the underlying table structure need to follow option A or B. I'm sorry if this question seems silly, but I've been beating my head on this one.

Upvotes: 0

Views: 59

Answers (1)

Rahul
Rahul

Reputation: 77876

Why Inheritance ... rather you should be using Composition here. I mean your class b should be composed with class c instance as class member. something like

public class B
{
  public IEnumerable<C> ListOfC { get; set; }
} 

Upvotes: 1

Related Questions