I'm trying really hard to refactor some of my legacy code in the project using best practices and design patterns + DDD so I'd love some feedback on an issue I'm currently having.
Let's assume that I have two entity classes:
class Dog
{
protected $name;
function __construct($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
function bark()
{
echo 'Rawr';
}
}
class Husky extends Dog
{
/**
* @var Sledge
*/
protected $sledge;
function __construct($name, Sledge $slegde)
{
parent::__construct($name);
$this->sledge = $slegde;
}
function pull()
{
echo $this->sledge->pull();
}
}
Dog
is my regular entity and his only responsibility is to map database fields.
Husky
on the other hand has same responsibilities as dog but also delegates Sledge
pulling.
Normally, both entities would have different repositories (should they? Since one inherits from one another) to call for, however business requirement implies that client does not have to specify dog's type (so it can either be a "basic" Dog
or special Husky
), just its name http://localhost/animal/fluffy
.
What is more, currently they both reside in the same database table (recognized by type
field) and right now there are no technical plans to change that (performance and time reasons).
What is the best way to do it?
- Should I create some
AnimalRepository
, pull the data and treat it as DTO, detect it's type and then create appropriate class? - Should I create some kind of higher abstraction level mapper? How should it look like?