Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?
share|improve this question
    
Just a feeling, but this sounds like a 100% CRUD application. –  Songo Oct 12 '14 at 17:10
    
I really can't see how you can draw that conclusion based on the question... –  JDT Mar 11 at 19:20

2 Answers 2

I am not an expert on this subject, but will try an answer. According to this article the Repository pattern is specifically designed to solve your problem. So in my understanding you should have one repository that will return the concrete or abstract Animal required.

share|improve this answer

We can solve any problem by introducing an extra level of indirection. I doubt that adding another level of abstraction would solve your problem.

I think the question you should be asking is 'do I need two repositories for this or just one' and the answer to this depends on a lot more than can be found here... No harm in starting off with the one Dog repository and moving off into separate repositories when it becomes clear that the Dog repository needs to have methods that are only valid for Huskies and your program structure would benefit from the separation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.