是一对一的关系糟糕的战略糟糕、战略、关系、是一对

2023-09-03 01:08:26 作者:玩世不恭.只求妳红颜一笑

用户总是有一个钱包。一个钱包总是属于一个用户。

因为我想单独钱的钱包有关的属性我创建钱包对象,并能够跟踪金钱交易,......我创建

 公众钱包:实体< INT>
{
    公共双入账金额{获得;组;}
    公众的IList< MoneyTrans>交易{获得;组;}
}
 

由于这显然是一对一的关系是它确定利用一对一的关系映射?

是一对一的糟糕的战略?

解决方案

我不得不追加的答案,有观点相反的角度。做的不可以使用一到一个映射。至少与NHibernate。

我说的不是概念领域驱动设计。只是我的经验,数据库设计和NHibernate的使用情况。

1)一到一个 - 硬质数据库设计

首先与共享主键的德兴的(继承的除外)的可能导致许多问题之后,当商务要求发生变更。

的典型场景中,非常类似于示例 23.2。作者/工作,其中作者映射一到一个为人。因此,作者的ID(主键)来自人(ID)。迟早,企业会来问我们能否得人映射到作者的(见拉尔斯·开普勒的例子)的

我只想在这里说的是:第24章最佳实践的(让我举一个点)的

  

对持久类声明标识符属性。

     

NHibernate的使得标识属性可选。有各种各样的原因,你应该使用它们。我们建议标识符应该是人造(生成的,没有业务含义)和非基本类型。为了获得最大的灵活性,可以使用的Int64或字符串。

由于这里所说的(从我的经验)的,这是非常,这有利于将所有实体与自己的 surrogated 的主键。在关系未来以后的变化 - 会影响只是引用(在surrogated键的上方),不表/实体本身

2)一到一个与NHibernate的不能偷懒

其实这个理由之一,为什么(尽管事实上我试过几次)的目前不使用一到一个可言。在一到一个是不支持延迟加载。搜索更多信息,但很好的解释可以在这里找到:

Ayende NHibernate的一对之一的(一举)的

  

在换言之,一个对一不能被延迟加载,这是原因之一,建议使用两个多到1来代替。    对懒加载(单对单)的几点说明      如下面这个问题的意见中提到的其中一个链接的(引用)的

         字节跳动的第一场败仗 烧光20亿,悟空问答终落幕

您既可以包括所有的属性列到你的实体表 - 但在这种情况下,很多栏目最终会空了显著数项

  

或:你可以把那些选属性到一个单独的表中,设置了一个1:1(或者更确切地说:0:1)与基实体表的关系,

  

那么,与NHiberante你就不会得到这么多的改进。这个建议是错误的。不支持延迟加载之一到一... 的

摘要:

这就是为什么我强烈建议:使用多到一个一对许多在可能的情况。你会获得更多...

User always has one Wallet. One Wallet belongs always to one User.

Since I want to separate money wallet related properties I was create Wallet object and to be able to track money transactions, ... I created

public Wallet : Entity<int>
{
    public double Amont {get; set;}
    public IList<MoneyTrans> Transactions {get; set;}
}

Since this is obviously one to one relationship is it ok to map using one to one relationship?

Is one to one bad strategy?

解决方案

I had to append answer, with opposite point of view. Do not use one-to-one mapping. At least with NHibernate.

I am not talking about the conceptual domain driven design. Just about my experience with DB design and NHibernate usage.

1) one-to-one - Rigid DB design

First of all the desing with shared primary keys (except of inheritance) could lead to many issues later, when the Business requirements are changed.

The typical scenario, very similar to example 23.2. Author/Work, where Author is mapped one-to-one to Person. Therefore, the id (primary key) of the Author comes from a Person (id). Sooner or later, Business will come and ask could we have to person mapped to Author (see Lars Kepler example)

What I am trying to say here is: Chapter 24. Best Practices (let me cite one point)

Declare identifier properties on persistent classes.

NHibernate makes identifier properties optional. There are all sorts of reasons why you should use them. We recommend that identifiers be 'synthetic' (generated, with no business meaning) and of a non-primitive type. For maximum flexibility, use Int64 or String.

As mentioned here (and from my experience), it is very benefitial to have all entities with their own surrogated primary keys. The changes in relations coming later - will effect just references (on top of surrogated keys), not the table/entities themselves.

2) one-to-one with NHibernate cannot be lazy

In fact this reason is the one, why (despite of the fact I tried few times) currently do not use the one-to-one at all. The one-to-one is not supporting lazy loading. Search for more info but nice explanation could be found here:

Ayende NHibernate one-to-one (a cite)

In other words, one-to-one cannot be lazily loaded, which is one of the reasons why it is recommended to use two many-to-one instead. Some explanations on lazy loading (one-to-one) As mentioned in one of the links in comments below the question (cite)

You can either include all those attributes as columns into your entity table - but in that case, lots of columns would end up empty for a significant number of the entries.

Or: you can put those "optional" attributes into a separate table, set up a 1:1 (or rather: 0:1) relationship with the base entity table,

Well, with NHiberante you won't get so much improvement. This suggestion is wrong. Lazy loading of one-to-one isn't supported...

SUMMARY:

That's why I would strongly suggest: use many-to-one or one-to-many where possible. You'll gain more...