依赖注入和对象初始化 [英] Dependency injection and initialization of the object

查看:166
本文介绍了依赖注入和对象初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是个不错的OOP和DI的新手,因此想知道,以下示例及其包含依赖对象的bootstrap类的初始化模式是否真的可以使用,例如:

 新会话(
新配置,
新数据库(
新配置
),
新页面(
新配置
),
新统计(
新数据库(
新配置

),
新通知,
新过滤器
);

我相信,那些熟悉DI的人可能会在bootstrap对象中对上面的那段代码说些什么?



它看起来有点笨重,但这还好吗?这样吗,我们可以称它为好/正确/可以接受

解决方案

经过研究新问题的调查,我终于得出结论,就我而言,最好的依赖注入容器将是:



Dice -用于PHP的简约依赖项注入容器。



它只有一个文件,为了拥有代码,这是我要初始化的问题,您需要做的只是:

 (新骰子)-> create('Session'); 

Dice DIC将负责其余的工作。您可以在汤姆·巴特勒(Tom Butler)(开发人员)主页上阅读有关 Dice 的更多信息。



我个人认为这是注入依赖项的最佳方式(最短,最简单),默认情况下应内置在PHP中,而我实际上想知道为什么不是这样? / p>




为了使代码与特定的依赖注入容器脱钩,我建议使用自定义类包装器:

 类DIC扩展骰子
{
函数__construct($ component,array $ args = array(), $ callback = null,$ forceNewInstance = false)
{
return parent :: create($ component,$ args,$ callback = null,$ forceNewInstance = false);
}
}

这也将有助于缩短初始化过程



现在,为了实例化复杂的DI集,例如:

 新会话(新配置,新数据库(新配置),新页面(新配置),新统计信息(新数据库(新配置)),新通知,新过滤器); 

所有您需要做的是:

  new DIC('Session'); 

如果您想将参数传递给构造函数,则可以使用:

  new DIC('Session',array($ param)); 

我不知道别人的想法,但我觉得这很神奇(至少到目前为止,我今天的位置)。



请问,对可能存在的弊端发表一些评论,我将来可能会使用这种DIC(骰子)或这种方法来面对!!


I am quite a newbie to decent OOP and DI, thus was wondering, if the following example and its pattern of initialization of my bootstrap class, that includes dependent objects, is really alright to use, e.g.:

new Session(
   new Config, 
   new Database ( 
       new Config 
   ), 
   new Page ( 
       new Config 
   ), 
   new Statistics ( 
      new Database ( 
         new Config 
      ) 
   ), 
   new Notification, 
   new Filter
);

I believe, those which familiar with DI could say something about the piece of code above in bootstrap object?

It looks a bit bulky but is this alright? Is this the way, that we could call it alright/correct/acceptable?

解决方案

After some time of learning an investigating on the new issue, I have finally came to the conclusion, that for my case, the best Dependency Injection Container would be:

Dice - minimalist Dependency Injection Container for PHP.

It has only one file and in order to have the code, that's from my question be initialized, all you need is:

(new Dice)->create('Session');

Dice DIC will take care of the rest. You can read more about Dice on the Tom Butler (developer) home page.

I personally think that this is the best (shortest and easiest) way of injecting dependencies and should be inbuilt in PHP by default, and I actually wonder why it's not?


In order to decouple your code from the particular dependency injection container, I'd better recommend using a custom class wrapper:

class DIC extends Dice
  {
    function __construct( $component, array $args = array(), $callback = null, $forceNewInstance = false )
      {
          return parent::create( $component, $args, $callback = null, $forceNewInstance = false );
      }
  }

which will also help to shorten even more the initialization process, to something really unbelievably amazing.

Now, in order to instantiate the complicated sets of DIs, like:

new Session( new Config, new Database ( new Config ), new Page ( new Config ), new Statistics ( new Database ( new Config ) ), new Notification, new Filter );

All you have to do is:

new DIC('Session');

If you would want to pass a parameter to the constructor, you could go with:

new DIC('Session', array($param));

I don't know what others think but I find this amazing (at least so far, of where I am today).

Ask you please, be kind commenting about possible drawbacks, that I could face in the future using this DIC (Dice) or this method in general!?

这篇关于依赖注入和对象初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆