2.6. 依赖注入¶
2.6.1. 目的¶
实现了松耦合的软件架构,可得到更好的测试,管理和扩展的代码
2.6.2. 用例¶
DatabaseConfiguration
gets injected and DatabaseConnection
will get all that it
needs from $config
. Without DI, the configuration would be created
directly in DatabaseConnection
, which is not very good for testing and
extending it.
2.6.3. 例子¶
- Doctrine2 ORM 使用了依赖注入,它通过配置注入了
Connection
对象。为了达到方便测试的目的,可以很容易的通过配置创建一个mock的``Connection`` 对象。 - many frameworks already have containers for DI that create objects via a configuration array and inject them where needed (i.e. in Controllers)
2.6.4. UML 图¶

2.6.5. 代码¶
你可以在 GitHub 上找到这些代码
DatabaseConfiguration.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php declare(strict_types=1);
namespace DesignPatterns\Structural\DependencyInjection;
class DatabaseConfiguration
{
private string $host;
private int $port;
private string $username;
private string $password;
public function __construct(string $host, int $port, string $username, string $password)
{
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
}
public function getHost(): string
{
return $this->host;
}
public function getPort(): int
{
return $this->port;
}
public function getUsername(): string
{
return $this->username;
}
public function getPassword(): string
{
return $this->password;
}
}
|
DatabaseConnection.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php declare(strict_types=1);
namespace DesignPatterns\Structural\DependencyInjection;
class DatabaseConnection
{
private DatabaseConfiguration $configuration;
public function __construct(DatabaseConfiguration $config)
{
$this->configuration = $config;
}
public function getDsn(): string
{
// this is just for the sake of demonstration, not a real DSN
// notice that only the injected config is used here, so there is
// a real separation of concerns here
return sprintf(
'%s:%s@%s:%d',
$this->configuration->getUsername(),
$this->configuration->getPassword(),
$this->configuration->getHost(),
$this->configuration->getPort()
);
}
}
|
2.6.6. 测试¶
Tests/DependencyInjectionTest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php declare(strict_types=1);
namespace DesignPatterns\Structural\DependencyInjection\Tests;
use DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration;
use DesignPatterns\Structural\DependencyInjection\DatabaseConnection;
use PHPUnit\Framework\TestCase;
class DependencyInjectionTest extends TestCase
{
public function testDependencyInjection()
{
$config = new DatabaseConfiguration('localhost', 3306, 'domnikl', '1234');
$connection = new DatabaseConnection($config);
$this->assertSame('domnikl:1234@localhost:3306', $connection->getDsn());
}
}
|