如何建模许多在代码中的许多关系? [英] How to model a Many to many-relationship in code?

查看:121
本文介绍了如何建模许多在代码中的许多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据库表2。
如:狗和放大器;老板
这是一个多对多的关系,导致老板可以有超过1狗,狗可以有超过1名业主。我是鲍比的所有者,但这样是我的妻子

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 am the owner of Bobby, but so is my wife.

但多对多是不允许的,所以有helpertable:DogsPerBoss

But many to many is not allowed, so there is a helpertable: DogsPerBoss

如何在代码模型呢?

级BOSS可以有狗的集合。
类犬可以有老板的集合。
- >至少,这就是我的想法。也许还有更好的解决方案?

Class Boss can have a collection of Dogs. Class Dog can have a collection of Bosses. --> at least, that is what I think. Perhaps there are better solutions?

怎么样在辅助表额外的数据?
假如那是在去老板级或狗类?
例如:昵称
(我称之为狗好孩子和我的妻子称他为小狗)

How about extra data that is in the helper-table? Should that be in de Boss-class or in the Dog-class? eg: Nickname (I call the dog "good boy" and my wife calls him "doggie")

我希望我的问题是有点儿明确?
是否有什么是实现这一目标的最佳途径的最佳实践?
你能给我一些参考?

I hope my question is kinda clear? Are there any best-practices on what is the best way to achieve this? Can you give me some references?

这是ORM(如NHibernate的)是不是一种选择。

An ORM (like NHibernate) is not an option.

推荐答案

为什么你在说什么表? ?您创建一个对象模型或数据库模型

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

有关对象模型,没有理由狗不能有一个列表<所有者> ; 和业主有一个列表与LT;狗> 。只有当你有关系的属性,你需要一个中间类(UML称为关联类)。这时候,你必须有额外的属性DogOwnership类,每个业主将有一个列表与LT; D​​ogOwnership> ,所以将各自的狗。该DogOwner将有一只狗,所有者,以及额外的属性。

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.

这篇关于如何建模许多在代码中的许多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆