Skip to content

Instantly share code, notes, and snippets.

@darles
Created January 20, 2014 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darles/8516410 to your computer and use it in GitHub Desktop.
Save darles/8516410 to your computer and use it in GitHub Desktop.
Situation: I have few entities (task, client, ...) most of them needs same functionality like search by id, create, save, delete. Firstly I created services for each of them, and all services looked same (with same methods). So know I tried to use parent service to optimize code duplication. Is this the right way to use parent services?
<?php
...
public function __construct(EntityManager $em, Paginator $paginator, EventDispatcherInterface $eventDispatcher, Router $router, $itemsPerPage)
{
$this->em = $em;
$this->paginator = $paginator;
$this->eventDispatcher = $eventDispatcher;
$this->router = $router;
$this->itemsPerPage = $itemsPerPage;
}
public function setClass($class)
{
$this->class = $class;
}
public function setRepo($repo)
{
$this->repo = $repo;
}
public function get($id)
{
return $this->repo->find($id);
}
// Other methods
...
<?php
...
public function __construct(EntityManager $em, Paginator $paginator, EventDispatcherInterface $eventDispatcher, Router $router, $itemsPerPage, EntityRepository $repo)
{
parent::__construct($em, $paginator, $eventDispatcher, $router, $itemsPerPage);
$this->setClass('Client');
$this->setRepo($repo);
}
<service id="abstract_base_service" abstract="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="knp_paginator"></argument>
<argument type="service" id="event_dispatcher"></argument>
<argument type="service" id="router"></argument>
<argument type="string">%paginator.items_per_page%</argument>
</service>
<service id="base_service" class="%base_service.class%" parent="abstract_base_service" />
<service id="task_service" class="%task_service.class%" parent="base_service">
<argument type="service" id="task.repository"></argument>
</service>
<service id="client_service" class="%client_service.class%" parent="base_service">
<argument type="service" id="client.repository"></argument>
</service>
...
<?php
...
public function __construct(EntityManager $em, Paginator $paginator, EventDispatcherInterface $eventDispatcher, Router $router, $itemsPerPage, EntityRepository $repo)
{
parent::__construct($em, $paginator, $eventDispatcher, $router, $itemsPerPage);
$this->setClass('Task');
$this->setRepo($repo);
}
@matthiasnoback
Copy link

The base_service service does not have any value here, the others makes sense.

@darles
Copy link
Author

darles commented Jan 20, 2014

Yeah, my parent in task/client services must be abstract. Thanks!

@l3pp4rd
Copy link

l3pp4rd commented Jan 20, 2014

I'd use a DIC extension to build these services based on a skeleton. See how doctrine does it. Inheritance is always the worst choice over composition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment