How to work with dependency injection in SOA?

I am currently using SOA, I have a bunch of Services (ArticleService, CommentService, UserService, etc.)

I also have a ConfigurationService that populates from an XML configuration file.

I am using Zend Framework.

This configuration service is required in some of my services, and I use dependency injection - is it good practice to add ConfigurationService to the constructor of most of my services in order to be able to get global configuration?

Thanks for your feedback.

+3
source share
1 answer

, , config - , , Zend_Config - . ( ) , //, .

, , ArticleService / ArticleRepository ArticleMapper db. / ArticleService , .

, , Bootstrap, - factory - , - // (, , , , , db, ). factory , / , . factory , - , .

, , :

:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initFactory()
    {
        $factory = new My_Factory($this);
        return $factory;
    }
}

factory:

class My_Factory
{
    protected $_registry;

    protected $_bootstrap;

    public function __constructor($bootstrap)
    {
        $this->_bootstrap = $bootstrap;
    }

    public function getDbAdapter()
    {
       if (!isset($this->_registry['dbAdapter']){
           $this->_bootstrap->bootstrap('db');  // probably using app resource
           $this->_registry['dbAdapter'] = $This->_bootstrap->getResource('db');
       }
       return $this->_registry['dbAdapter'];

    }

    public function getArticleService()
    {
       if (!isset($this->_registry['articleService']){
           $dbAdapter = $this->getDbAdapter();
           $this->_registry['articleService'] = new My_ArticleService($dbAdapter);
       }
       return $this->_registry['articleService'];
    }

    public function getTwitterService()
    {
       if (!isset($this->_registry['twitterService']){
           $options = $this->_bootstrap->getOptions();
           $user = $options['twitter']['user'];
           $pass = $options['twitter']['pass'];
           $this->_registry['twitterService'] = new My_TwitterService($user, $pass);
       }
       return $this->_registry['twitterService'];
    }
}

ArticleService:

class SomeController extends Zend_Controller_Action
{
    protected $_factory;

    public function init()
    {
        $this->_factory = $this->getInvokeArg('bootstrap')->getResource('factory');
    }

    public function someAction()
    {
        $articleService = $this->_factory->getArticleService();
        $this->view->articles = $articleService->getRecentArticles(5);  // for example
    }

}

, , factory - , / .

, , spitballing . , , ; , DIC - , Symbian DIC Zend\Di ZF2 - . , , . , (!) .; -)

+3

All Articles