How to model a Many to many-relationship in code?

前端 未结 12 1883
星月不相逢
星月不相逢 2020-12-05 07:12

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

相关标签:
12条回答
  • 2020-12-05 07:21

    Why are you talking about tables? Are you creating an object model or a database model?

    For an object model, there's no reason a Dog can't have a List<Owner> and an owner have a List<Dog>. Only if you have attributes on the relationship do you need an intermediate class (what UML calls an Association Class). That's when you'd have a DogOwnership class with extra properties, and each Owner would have a List<DogOwnership>, and so would each Dog. The DogOwner would have a Dog, an Owner, and the extra properties.

    0 讨论(0)
  • 2020-12-05 07:28

    This is a classic issue between databases where many to many doesn't work, hence your helper table, and the object world where many to many works fine. As soon as the relationship has attributes then you should create a new class to hold that information. However, you'll save yourself a lot of head time if you look at Object Relation Mapping - ORM - that whole field grew up to solve this (and many other) problems between DB and Object.

    0 讨论(0)
  • 2020-12-05 07:31

    If you didn't need to record the nickname, then Dog should have a list of Bosses and Boss should have a list of Dogs.

    If the relationship between Dog and Boss has attributes, in this case nickname, then you should create a class to represent that relationship and have Dog and Boss both hold lists of that type.

    I've been using NHibernate for a while now and find it very useful for easing this sort of object relational impedance mismatch.

    0 讨论(0)
  • 2020-12-05 07:32

    the traditional many to many relation would have no extra fields on the matching table.

    Because you do have fields with unique information I tend to stop thinking of these relations as many to many.

    Once you add information to the matching table i think you have then made this table into an entity in its own right and so needs its own object to represent it.

    At this point you can begin to have a DogsName class to connect a person and a dog - both of which would contain references to this object as part of a collection.

    However whether you give the dog a name to be called by or own the dog are independant.

    As well as modelling the relation of dogs name according to different people you also need to model the ownership relationships. In memory this would mean both objects contain a list of the other objects.

    0 讨论(0)
  • 2020-12-05 07:33

    If you have a simple many-to-many linking table with foreign keys from each table in the relationship, then you would model it as you suggest: Boss has a collection of Dogs and Dog has a collection of Bosses.

    If you have a many-to-many relationship with extra data, such as Nickname, then you would model that as two one-to-many relationships. Create an entity, such as DogBoss so that Boss has a collection of DogBoss and Dog has a collection of DogBoss.

    0 讨论(0)
  • 2020-12-05 07:33

    I guess am missing something. Why is many to many not allowed?

    public class Boss
    {
        Dog[] dogs;
    }
    
    public class Dog
    {
        Boss[] bosses;
    }
    
    0 讨论(0)
提交回复
热议问题