Domain objects and value objects - are they equal?

Looking at the Zend Quickstart Domain object example and other examples regarding DAO / VO patterns, they seem very similar.

Can we conclude that the “Value Object” is the same as saying “Domain Object”?

If not, can you clarify the differences between the two?

What is the function of one, and what if the function of the other?

I ask about this because both of them are made up of getters and setters and nothing more. They seem to perform the same function ...

Update:

So, the Zend Framework Quick Tutorial documentation called this a domain object:

// application/models/Guestbook.php class Application_Model_Guestbook { protected $_comment; protected $_created; protected $_email; protected $_id; public function __construct(array $options = null) { if (is_array($options)) { $this->setOptions($options); } } public function __set($name, $value) { $method = 'set' . $name; if (('mapper' == $name) || !method_exists($this, $method)) { throw new Exception('Invalid guestbook property'); } $this->$method($value); } public function __get($name) { $method = 'get' . $name; if (('mapper' == $name) || !method_exists($this, $method)) { throw new Exception('Invalid guestbook property'); } return $this->$method(); } public function setOptions(array $options) { $methods = get_class_methods($this); foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (in_array($method, $methods)) { $this->$method($value); } } return $this; } public function setComment($text) { $this->_comment = (string) $text; return $this; } public function getComment() { return $this->_comment; } public function setEmail($email) { $this->_email = (string) $email; return $this; } public function getEmail() { return $this->_email; } public function setCreated($ts) { $this->_created = $ts; return $this; } public function getCreated() { return $this->_created; } public function setId($id) { $this->_id = (int) $id; return $this; } public function getId() { return $this->_id; } } 

1) Strictly speaking, are we in the face of an “anemic domain object”?

2) Is it called a “domain object” just because it contains domain logic?

3) If so, then those that contain methods such as findBookByAuthor (); Do they also deal with domain logic? Can they be considered domain objects?

thanks a lot

+4
source share
3 answers

Typically, a value object encapsulates a value that matters: currency, dates, temperature, etc. They may contain meaning and units, but they are not complex.

A domain object is likely to be more complex (unless it is an anonymous domain object, which is a bunch of getters and setters claiming 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 element), and each invoice line may have a net amount, tax amount, and an invoice element. The amount and, possibly, the invoice are usually Value objects and can be quite simple.

The Account itself may be complicated by interest rates for late payments, supporting the approval process, or supporting your accounting system.

The Value object is simple enough to be reusable across domains. Domain objects simulate your actual domain and are usually written to simulate your particular business or domain, including your business logic.

The reason you often find a small difference between the two is because many developers will use the Transaction Script / Data Transfer Object project, but call it a domain model. They label their collections of getters and setters with "domain items."

+7
source

They may be the same. And in many cases they are. But:

  • domain objects can execute business logic (at least according to a slave domain), value objects cannot domain objects
  • have all the information, and value objects contain only a part of the information related to its consumer.

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

On the other hand, the User domain object will have a password field and an email field that you want to process on your system, but you should never send it to other systems. Therefore, you need a new value object that does not have these two fields.

+2
source

Based on the previous answers, I believe that they are not the same:

  • Domain objects may contain business logic. They represent objects in a problem space; their property values ​​can be changed and identified using a unique identifier.
  • According to the gang of four objects, Value objects are immutable . Such objects are not identified by any ID, but instead by their value.
+2
source

All Articles