-
Notifications
You must be signed in to change notification settings - Fork 4
static #1
Comments
+1 |
The problem is, that we can't give a context to the ZF2 DI. I do know mess generated by static call like Zend_Registry. If you wan't to get the context, you have to get your service locator from your controller, and so degrade your factory signature with Service Locator for instanciation from a controller. For example, a factory of yours allow you to get a cache version, and a normal one with decorator pattern.
If you wan't to use that kind of architecture, how can you with actual DI without making mess in your controller? (It's a real question, i'm interrested! :) ) |
That's not true. The factory gets the actual ServiceManager instance already. If your architecture is setup correctly, there's no need to fetch this statically. It was made difficult on purpose, so you'd know that your application architecture is wrong. Update |
Indeed, it do. but the real question is the context, not simply get the ServiceManager. How can you inject context to your Factory ? |
I have no idea what you're talking about. Could you try explaining it? No offense but your English is a bit difficult to understand. |
No problem about it, i do know about my weird english. Imagin that you want to choose in one of your service, for example a data access object,on wich source of data it will be plug, depeding on context. In on case you would like to get data from a local source (a database), in other one from a distant webservice. The service manager allow us to get our service where ever we want to get it, that great, but it dosn't allow us to set option for the createService() method. If I want to be sure that only dependancy for the source I will use will be inject, how can I set it up?
|
In your module.config.php: <?php
return array(
'service_manager' => array(
'factories' => array(
'Corp\News\NewsDAOFactory' => function($sm) {
return new \Corp\News\NewsDAOFactory($sm);
}
),
),
); In your NewsDAOFactory.php: <?php
namespace Corp\News;
use Corp\News\DAO\NewsWebservice;
use Corp\News\DAO\NewsDatabase;
use Corp\News\NewsDAOInterface;
use Zend\ServiceManager\ServiceManager;
use ServiceLocatorFactory\ServiceLocatorFactory;
class NewsDAOFactory
{
protected $serviceManager;
public function __construct($serviceManager)
{
$this->serviceManager = $serviceManager;
}
} Now, if you do You could also make your "factory" implement the interface to have it set, and add the module as an invokable. Factories: Interface: |
Yea this is a bad idea, it goes against DI. Instead of tightly coupling your object to their dependencies, you're tightly coupling your objectes to the serviceManager. Like @RWOverdijk illustrated - use injection. |
I'm look at your example And I see, that i need If you want to tell me about refactoring code and make it more flatten (not have many deep levels) - architecture became around So, for me problem not in options for factories, but in accessing |
for example I have Also inside this method I need not to interact with So, in my example I have places in code, where I need from And I need call |
example pseudo code <?php
class Helper {
public static method findSomethingById($id) {
//prepare and check code
//...
$model = Registy::getModel();
$found = $model->find($id);
return $found;
}
public static updateSomething($id, $data) {
$model = Registy::getModel();
$result = $model->update($id, $data);
return $result;
}
}
class Registry {
public static getModel() {
//there I call ServiceLocator
$sm = ServiceLocatorFactory::getInstance();
$model = $sm->get('mysqlModel'); //or mongoModel
$model->setSM($sm);
//or call factory and factory use config inside, but I need ServiceLocator to call factory and inject it to model :)
//use config
$model = $sm->get('modelFactory');
return $model;
}
}
class DoctrineModel {
public function setSM($sm) {
$this->sm = $sm;
}
public function find ($id) {
//there I need $sm
$repo = $this->sm->get(Doctrine\EntityManager)->getRepository('blahblah');
return $repo->find($id);
}
}
//and client code
class SomeClient {
private finction someMethod() {
$id = '1';
$entity = Helper::findSomethingById($id);
}
} How solve it without static call? ) |
There's a reason why the servicemanager can't be accessed statically. (It can, but we're not advertising it). It generates a mess (remember Zend_Registry?)
The text was updated successfully, but these errors were encountered: