开发者

Adapters and Dependency Injection

I am currently building an MVC application in PHP (not using any frameworks). I am using yadif (https://github.com/beberlei/yadif) for dependency injection.

I would like to build a login module. It should be able to use adapters, for example one might be able to set that logins are authenticated using the MySql database or some LDAP directory. This setting would be done in the admin area and is stored in a database.

I imagine that I would have an abstract adapter:

<?php 
abstract AbstractLoginAdapter{
    abstract function login($username, $pass){}
}

I would then just implement adapters like so:

<?php
MySQLLoginAdapter extends AbstractLoginAdapter{

    public function login($username, $pass){
        //do stuff
    }
}

That's all nice and well, but how do I create an instance of the adapter? Usually, dependen开发者_运维知识库cies would be injected using yadif via the constructor:

<?php
class loginController{

    private $_adapter;

    public function __construct(AbstractLoginAdapter $adapter){
          $this->_adapter = $adapter;
    }
}

However, since I don't know which concrete adapter will be injected, I can't set that in a configuration before hand. Yadif allows me to create a configuration which I then need to pass to the container:

$builder = new Yadif_Builder();
$builder->bind("loginController")
    ->to("loginController")
    ->args($SelectedLoginAdapter);

Since the application uses a front controller, a DI container is created there. It then creates a routing object etc.

  • In light of this, should I pass a reference of that container down to the loginController object, and then use that container to instantiate my adapter?

  • Or should I instantiate a new container within my loginController object and then just load in an instance of the adapter?


I would do the first: pass a reference down to your controller. You'll want to use a single Dependency Injector Container (DIC) in your application. You don't want to create a new DIC whenever you need access to it. That would lead to duplication of objects stored in the DIC.

I know this is how Symfony 2 does it. All controllers (and many other classes) implement the ContainerAware interface. That interface has a single method setContainer() that is used to pass down a reference to the DIC.


I don't know about your specific DI tool but from a DI point of view you would be specifying which type to use. The container itself is responsible for instantiating a new instance (and possibly of all the dependencies of that type as well) of the configured type.

The benefit of DI in your example would be that you could deploy exactly the same code with a different configuration with 1 installation using LDAP and the other using MySQL authentication.


Refactor type hinting ("AbstractLoginAdapter") to ("MySQLLoginAdapter").

If you call abstract class method in the new __CLASS__ // Fatal Error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜