CQRS中的值对象-在何处使用 [英] Value Objects in CQRS - where to use

查看:78
本文介绍了CQRS中的值对象-在何处使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们拥有受CQRS启发的体系结构,其中包含命令,域模型,域事件,读取模型DTO等组件.
当然,我们可以在域模型中使用值对象.我的问题是,它们是否也应用于:

Let's say we have CQRS-inspired architecture, with components such as Commands, Domain Model, Domain Events, Read Model DTOs.
Of course, we can use Value Objects in our Domain Model. My question is, should they also be used in:

  1. 命令
  2. 事件
  3. DTO

我还没有看到在上述组件中使用值对象(VO)的任何示例.而是使用原始类型.也许仅仅是简单的例子.毕竟,我对在DDD中使用VO的理解是,它们是整个应用程序的粘合剂.

I haven't seen any examples where Value Objects (VO) are used in the components mentioned above. Instead, primitive types are used. Maybe it's just the simplistic examples. After all, my understanding of VOs use in DDD is that they act as a glue for the whole application.

我的动机:

命令.
假设用户提交了一个包含地址字段的表单.我们有地址值对象来表示这个概念.在客户端中构造Command时,无论如何我们都应该验证用户输入,并且当其格式正确时,我们可以在那里创建Address对象并使用它初始化Command.我看到不需要将Address对象的创建委派给命令处理程序.

Commands.
Let's say user submits a form which contains address fields. We have Address Value Object to represent this concept. When constructing command in the client, we should validate user input anyway, and when it is well-formed, we can create Address object right there and initialize Command with it. I see no need to delegate creation of Address object to command handler.

域事件.
域模型已经根据值对象进行操作,因此通过使用VO发布事件而不是将其转换为原始类型,我们可以避免使用某些映射代码.我很确定在这种情况下可以使用VO.

Domain Events.
Domain Model already operates in terms of Value Objects, so by publishing events with VOs instead of converting them to primitive types, we can avoid some mapping code. I'm pretty sure it's alright to use VOs in this case.

DTO.
如果我们的查询端DTO可以包含值对象,则可以提供更多的灵活性.例如,如果我们有Money对象,则可以选择以EUR或USD显示它,而无需更改读取模型.

DTOs.
If our query-side DTOs can contain Value Objects, this allows for some more flexibility. E.g., if we have Money object, we can choose whether to display it in EUR or USD, no need to change Read Model.

推荐答案

好,我改变了主意.我最近一直在尝试观看VO,并且观看了 http://www.infoq .com/presentations/Value-Objects-Dan-Bergh-Johnsson 它为我澄清了几件事.

Ok I've changed my mind. I have been trying to deal with VOs a bunch lately and after watching this http://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson it clarified a couple of things for me.

命令和事件是消息(而不是对象,对象是数据+行为),在某些方面与DTO一样,它们传达有关事件的数据,并且它们本身不封装任何行为.

Commands and Event are messages (and not objects, objects are data + behavior), in some respects much like DTOs, they communicate data about an event and they themselves encapsulate no behavior.

值对象根本不像DTO.它们是一个域表示形式,通常来说,它们像其他所有域表示形式一样,在行为上也很丰富.

Value Objects are not like DTOs at all. They are a domain representation and they are, generally speaking, rich on behavior like all other domain representations.

命令和事件分别将信息传递到域中或从域中传递出去,但是它们本身并不封装任何行为.从这个角度看,传递VO到内部似乎是错误的,并且可能是违反上下文.

Commands and Events communicate information into and out of the domain respectively, but they themselves do not encapsulate any behavior. From that perspective it seems wrong and a possibly a violation context boundaries to pass VOs inside of them.

对奥伦(尽管他指的是nHibernate和WCF)进行解释:不要通过网线发送您的域". http://ayende.com/Blog/archive/2009/05/14/the-stripper-pattern.aspx

To paraphrase Oren (though he was referring to nHibernate and WCF) "Don't send your domain across the wire". http://ayende.com/Blog/archive/2009/05/14/the-stripper-pattern.aspx

如果您想传达一个价值对象,那么我建议您传递在其中重建VO所需的必要属性.

If you want to communicate a value object, then I suggest passing the necessary attributes needed to re-construct the VO within them instead.

原始文本(供后代使用):

Original Text (for posterity):

如果您要问域模型可以将值对象传递给事件还是可以将命令传递给对象,那么我并不认为前者有什么大问题,尽管后者可能违反了聚合的某些规则根是价值的所有者".

If you are asking if Value Objects can be passed by the domain model to events or passed in by commands, I don't really see a huge problem with the former, though the latter may violate some of the rules of the aggregate root being the "owner" of values.

该值对象表示诸如颜色之类的概念.您没有绿色,您绿色.通过传递此命令告诉您绿色的命令,似乎没有本质上的错误.

That said a value object represents concepts like for example a color. You don't have green, you are green or not. There seems to be nothing intrinsically wrong with a command telling you that you are green by passing this.

从DDD中读取有关聚合根模式的章节可以很好地解释实体和值对象,值得多次阅读.

Reading the chapter from DDD on the Aggregate Root pattern explains Entities and Value Objects quite well and is worth reading over a few times.

这篇关于CQRS中的值对象-在何处使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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