Created
January 20, 2014 07:40
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
Yeah, my parent in task/client services must be abstract. Thanks!
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
The
base_service
service does not have any value here, the others makes sense.