0

I am trying to better implement OOP and dependency injection in my code and come across the below issue.

I provide services to clients where an employer and a company are involved (with corresponding models, mappers and database tables):

class Service
{
    protected $clientId;
    protected $client;
    protected $employerId;
    protected $employer;
    protected $companyId;
    protected $company;

    public function setClient(Client $client)
    {
       $this->client = $client;       
    }

    public function setEmployer(Employer $client)
    {
        $this->employer = $employer;
    }

    public function setCompany(Company $company)
    {
        $this->company = $company;
    }

    // more
}

To get a Service object I first instantiate the Service object which returns a clientId from the database. With the clientId I instantiate a Client object (and attach it to the Service) which involves going to the database again. Same for Employer and Company.

I could just retrieve service, client, employer and company in one go from the database with joins but that would make my mapper(s) more complex. E.g. clients, employers and companies all have addresses, so I need to alias these columns and map them to the respective models. That is less clean than just retrieve all columns from each table separately and map them to each model individually (e.g. with some logic to translate underscored columns to ZF camelCase), reusing my Client, Employer and Company Mappers.

Is there a best practice solution or is it up to personal preference and circumstances (performance vs maintainability)?

tihe
  • 2,452
  • 3
  • 25
  • 27

1 Answers1

1

First, there's a great article on PHP dependency injection that I think is Worth Checking Out. Secondly, your question isn't really one about dependency injection, as the main questions you have are all about how many times you should be querying your database.

If you're injecting the objects into Service, it doesn't really matter if you call them all at once, or one after the other. The real question is just "can I afford to hit the database multiple times". The big advantage of Dependency Injection is that you can change the source of the object being provided easily (so if you have a database helper class or something like that, dependency injection is great for changeable database objects inside).

You have to just ask yourself the regular MySQL questions here for collecting that information (performance v. maintainability). Realistically though if you have a relational table, the whole point of that is to be able to query it with joins, and map out those tables.

You can take a look at how other ORM's hydrate their objects from the database, but you're the one who knows your application best. Maintainability SHOULDNT be a huge concern here, since you have things nicely abstracted.

If your DB changes you only need to change your mapping objects, and if your business logic changes, you don't need to touch your DBAL.

DaOgre
  • 2,080
  • 16
  • 25