依赖注入容器_如何构建自己的依赖注入容器

论坛 期权论坛     
选择匿名的用户   2021-5-22 16:35   53   0
<section>
<p>依赖注入容器</p>
<p>A search for <a href="https://packagist.org/search/?q&#61;dependency%20injection%20container">“dependency injection container” on packagist</a> currently provides over 95 pages of results. It is safe to say that this particular “wheel” has been invented.</p>
<p> <a href="https://packagist.org/search/?q&#61;dependency%20injection%20container">在packagist上</a>搜索<a href="https://packagist.org/search/?q&#61;dependency%20injection%20container">“依赖注入容器”</a>目前可提供95页以上的结果。 可以肯定地说,已经发明了这种特殊的“轮子”。 </p>
<div style="text-align: center;">
  <img alt="Square wheel?" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-57f701ec974ab0df89b8fcae55cb71af.jpg" style="outline: none;">
</div>
<p>However, no chef ever learned to cook using only ready meals. Likewise, <strong>no developer ever learned programming using only “ready code”</strong>.</p>
<p> 然而,没有厨师学会只用即食做饭。 同样, <strong>没有开发人员曾经只使用“就绪代码”来学习编程</strong> 。 </p>
<p>In this article, we are going to learn how to make a simple dependency injection container package. All of the code written in this article, plus PHPDoc annotations and unit tests with 100% coverage is available <a href="https://github.com/sitepoint/Container">at this GitHub repository</a>. It is also listed on <a href="https://packagist.org/packages/sitepoint/container">Packagist</a>.</p>
<p> 在本文中,我们将学习如何制作一个简单的依赖项注入容器包。 <a href="https://github.com/sitepoint/Container">该GitHub存储库中</a>提供<a href="https://github.com/sitepoint/Container">了</a>本文编写的所有代码,以及覆盖率100%PHPDoc注释和单元测试。 它也在<a href="https://packagist.org/packages/sitepoint/container">Packagist</a>上<a href="https://packagist.org/packages/sitepoint/container">列出</a> 。 </p>
<h2 id="planning-our-dependency-injection-container"> 规划我们的依赖注入容器 <span style="font-weight: bold;">(</span>Planning Our Dependency Injection Container<span style="font-weight: bold;">)</span></h2>
<p>Let us start by planning what it is that we want our container to do. A good start is to split “Dependency Injection Container” into two roles, “Dependency Injection” and “Container”.</p>
<p> 让我们从计划容器要做什么开始。 一个好的开始是将“依赖注入容器”划分为两个角色,“依赖注入”和“容器”。 </p>
<p>The two most common methods for accomplishing dependency injection is through <strong>constructor injection</strong> or <strong>setter injection</strong>. That is, passing class dependencies through constructor arguments or method calls. If our container is going to be able to instantiate and contain services, it needs to be able to do both of these.</p>
<p> 完成依赖项注入的两种最常见方法是通过<strong>构造函数注入</strong>或<strong>setter注入</strong> 。 也就是说,通过构造函数参数或方法调用传递类依赖关系。 如果我们的容器能够实例化并包含服务,则它必须能够同时执行这两项。 </p>
<p>To be a container, it has to be able to store and retrieve instances of services. This is quite a trivial task compared to creating the services, but it is still worth some consideration. The <a href="https://github.com/container-interop/container-interop">container-interop</a> package provides a set of interfaces that containers can implement. The primary interface is the <code>ContainerInterface</code> that defines two methods, one for retrieving a service and one for testing if a service has been defined.</p>
<p> 要成为容器,它必须能够存储和检索服务实例。 与创建服务相比,这是一项微不足道的任务,但是仍然值得考虑。 <a href="https://github.com/container-interop/container-interop">container-interop</a>软件包提供了一组容器可以实现的接口。 主要接口是<code>ContainerInterface</code> ,它定义了两种方法,一种用于检索服务,另一种用于测试是否已定义服务。 </p>
<pre class="blockcode" tabindex="0"><code class="prism  language-php">interface ContainerInterface
{
    public function get($id);
    public function has($id);
}</code></pre>
<h2 id="learning-from-other-dependency-injection-containers"> 向其他依赖注入容器学习 <span style="font-weight: bold;">(</span>Learning From Other Dependency Injection Containers<span style="font-weight: bold;">)</span></h2>
<p>The <a href="http://symfony.com/doc/current/components/dependency_injection/introduction.html">Symfony Dependency Injection Container</a> allows us to define services in a variety of different ways. In YAML, the configuration for a container might look like this:</p>
<p> <a href="http://symfony.com/doc/current/components/dependency_injection/introduction.html">Symfony依赖注入容器</a>允许我们以各种不同的方式定义服务。 在YAML中,容器的配置可能如下所示: </p>
<pre class="blockcode" tabindex="0"><code class="prism  language-yaml">parameters:
    # ...
    mailer.transport: sendmail

services:
    mailer:
        class:     Mailer
        arguments:
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP