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

前端 未结 12 2013
星月不相逢
星月不相逢 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:37

    Am I missing something or is the only code you need for this as follows:

    List BossList;
    
    class Dog {}
    class Boss { Dog[] Dogs; }
    

    You don't need to explicitly model the two-way relationship. It's implicit in the code structure. There may be other reasons to do so, but in general it is sufficient to have a one-way reference and a way to traverse the set of referencing objects.

提交回复
热议问题