在 NHibernate 中选择一对一和组件
我正在尝试映射类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键应该在此类的您的用例中。不要以 ASP.NET Membership 为例,因为它的设计很糟糕。
您需要回答以下问题:
如果大多数问题都是正确的,那么您就有一个使用一对一的好例子(我不同意@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:
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.
你不应该使用任何一个。
一对一
您的用户和配置文件位于不同的数据库表中,但两者共享互斥的 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.