Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain Objects and Value Objects - are they equal?

By looking to the example of a Domain Object into Zend Quickstart tutorial, and other examples considering a DAO/VO patterns, they both seem to be very similar.

Can we deduce that to say "Value Object" is the same as to say "Domain Object" ?

If not, can you please clarify the differences between those?

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

I'm asking this because, both are composed by getters and setters and nothing more then that. It seems that, they do the same function...

Update:

So, 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 face of a "Anemic Domain Object" ?

2) Is it called "domain object" just because it contains domain logic ?

3) If this is the case, then, those mappers containing methods like findBookByAuthor(); they are also dealing with domain logic right? Could they be considered domain objects as well ?

Thanks a lot

like image 947
MEM Avatar asked May 02 '11 19:05

MEM


People also ask

Are value objects part of domain?

An object that represents a descriptive aspect of the domain with no conceptual identity is called a Value Object. Value Objects are instantiated to represent elements of the design that we care about only for what they are, not who or which they are. Think back to the student example in the Entities section.

Is value objects different from reference objects?

A reference is an address (pointer) to where the value of the object resides. A value is the actual value the binary representation. If you assign a value you are making a copy of the value into the new variable. If you assign a reference you are just passing the address of where the value is saved.

What is the difference between an entity and a value object?

In terms of immutability, the difference between entities and value object is that value objects are immutable, whereas entities are almost always mutable. You don't modify a value object; instead, you create a new one and replace the old instance with it.

What is a value object in domain driven design?

Value Object is an object that represents a concept from your problem Domain. It is important in DDD that Value Objects support and enrich Ubiquitous Language of your Domain. They are not just primitives that represent some values - they are domain citizens that model behaviour of your application.


1 Answers

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".

like image 175
Terry Wilcox Avatar answered Oct 12 '22 03:10

Terry Wilcox