Suppose I have 2 tables in a database. eg: Dog & Boss This is a many to many relationship, cause a boss can have more than 1 dog, and a dog can have more than 1 owner. I
Something like this; It still needs some finetuning though (make the collection private and add a readonly public accessor for it which returns a readonlycollection for instance, but you'll catch the drift.
public class Dog
{
public List Bosses;
public void AddBoss( Boss b )
{
if( b != null && Bosses.Contains (b) == false )
{
Bosses.Add (b);
b.AddDog (this);
}
}
public void RemoveBoss( Boss b )
{
if( b !=null && Bosses.Contains (b) )
{
Bosses.Remove (b);
b.RemoveDog (this);
}
}
}
public class Boss
{
public List Dogs;
public void AddDog( Dog d )
{
if( d != null && Dogs.Contains (d) == false )
{
Dogs.Add(d);
d.AddBoss(this);
}
}
public void RemoveDog( Dog d )
{
if( d != null && Dogs.Contains(d) )
{
Dogs.Remove (d);
d.RemoveBoss(this);
}
}
}
In this way, you could model a many-to-many in your code where every Dog knows his Bosses, and every Boss knows his Dogs. When you need extra data in the helper table, you'll need to create another class as well.