重视DDD中的对象-为什么不可变? [英] Value objects in DDD - Why immutable?

查看:346
本文介绍了重视DDD中的对象-为什么不可变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么DDD中的值对象应该是不可变的,也看不出如何轻松地做到这一点. (如果重要的话,我主要关注C#和Entity Framework.)

I don't get why value objects in DDD should be immutable, nor do I see how this is easily done. (I'm focusing on C# and Entity Framework, if that matters.)

例如,让我们考虑经典的Address值对象.如果您需要将"123 Main St"更改为"123 Main Street ",为什么我需要构造一个全新的对象而不是说myCustomer.Address.AddressLine1 = "123大街"? (即使实体框架支持结构,这仍然是一个问题,不是吗?)

For example, let's consider the classic Address value object. If you needed to change "123 Main St" to "123 Main Street", why should I need to construct a whole new object instead of saying myCustomer.Address.AddressLine1 = "123 Main Street"? (Even if Entity Framework supported structs, this would still be a problem, wouldn't it?)

我理解(我认为)价值对象没有身份并且是领域对象的一部分的想法,但是有人可以解释为什么不变性是一件好事吗?

I understand (I think) the idea that value objects don't have an identity and are part of a domain object, but can someone explain why immutability is a Good Thing?

编辑:我的最后一个问题确实应该是有人可以解释为什么不变性是应用于价值对象的好东西吗? ?"抱歉造成混乱!

EDIT: My final question here really should be "Can someone explain why immutability is a Good Thing as applied to Value Objects?" Sorry for the confusion!

编辑:为方便起见,我并不是在问CLR值类型(相对于引用类型).我问的是价值对象的更高层次的DDD概念.

EDIT: To clairfy, I am not asking about CLR value types (vs reference types). I'm asking about the higher level DDD concept of Value Objects.

例如,这是一种为实体框架实现不可变值类型的方法:

For example, here is a hack-ish way to implement immutable value types for Entity Framework: http://rogeralsing.com/2009/05/21/entity-framework-4-immutable-value-objects. Basically, he just makes all setters private. Why go through the trouble of doing this?

推荐答案

忽略关于线程安全等的所有疯狂答案,这些答案与DDD无关. (我还没有看到线程安全的O/R映射器或其他DDD友好的dal)

Ignore all the crazy answers about thread safe etc, that has nothing to do with DDD. (I've yet to see a thread safe O/R mapper or other DDD friendly dal)

想象一下一个权重的值对象. 可以说我们有一个KG值对象.

Imagine a value object for weights. lets say we have a KG value object.

样本(为清晰起见进行了编辑):

sample (edited for clarity):

var kg75 = new Weight(75);
joe.Weight = kg75;
jimmy.Weight = kg75;

现在,如果我们这样做会发生什么:

Now what would happen if we do:

jimmy.Weight.Value = 82;

如果我们仍然使用相同的对象引用,那也会改变joe的权重. 请注意,我们为joe和jimmy分配了一个代表75kg的对象. 当jimmy变胖时,不是kg75对象发生了变化,而是jimmys体重发生了变化,因此,我们应该创建一个代表82 kg的新对象.

That would change the weight of joe too, if we are still using the same object references that is. Note that we assigned an object representing 75kg to both joe and jimmy. When jimmy gains weight, it is not the kg75 object that has changed, it is jimmys weight that has changed, thus, we should create a new object representing 82 kg.

但是如果我们有一个新的会话并将joe和jimmy都加载到干净的UoW中怎么办?

But what if we have a new session and load both joe and jimmy in a clean UoW?

 var joe = context.People.Where(p => p.Name = "joe").First();
 var jimmy = context.People.Where(p => p.Name = "jimmy").First();
 jimmy.Weight.Value = 82;

那会发生什么?好吧,由于在您的情况下EF4会加载joe和jimmy以及它们的权重而没有任何标识,因此我们将获得两个不同的权重对象,并且当我们更改jimmys的权重时,joe的权重仍将与以前相同.

What would happen then? well, since EF4 in your case would load joe and jimmy and their weights without any identity , we would get two different weight objects and when we change jimmys weight , joe would still weigh the same as before.

因此,对于同一代码,我们将有两种不同的行为. 如果对象引用仍然相同,则joe和jimmy都将获得新的权重. 如果将joe和jimmy放在干净的地方,那么其中只有一个会受到更改的影响.

So we would have two different behaviours for the same code. If the object references are still the same, then both joe and jimmy would get a new weight. If joe and jimmy are loaded in a clean uow, only one of them would be affected by the change.

那将是相当令人讨厌的imo.

And that would be quite incosistent imo.

通过使用不可变的VO,在两种情况下您将获得相同的行为,并且在构造对象图时仍可以重用对象引用以减少内存占用.

By using immutable VO's, you would get the same behavior in both cases and you can still reuse object references for a smaller memory footprint when constructing object graphs.

这篇关于重视DDD中的对象-为什么不可变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆