DEV Community

mohamed ahmed
mohamed ahmed

Posted on

What is a Value Object

What is the most annoying thing when you're working with legacy code written by php as example? there's a lot of annoying thing, look at this method :

public function filter($data)
{
    // filter data
}
Enter fullscreen mode Exit fullscreen mode

first thing, we cant detact $data type so we need to jump to the filter method involving to know the type of $data . and we need to know what inside $data . and we need to read filter method to know if $data altered or not. php is weak type language sothat we can change $data type if it is array we can make it int. so this is another problem.

value object come to solve this problems to us.

value object is a small object that represents a simple entity whose equality is not based on identity: two value objects are equal when they have the same value, not necessarily being the same object. from wiki.

Eric Evans define value object like this, 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.

Value objects are used to represent some undividable concept through the app. like, a customer address or currency. Instead of throwing scalars between classes that cause alot of problem, we send value object. When you receive a value object you don't need to validate it, it is valid by default. The other important thing of value object is immutability. so you pass objects to classes and you be sure this objects not altered.

The benefits of Value Objects:
1- everything is type hinted. so we send value object to class and we know the type.
2- Built in validation, An object contains built-in validators which enforce its internal format. like, an address must have city, country and street. Whenever you instantiate a new address object, you must give it valid data otherwise the object instantiation fails.
3- immutability An object cannot be changed by anyone. once created it can only be cloned, thus the logic who created the object is confident that the data is never altered.
4- no identity, a corollary of value objects identity-less nature is, obviously, not having an Id property. Unlike entities, value objects should be compared by value, not by identity field.

php example :

final class Address
{
    public function __construct(
        private string $country,
        private string $city,
        private string $street,
        private int    $buildingNumber,
        private int    $flatNumber
    )
    {
        if ($this->country != 'USA') {
            throw new \InvalidArgumentException("Not supported country: {$this->country}.");
        }
    }

    public function getCountry(){
        return $this->country;
    }

    public function isValidateCountry(string $country){
        return $this->country === $country;
    }
} 
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
khairallah profile image
Khairallah AL-Awady

Inspirational article.

Thank you for writing this, Mohamed! :D

Collapse
 
mohamedahmed00 profile image
mohamed ahmed

You are welcome