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

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

提交回复
热议问题