在 NHibernate 中选择一对一和组件

发布于 2024-10-06 04:53:48 字数 610 浏览 5 评论 0原文

我正在尝试映射类 User 和 Profile,如下所示:

public class User
{
    public long ID { get; set; }
    public string LoginName { get; set; }
    public Profile Profile { get; set; }
}

public class Profile
{
    //the ID is the same with User's ID 
    public long ID { get; protected set; }
    public string NickName { get; set; }
    public Gender Gender { get; set; }
}

因此,它们可以映射为一对一关系和组件关系。我发现有些人评估该组件并认为一对一是一种不好的做法,为什么?出于性能原因?但在许多场景中它们被设计为两个单独的表(例如asp.net2.0 Membership)。

我该如何选择?我应该考虑哪些方面?我知道组件意味着“值对象”,但不是实体,但这是否意味着更多的事情?

ps:更让我困惑的是,即使在现实世界中是一对一的关系,也应该使用多对一的观点!

I am trying to map classes User and Profile, just as the following:

public class User
{
    public long ID { get; set; }
    public string LoginName { get; set; }
    public Profile Profile { get; set; }
}

public class Profile
{
    //the ID is the same with User's ID 
    public long ID { get; protected set; }
    public string NickName { get; set; }
    public Gender Gender { get; set; }
}

So, they can be mapped as both one-to-one and componenet relationship. And I find some people appraise the component and think one-to-one is a bad practise, why? For performance reason? But they are designed as two separate tables in many scenarios(asp.net2.0 Membership, for example).

How should I choose? Which aspects should I consider? I know component means "value object" but not an enitity, but does this mean some further things?

ps: And what confused me more is the opinion that the many-to-one should be used even it's one-to-one relationship in real world!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2024-10-13 04:53:48

关键应该在此类的您的用例中。不要以 ASP.NET Membership 为例,因为它的设计很糟糕。

您需要回答以下问题:

  1. 配置文件作为其自身的实体是否有意义?
  2. 您在您所在域的其他地方是否有对该配置文件的引用?
  3. 您可以拥有没有个人资料的用户吗?
  4. 它有自己的行为吗?
  5. 您会出于某种原因扩展(继承)配置文件吗?
  6. 大多数用例是否只处理用户(及其登录名,而不仅仅是 ID)而不处理个人资料?

如果大多数问题都是正确的,那么您就有一个使用一对一的好例子(我不同意@Falcon;这实际上是一对一的合法用途之一)

否则,组件将正常工作。它没有 ID,因此您可以删除该属性。

The key should be in your use cases for this class. Don't take ASP.NET Membership as an example, because its design is terrible.

You need to answer these questions:

  1. Does a Profile make sense as an entity of its own?
  2. Do you have references to the Profile anywhere else in your domain?
  3. Can you have a User without a Profile?
  4. Does it have a behavior of its own?
  5. Would you extend (inherit) Profile for some reason?
  6. Do most use cases just deal with the user (and its LoginName, not just the ID) but not the profile?

If most questions are true, you have a good case for using one-to-one (I disagree with @Falcon; this is actually one of the legitimate uses for one-to-one)

Otherwise, a Component will work fine. It doesn't have an ID, so you can remove that property.

北恋 2024-10-13 04:53:48

你不应该使用任何一个。

一对一

您的用户和配置文件位于不同的数据库表中,但两者共享互斥的 PK:
请参阅http://jagregory.com/writings /i-think-you-mean-a-many-to-one-sir/

对于关系数据库来说,这是相当糟糕的设计实践,它很混乱,并且不一定对关系强制实施约束。

组件

您可以使用组件从杂乱的关系数据库中获取干净的对象模型,配置文件和用户数据都存储在同一个数据库表中,但它们应该在您的对象模型中分开(就像您想要的那样,从您的代码来看) 。可能不支持延迟加载,这会导致数据库流量较高。

参考

恕我直言,您应该使用参考。它的概念有点像一对一,但用户引用一个配置文件。配置文件可以存储在它们自己的表中,可以延迟加载(性能)并且不依赖于用户。

关于您的困惑:
只需阅读我提供的链接即可。从技术上讲,对于正确设计的数据库方案,您需要多对一,因为这在技术上是可能的并且将被映射。我知道这很令人困惑。如果您只需要映射一侧,请考虑参考而不是一对一。

You should use neither.

One-To-One

You have the user and the profile in different database tables but both share a mutually exclusive PK:
See http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

Pretty bad design practice for relational databases, it's messy and does not necessarily enforce constraints for the relationship.

Component

You can use component to get a clean Object-Model from a messy relational database, profile and user data are both stored in the same database table but they should be separated in your object model (like you want it, judging from your code). Lazy loading probably isn't supported, which will cause high database traffic.

Reference

Imho, you should use a Reference. It's conceptual kinda like one-to-one but a user references a profile. The profiles can be stored in their own table, can be loaded lazily (performance) and are not dependent on a user.

Regarding your confusion:
Just read the link I supplied. Technically, you need a many to one for a properly designed database-scheme, as that is what is technically possible and will be mapped. I know it's confusing. If you just need to map one-side, think of a reference instead of a one-to-one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文