-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinversion-of-control.html
119 lines (101 loc) · 3.57 KB
/
inversion-of-control.html
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Inversion of control and dependency injection</title>
<link rel="stylesheet" href="css/css.css" type="text/css" media="all">
<link rel="stylesheet" href="css/prism.css" type="text/css" media="all">
<link rel="stylesheet" href="css/latex.css" type="text/css" media="all">
</head>
<body>
<h1 id="inversion-of-control-and-dependency-injection">Inversion of control and dependency injection</h1>
<blockquote>
<p>Inversion of control (IoC) principle - Objects <strong>do not create other objects</strong>
on which they rely. Instead, they they get objects that they need
from an outside source.</p>
</blockquote>
<h2 id="what-is-an-dependency%3F">What is an dependency?</h2>
<p>When class <code>A</code> needs some copy or instance of class <code>B</code> inside of it
for it's operation to be successful,
then we say that class <code>A</code> depends on class <code>B</code>.</p>
<h2 id="typical-procedural-implementation">Typical procedural implementation</h2>
<p>Here <code>User</code> class depends on <code>MYSQLDatabase</code>.</p>
<pre><code class="language-php"><?php
class User {
protected MYSQLDatabase $database;
public function __construct() {
$this->database = new MYSQLDatabase();
}
public function save() {
$this->database->sql('UPDATE WHERE etc..');
}
}
$user = new User();
$user->save();
</code></pre>
<p>Couple of problems with this:</p>
<ul>
<li>impossible to write unit tests for <code>User</code> class (cannot pass in a mock instance of database)</li>
<li>tight coupling between between <code>User</code> and <code>MYSQLDatabase</code> objects</li>
<li>depending on concrete implementation</li>
</ul>
<h2 id="inversion-of-control">Inversion of control</h2>
<p>All we need to do here is to invert the <em>control flow</em>.
Instead of <code>User</code> class handling the creation and life-cycle of
this database object, initialize the database instance
somewhere higher up the <em>dependency graph</em>,
then pass it down as a parameter (inject).</p>
<pre><code class="language-php"><?php
class User {
protected MYSQLDatabase $database;
public function __construct(MYSQLDatabase $database) {
$this->database = $database;
}
public function save() {
$this->database->sql('UPDATE WHERE etc..');
}
}
$database = new MYSQLDatabase();
$user = new User($database);
$user->save();
</code></pre>
<p>Now we can unit test the internals of this class
by passing in a <em>mock</em> instance of database.</p>
<p>Still, couple of problems with this:</p>
<ul>
<li>hadrcoded assumption about the database used</li>
<li><code>User</code> is hinged on concrete implementation</li>
</ul>
<blockquote>
<p>IoC principle asks us to <strong>rely on abstractions</strong>
rather than concrete implementations.</p>
</blockquote>
<pre><code class="language-php"><?php
interface Database {
public function sql(string $sql);
}
class OracleDatabase implements Database {
// ..
}
class MYSQLDatabase implements Database {
// ..
}
class User {
protected Database $database;
public function __construct(Database $database) {
$this->database = $database;
}
public function save() {
$this->database->sql('UPDATE WHERE etc..');
}
}
$database = new OracleDatabase();
$user = new User($database);
$user->save();
$database = new MYSQLDatabase();
$user = new User($database);
$user->save();
</code></pre>
<script src="js/prism.js"></script>
</body>
</html>