Domain Objects and Value Objects - are they equal?

此生再无相见时 提交于 2019-12-04 04:42:26

Typically a value object encapsulates something that has a value: currency, dates, temperature, etc. They may contain a value and units, but they're not complex.

A domain object is likely to be more complex (unless it's an Anemic Domain Object, which is a bunch of getters and setters pretending to be a domain object) because it contains domain logic.

For example, you might have an Invoice domain object that contains many Invoice Lines (a line for each Invoice Item), and each Invoice Line might have a Net Amount, a Tax Amount, and an Invoice Item. The Amounts and maybe Invoice Item would typically be Value Objects and be reasonably simple.

The Invoice itself could be complicated with interest rates for late payment, support for an approval process, or support for your accounting system.

The Value Object is simple enough to be reusable across different domains. The Domain Objects model your actual domain and are typically written to model your specific business or domain, including your business logic.

The reason you'll often see little difference between them is that many developers will use a Transaction Script/Data Transfer Object design, but call it a Domain Model. They label their collections of getters and setters "domain objects".

They can be the same thing. And in many cases they are. However:

  • domain objects can perform business logic (according to domain-driven design at least), value objects can't
  • domain objects have the whole information, while value objects hold only part of the information that is relevant to its consumer.

For example, in case of an Invoice domain object, it will be same as the value object, and then you can use the same class for both - it will have an invoice number, ordered items, total price.

On the other hand, a User domain object will have a password field and email field, which you want to be able to process within your system, but you should never send to other systems. Hence you need a new, value object, that lacks these two fields.

Building on the previous responses, I am of the opinion that they are not the same:

  1. Domain objects can contain business logic. They represent entities in the problem space, their property values may be changed and are identified by a unique ID.
  2. According to the Gang of Four, Value Objects are immutable. Such objects are not identified by any ID, but instead by their value.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!