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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
<html>
<head>
<title>Needle Manual :: Chapter 4: Dependency Injection</title>
<link type="text/css" rel="stylesheet" href="manual.css" />
</head>
<body>
<div id="banner">
<table border='0' cellpadding='0' cellspacing='0' width='100%'>
<tr><td valign='top' align='left'>
<div class="title">
<span class="product">Needle—</span><br />
<span class="tagline">to the point --></span>
</div>
</td><td valign='middle' align='right'>
<div class="info">
Needle Version: <strong>1.2.0</strong><br />
Manual Last Updated: <strong>2004-11-18 15:36 GMT</strong>
</div>
</td></tr>
</table>
</div>
<table border='0' width='100%' cellpadding='0' cellspacing='0'>
<tr><td valign='top'>
<div id="navigation">
<h1>Needle Manual</h1>
<h2>Chapters</h2>
<ol type="I">
<li>
<a href="chapter-1.html">
Introduction
</a>
<ol type="1">
<li><a href="chapter-1.html#s1">What is Needle?</a></li>
<li><a href="chapter-1.html#s2">How Can It Help Me?</a></li>
<li><a href="chapter-1.html#s3">Alternatives</a></li>
<li><a href="chapter-1.html#s4">License Information</a></li>
<li><a href="chapter-1.html#s5">Support</a></li>
</ol>
</li>
<li>
<a href="chapter-2.html">
Registry
</a>
<ol type="1">
<li><a href="chapter-2.html#s1">Overview</a></li>
<li><a href="chapter-2.html#s2">Creating</a></li>
<li><a href="chapter-2.html#s3">Services</a></li>
<li><a href="chapter-2.html#s4">Namespaces</a></li>
</ol>
</li>
<li>
<a href="chapter-3.html">
Service Locator
</a>
<ol type="1">
<li><a href="chapter-3.html#s1">Overview</a></li>
<li><a href="chapter-3.html#s2">Conventional Architecture</a></li>
<li><a href="chapter-3.html#s3">Locator Pattern</a></li>
</ol>
</li>
<li><strong>
<a href="chapter-4.html">
Dependency Injection
</a>
</strong> <big>←</big>
<ol type="1">
<li><a href="chapter-4.html#s1">Overview</a></li>
<li><a href="chapter-4.html#s2">Setup</a></li>
</ol>
</li>
<li>
<a href="chapter-5.html">
Interceptors
</a>
<ol type="1">
<li><a href="chapter-5.html#s1">Overview</a></li>
<li><a href="chapter-5.html#s2">Architecture</a></li>
<li><a href="chapter-5.html#s3">Attaching</a></li>
<li><a href="chapter-5.html#s4">Ordering</a></li>
<li><a href="chapter-5.html#s5">Custom</a></li>
</ol>
</li>
<li>
<a href="chapter-6.html">
Service Models
</a>
<ol type="1">
<li><a href="chapter-6.html#s1">Overview</a></li>
<li><a href="chapter-6.html#s2">Pipelines</a></li>
<li><a href="chapter-6.html#s3">Models</a></li>
</ol>
</li>
<li>
<a href="chapter-7.html">
Logging
</a>
<ol type="1">
<li><a href="chapter-7.html#s1">Overview</a></li>
<li><a href="chapter-7.html#s2">LogFactory</a></li>
<li><a href="chapter-7.html#s3">Configuration</a></li>
</ol>
</li>
<li>
<a href="chapter-8.html">
Service Libraries
</a>
<ol type="1">
<li><a href="chapter-8.html#s1">Overview</a></li>
<li><a href="chapter-8.html#s2">Creating Libraries</a></li>
<li><a href="chapter-8.html#s3">Using Libraries</a></li>
</ol>
</li>
<li>
<a href="chapter-9.html">
Customizing Needle
</a>
<ol type="1">
<li><a href="chapter-9.html#s1">Namespaces</a></li>
<li><a href="chapter-9.html#s2">Interceptors</a></li>
<li><a href="chapter-9.html#s3">Contexts</a></li>
</ol>
</li>
</ol>
<h2>Other Documentation</h2>
<ul>
<li><a href="http://needle.rubyforge.org/api/index.html">Needle API</a></li>
<li><a href="http://needle.rubyforge.org/faq.html">Needle FAQ</a></li>
</ul>
<h2>Tutorials</h2>
<ol>
</ol>
<p align="center"><strong>More To Come...</strong></p>
<div class="license">
<a href="http://creativecommons.org/licenses/by-sa/2.0/"><img alt="Creative Commons License" border="0" src="http://creativecommons.org/images/public/somerights" /></a><br />
This manual is licensed under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons License</a>.
</div>
</div>
</td><td valign='top' width="100%">
<div id="content">
<h1>4. Dependency Injection</h1>
<h2>
<a name="s1"></a>
4.1. Overview
</h2>
<div class="section">
<p>The service locator works well when there are few dependencies, and the dependency graph is not very deep. However, it has a few drawbacks:</p>
<ol>
<li>It requires each object to accept the locator as a parameter, and to know how to use it. This is problematic if you want to use existing classes that were created without knowledge of a locator. (I.e., <code>Logger</code>, in the Ruby library).
</li>
<li>It requires each object to know what the services are named, in the locator. If you ever decide to change the name of a service in the locator, you may have to change lots of code to comply with the change.
</li>
<li>For deep dependency graphs, it can become cumbersome to have to pass the locator to each constructor.</li>
</ol>
<p>This is where <em>dependency injection</em> comes in. It allows you to define how each service is initialized, including setting dependencies (either via constructor parameters or via property accessors). In fact, it can do a lot more than that, even allowing you to specify how the lifecycle of the service should be managed and hooking “interceptors” onto the service to filter method invocations.</p>
</div>
<h2>
<a name="s2"></a>
4.2. Setup
</h2>
<div class="section">
<p>Setting up for DI is very similar to the setup for a service locator, but instead of passing the locator (we’ll call it a <em>registry</em> now), we only pass (or set) the dependencies that the service itself needs.</p>
<pre>
require 'needle'
def create_application
registry = Needle::Registry.define do |b|
b.view { View.new }
b.logger { Logger.new }
b.database { Database.new( b.logger ) }
b.authenticator { Authenticator.new(b.logger, b.database) }
b.session { Session.new(b.logger, b.database) }
b.app do
app = Application.new
app.logger = b.logger
app.view = b.view
app.database = b.database
app.authenticator = b.authenticator
app.session = b.session
app
end
end
registry[:app]
end
class Application
attr_writer :view, :logger, :database, :authenticator, :session
end
class Session
def initialize( logger, database )
@database = database
@logger = logger
end
end
...
</pre>
<p>The <code>create_application</code> method is now (necessarily) a little more complex, since it now contains all of the initialization logic for each service in the application. However, look how much simpler this made the other classes, especially the <code>Application</code> class.</p>
<p>Now, each class no longer even needs to care that it is being initialized via another container. All it knows is that when it is created, it will be given each of its dependencies (either as constructor parameters or as property accessors).</p>
</div>
</div>
</td></tr>
</table>
</body>
</html>
|