多对多具有隐式和显式关系 [英] Many to many with implied and explicit relationships
问题描述
我的数据库中Person和Widget之间存在标准的多对多关系.具有管理角色的人员有权访问所有小部件.在我的应用程序中,我想查看一个人可以访问哪些小部件.
I have a standard many-to-many relationship in my database between Person and Widget. A Person in an administrative role has access to ALL Widgets. In my application, I want to see which Widgets a Person has access to.
我有两个高级选择:
-
明确管理关系.当人员成为管理员时,将该人员与所有现有小部件关联.创建窗口小部件后,将该窗口小部件与所有现有管理员相关联.
Explicitly manage the relationships. When a Person becomes an administrator, relate that Person to all existing Widgets. When a Widget is created, relate that Widget to all existing administrators.
在运行时,如果Person是管理员,则假定他们有权访问所有窗口小部件,并在加载窗口小部件时绕过关系表.
At run-time, if the Person is an administrator, assume they have access to ALL widgets and bypass the relationship table when loading Widgets.
一种选择是否比另一种更好?这种情况有名字吗?
我一直在尝试使用NHibernate应用选项2,我似乎无法弄清楚如何为实体加载所有小部件时使其绕过关系表(即使我可以,也不必要地加载了很多)信息,除非我与Person实体分开加载小部件并应用分页).
I have been trying to apply option 2 using NHibernate and I can't seem to figure out how to get it to bypass the relationship table when loading all Widgets for an entity (and even if I could, this would unnecessarily load alot of information unless I load Widgets separately from the Person entity and apply paging).
推荐答案
我将通过角色对此进行映射.
I would map this by means of Roles.
角色:人= 1:很多
因此,当您创建一个人时,您还将创建一个新角色,除非他们是管理员,在这种情况下,他们将使用现有的管理员角色.
So when you create a person, you also create a new Role, unless they are an Administrator in which case they use the existing Admin Role.
然后问题很简单:您需要一个WidgetRole表.
Then the problem is easy: You need a WidgetRole table.
创建新的Widget并将条目自动添加到NewWidget AdminRole的WidgetRole表中
When a new Widget is created, and entry is automatically added to the WidgetRole table for NewWidget, AdminRole
当人员更改为管理员角色后,只需更改其当前角色即可.
When a Person changes to an Admin Role, simply change their current Role.
与具有特殊的Admin情况相比,此设置在逻辑上更简单.
imo this setup is logically simpler, than having a special Admin case.
这篇关于多对多具有隐式和显式关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!