I'm having my User
entity value objects loosely coupled, and because of that I use a UserFactory
to build the object whenever it comes from the database or when creating a entirely new entity to the domain.
Would it be okay to tightly couple the value objects so I can get rid of its Factory and having my Application Services bloated with individual value object instantiation logic (before being able to inject them) when updating the entity its properties? Aren't value objects tightly related to their root anyways?
For example, when I update one of the properties with the loosely coupled version I would have to instantiate the value object first, and then inject it. But in the tightly coupled example I would be able to just enter the new values directly without having to explicitly go through the process of instantiating the VOs.
Example:
// Updating User's name (loosely coupled version)
$firstName = new FirstName('John');
$lastName = new LastName('Doe');
$fullName = new FullName($firstName, $lastName);
$user->setFullName($fullName);
// Updating User's name (tightly coupled version)
$user->setFullName('John', 'Doe');
Loosely coupled:
class User extends Entity
{
private $fullName;
private $email;
public function getFullName()
{
return $this->fullName;
}
public function setFullName(FullName $fullName)
{
$this->fullName = $fullName;
return $this;
}
public function getEmail()
{
return (string) $this->email;
}
public function setEmail(Email $email)
{
$this->email = $email;
return $this;
}
// etc.
}
Tightly coupled:
class User extends Entity
{
private $fullName;
private $email;
public function getFullName()
{
return $this->fullName;
}
public function setFullName($firstName, $lastName)
{
$firstName = new FirstName($firstName);
$lastName = new LastName($lastName);
$this->fullName = new FullName($firstName, $lastName);
return $this;
}
public function getEmail()
{
return (string) $this->email;
}
public function setEmail($email)
{
$this->email = new Email($email);
return $this;
}
// etc.
}