我是否打破了这个模型中的总体边界?
我正在使用 NHibernate 建模一个非常基本的 ASP.NET MVC 应用程序,但我似乎陷入了我的设计之中。 这是我的模型的草图:
正如你所看到的,这是非常基本的,但我对此有一些担忧。 用户根实体和组织根实体通过两个一对多关系访问相同的 Organization_Users 子实体。 这似乎不对,我认为我正在打破总体界限。 这个模型对我来说很奇怪,但我喜欢这个想法,因为我想要这样的代码:
var user = userRepository.Load(1);
var list = user.Organizations; // All the organizations the user is a part of.
并且
var org = orgRepository.Load(1);
var list = org.Users; // All the users in an organization.
表中的额外数据(如标记和角色)将由组织实体使用。 这是一个糟糕的设计吗? 如果您有任何想法那就太好了。 我仍在努力思考 DDD 的思想。 谢谢
I'm modeling a very basic ASP.NET MVC app using NHibernate and I seem to be stuck on my design. Here's a sketch of my model:
As you can see this is VERY basic but I have some concerns about it. The User root entity and the Organization root entity are accessing the same Organization_Users entity child via two one-to-many relationships. This doesn't seem right and I think I am breaking the aggregate boundaries. This model smells to me but I like the idea because I would like to have code like this:
var user = userRepository.Load(1);
var list = user.Organizations; // All the organizations the user is a part of.
and
var org = orgRepository.Load(1);
var list = org.Users; // All the users in an organization.
Also the extra data in the table like flagged and role would be used by the Organization entity. Is this a bad design? If you have any thoughts that would be great. I'm still trying to get my mind around the thinking of DDD. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是典型的多对多关系。 Organization_Users 表是桥表。 事实上,NHibernate 和所有其他 ORM 工具都具有支持桥接表的内置功能。
这个问题应该在数据建模级别而不是应用程序级别来解决。 您应该分析您的数据模型,建议避免多对多关系(从某种意义上说,如果不是领域模型的必要性,您应该尽量避免多对多关系)。
首先,您需要确保数据模型中的多对多关系对于映射域实体是必要的。 完成此操作后,图表中表示的模型就可以在应用程序级别映射这些关系
This is a typical Many-To-Many relationship. And the Organization_Users tables is the bridge table. Infact NHibernate and all the other ORM tools have built-in feature to support bridge table.
This thing should be resolved at data modelling level rather than at application level. You should analyze your data model and it is recommended to avoid many-to-many relationships (in the sense if it is not the necesity of domain model, you should try to avoid many-to-many relationship).
First thing first you need to be sure that many-to-many relationship in data model is necessary for mapping domain entities. Once you have done this then the model represented in your diagram is ok for mapping those relationships at application level
我曾多次使用与您的第一个模型类似的方法。 这种方法的一个问题是,您需要在域中创建一个 OganizationUser 类来处理域中的 Role 和 Flagged 字段。 这会在您的代码中留下类似的内容。
*如果您要经常迭代所有用户组织,您可能希望在
您提交的第二个设计中 立即加载 Organization 实体以及 OrganzitionUser看起来您可以将用户添加到 OrgUserDetails 中,而无需将用户添加到 OrganizationUser 中。 这似乎不是我想要从我的域中支持的东西。
I have used an approach similar to your first model on several occasion. The one catch with this approach is that you need to create an OganizationUser class in your domain to handle the Role and Flagged fields from you Domain. This would leave you with something like this in your code.
*If you're going to be iterating through all a users organizations quite often you'd likely want to eager load the Organization entity along with OrganzitionUser
With the second design you submitted it looks like you would be able to add a user to the OrgUserDetails without adding the user to OrganizationUser. That doesn't seem like something I would want to support from my Domain.
在 DDD 中首先要考虑的事情是:
没有数据库!)
The first things to consider in DDD are :
no database !)
我认为你的模型很好。 当我想到领域聚合根时,我通常会根据公开暴露的内容而不是内部实现来考虑它们。 对于关系,我会考虑在关系中哪个实体“负责”。 也就是说,将用户添加到组织还是将组织添加到用户更自然? 在这种情况下,两者都可能有意义,用户加入组织; 组织接受用户为会员。
如果您的域从用户的角度查看关系,您可以在用户上放置维护(添加、删除等)关系的方法,并在组织上公开只读集合。
回应你的第二个设计(如果你编辑了原来的问题会更好):我一点也不喜欢它。 你原来的设计很好。 在设计类时,我不一定会忽略数据库,好的设计应该准确地对域进行建模,并且可以直接在关系数据库中实现。 有时你必须在两个方向上做出妥协才能达到最佳效果。 打破总体界限不会被判入狱。 :-)
I think your model is fine. I usually think of domain aggregate roots, when I think of them at all, in terms of what is publicly exposed, not internal implementation. With relationships I think of which entity "wears the pants" in the relationship. That is, is it more natural to add a User to an Organization or add an Organization to a User? In this case both may make sense, a User joins an Organization; an Organization accepts a User for membership.
If your domain sees the relationship from the User's perspective, you can put the methods to maintain (add, remove, etc.) the relationship on the User and expose a read-only collection on the Organization.
In response to your second design (it would have been better if you had edited the original question): I don't like it at all. Your original design is fine. I wouldn't necessarily ignore the database while designing your classes, a good design should accurately model the domain and be straightforward to implement in a relational database. Sometimes you have to compromise in both directions to hit the sweet spot. There's no jail term for breaking aggregate boundaries. :-)
我的理解是:
一个用户可以属于0到多个组织。
和
组织由 0 到多个用户组成。
两者都正确吗? 如果是这样,那对我来说确实听起来像是多对多。
在多对多中,您非常需要某种类似关系的对象来弥合这一差距。 问题是,域中没有 user_organization。
这让我认为您本身不应该将 user_organization 作为域的一部分。 感觉像是一个实现细节。
另一方面,也许在您的域中拥有一个名册来保存组织中的用户并存储他们的角色和特定于该关系的其他信息是有意义的。
My understanding is:
A User can belong to 0-to-many Organizations.
AND
An Organization consists of 0-to-many Users.
Are both of those correct? If so, that does sound like a many-to-many to me.
In a many-to-many, you pretty much need a relationship-like object of some sort to bridge that gap. The problem is, there is no user_organization in the domain.
This makes me think you shouldn't have user_organization as a part of your domain, per se. It feels like an implementation detail.
On the other hand, maybe it makes sense in your domain to have a Roster which holds the Users in an Organization and stores their role and other information specific to that relationship.
谢谢大家的回答。 他们非常有帮助。
当我更多地思考我的模型时,我画了一些我认为会更好的新东西。
我的想法是这样的:
当用户登录网站时,系统会找到他们的帐户,然后返回他们所属的组织列表,并从 user_organizations 对象获取此信息。
当用户单击他们所属的组织之一时,会将他们定向到该组织的控制面板。
当用户单击他们所属的组织之一时,会将
然后,所选组织在其 org_user_details 中查找该用户的角色,以了解该用户对该组织控制面板应具有的访问权限。
然后,所选组织在其 org_user_details 中查找
那有意义吗? :)
我觉得这在模型中会很好,但我对数据库实现有一些疑问。 我知道我什至不应该担心,但我还无法改掉我的坏习惯! 您可以看到 user_organizations 对象和 org_user_details 对象中存在重复数据。 我不是数据库专业人士,但这是一个糟糕的数据库设计吗? 我是否应该将 user_organizations 和 org_user_details 中的数据合并到一个表中,就像我第一篇文章中的表一样,然后告诉 NHibernate User 将其视为多对多关系,而 Organization 将其视为一对多关系? 这听起来像是我在欺骗系统。 抱歉,如果我对此感到很困惑。
您对此有何看法? 我是不是想多了? :P
Thanks everyone for your answers. They have been very helpful.
While I was thinking about my model a little bit more, I sketched something new that I think would be better.
My thinking was this:
When a user logs into the site the system finds their account and then returns a list of organizations they are apart of and it gets this info from the user_organizations object.
When a user clicks on one of the organizations they are apart of it directs them to the organization's control panel.
The selected organization then looks up that user's role in its org_user_details to know what access the user should have to that organizations control panel.
Does that make sense? :)
I feel like that would be good in a model but I'm having some doubts about the DB implementation. I know I shouldn't even worry about it but I can't break my bad habit yet! You can see that there is kind of duplicate data in the user_organizations object and the org_user_details object. I'm not a DB pro but is that a bad DB design? Should I instead combine the data from user_organizations and org_user_details into a table like the one in my first post and just tell NHibernate that User looks at it as a Many-to-Many relationship and Organization looks at it as a One-to-Many relationship? That sounds like I'm tricking the system. Sorry if I seemed really confused about this.
What are your thoughts on this? Am I over thinking this? :P