needle 0.6.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/doc/faq/faq.rb +143 -0
- data/doc/faq/faq.yml +75 -0
- data/doc/manual-html/chapter-1.html +26 -6
- data/doc/manual-html/chapter-2.html +39 -9
- data/doc/manual-html/chapter-3.html +177 -8
- data/doc/manual-html/chapter-4.html +108 -8
- data/doc/manual-html/chapter-5.html +28 -8
- data/doc/manual-html/chapter-6.html +28 -8
- data/doc/manual-html/chapter-7.html +29 -9
- data/doc/manual-html/chapter-8.html +176 -0
- data/doc/manual-html/index.html +26 -6
- data/doc/manual/manual.rb +1 -1
- data/doc/manual/manual.yml +7 -0
- data/doc/manual/parts/02_creating.txt +12 -2
- data/doc/manual/parts/03_conventional.txt +29 -0
- data/doc/manual/parts/03_locator.txt +60 -0
- data/doc/manual/parts/03_overview.txt +19 -0
- data/doc/manual/parts/04_overview.txt +9 -0
- data/doc/manual/parts/04_setup.txt +44 -0
- data/lib/needle/container.rb +68 -11
- data/lib/needle/registry.rb +25 -28
- data/lib/needle/service-point.rb +6 -1
- data/lib/needle/version.rb +1 -1
- data/test/models/model_test.rb +21 -1
- data/test/tc_container.rb +28 -1
- data/test/tc_registry.rb +22 -5
- data/test/tc_service_point.rb +9 -0
- metadata +11 -2
@@ -1,6 +1,6 @@
|
|
1
1
|
<html>
|
2
2
|
<head>
|
3
|
-
<title>Needle Manual :: Chapter 4:
|
3
|
+
<title>Needle Manual :: Chapter 4: Dependency Injection</title>
|
4
4
|
<link type="text/css" rel="stylesheet" href="manual.css" />
|
5
5
|
</head>
|
6
6
|
|
@@ -14,8 +14,8 @@
|
|
14
14
|
</div>
|
15
15
|
</td><td valign='middle' align='right'>
|
16
16
|
<div class="info">
|
17
|
-
Needle Version: <strong>0.
|
18
|
-
Manual Last Updated: <strong>2004-10-
|
17
|
+
Needle Version: <strong>0.9.0</strong><br />
|
18
|
+
Manual Last Updated: <strong>2004-10-28 15:34 GMT</strong>
|
19
19
|
</div>
|
20
20
|
</td></tr>
|
21
21
|
</table>
|
@@ -70,27 +70,37 @@
|
|
70
70
|
|
71
71
|
<li>
|
72
72
|
<a href="chapter-3.html">
|
73
|
-
|
73
|
+
Service Locator
|
74
74
|
</a>
|
75
75
|
|
76
76
|
<ol type="1">
|
77
77
|
|
78
|
+
<li><a href="chapter-3.html#s1">Overview</a></li>
|
79
|
+
|
80
|
+
<li><a href="chapter-3.html#s2">Conventional Architecture</a></li>
|
81
|
+
|
82
|
+
<li><a href="chapter-3.html#s3">Locator Pattern</a></li>
|
83
|
+
|
78
84
|
</ol>
|
79
85
|
</li>
|
80
86
|
|
81
87
|
<li><strong>
|
82
88
|
<a href="chapter-4.html">
|
83
|
-
|
89
|
+
Dependency Injection
|
84
90
|
</a>
|
85
91
|
</strong> <big>←</big>
|
86
92
|
<ol type="1">
|
87
93
|
|
94
|
+
<li><a href="chapter-4.html#s1">Overview</a></li>
|
95
|
+
|
96
|
+
<li><a href="chapter-4.html#s2">Setup</a></li>
|
97
|
+
|
88
98
|
</ol>
|
89
99
|
</li>
|
90
100
|
|
91
101
|
<li>
|
92
102
|
<a href="chapter-5.html">
|
93
|
-
|
103
|
+
Interceptors
|
94
104
|
</a>
|
95
105
|
|
96
106
|
<ol type="1">
|
@@ -100,7 +110,7 @@
|
|
100
110
|
|
101
111
|
<li>
|
102
112
|
<a href="chapter-6.html">
|
103
|
-
|
113
|
+
Service Models
|
104
114
|
</a>
|
105
115
|
|
106
116
|
<ol type="1">
|
@@ -110,6 +120,16 @@
|
|
110
120
|
|
111
121
|
<li>
|
112
122
|
<a href="chapter-7.html">
|
123
|
+
Logging
|
124
|
+
</a>
|
125
|
+
|
126
|
+
<ol type="1">
|
127
|
+
|
128
|
+
</ol>
|
129
|
+
</li>
|
130
|
+
|
131
|
+
<li>
|
132
|
+
<a href="chapter-8.html">
|
113
133
|
Creating Libraries
|
114
134
|
</a>
|
115
135
|
|
@@ -143,7 +163,87 @@
|
|
143
163
|
|
144
164
|
<div id="content">
|
145
165
|
|
146
|
-
<h1>4.
|
166
|
+
<h1>4. Dependency Injection</h1>
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
<h2>
|
171
|
+
<a name="s1"></a>
|
172
|
+
4.1. Overview
|
173
|
+
</h2>
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
<div class="section">
|
178
|
+
<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>
|
179
|
+
|
180
|
+
<ol>
|
181
|
+
<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).<br />
|
182
|
+
</li>
|
183
|
+
<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.<br />
|
184
|
+
</li>
|
185
|
+
<li>For deep dependency graphs, it can become cumbersome to have to pass the locator to each constructor.</li>
|
186
|
+
</ol>
|
187
|
+
|
188
|
+
<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>
|
189
|
+
</div>
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
<h2>
|
194
|
+
<a name="s2"></a>
|
195
|
+
4.2. Setup
|
196
|
+
</h2>
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
<div class="section">
|
201
|
+
<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>
|
202
|
+
|
203
|
+
<pre>
|
204
|
+
require 'needle'
|
205
|
+
|
206
|
+
def create_application
|
207
|
+
registry = Needle::Registry.define do |b|
|
208
|
+
b.view { View.new }
|
209
|
+
b.logger { Logger.new }
|
210
|
+
b.database { Database.new( b.logger ) }
|
211
|
+
b.authenticator { Authenticator.new(b.logger, b.database) }
|
212
|
+
b.session { Session.new(b.logger, b.database) }
|
213
|
+
|
214
|
+
b.app do
|
215
|
+
app = Application.new
|
216
|
+
app.logger = b.logger
|
217
|
+
app.view = b.view
|
218
|
+
app.database = b.database
|
219
|
+
app.authenticator = b.authenticator
|
220
|
+
app.session = b.session
|
221
|
+
app
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
registry[:app]
|
226
|
+
end
|
227
|
+
|
228
|
+
class Application
|
229
|
+
attr_writer :view, :logger, :database, :authenticator, :session
|
230
|
+
end
|
231
|
+
|
232
|
+
class Session
|
233
|
+
def initialize( logger, database )
|
234
|
+
@database = database
|
235
|
+
@logger = logger
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
...
|
240
|
+
</pre>
|
241
|
+
|
242
|
+
<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>
|
243
|
+
|
244
|
+
<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).<br />
|
245
|
+
</p>
|
246
|
+
</div>
|
147
247
|
|
148
248
|
|
149
249
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
<html>
|
2
2
|
<head>
|
3
|
-
<title>Needle Manual :: Chapter 5:
|
3
|
+
<title>Needle Manual :: Chapter 5: Interceptors</title>
|
4
4
|
<link type="text/css" rel="stylesheet" href="manual.css" />
|
5
5
|
</head>
|
6
6
|
|
@@ -14,8 +14,8 @@
|
|
14
14
|
</div>
|
15
15
|
</td><td valign='middle' align='right'>
|
16
16
|
<div class="info">
|
17
|
-
Needle Version: <strong>0.
|
18
|
-
Manual Last Updated: <strong>2004-10-
|
17
|
+
Needle Version: <strong>0.9.0</strong><br />
|
18
|
+
Manual Last Updated: <strong>2004-10-28 15:34 GMT</strong>
|
19
19
|
</div>
|
20
20
|
</td></tr>
|
21
21
|
</table>
|
@@ -70,27 +70,37 @@
|
|
70
70
|
|
71
71
|
<li>
|
72
72
|
<a href="chapter-3.html">
|
73
|
-
|
73
|
+
Service Locator
|
74
74
|
</a>
|
75
75
|
|
76
76
|
<ol type="1">
|
77
77
|
|
78
|
+
<li><a href="chapter-3.html#s1">Overview</a></li>
|
79
|
+
|
80
|
+
<li><a href="chapter-3.html#s2">Conventional Architecture</a></li>
|
81
|
+
|
82
|
+
<li><a href="chapter-3.html#s3">Locator Pattern</a></li>
|
83
|
+
|
78
84
|
</ol>
|
79
85
|
</li>
|
80
86
|
|
81
87
|
<li>
|
82
88
|
<a href="chapter-4.html">
|
83
|
-
|
89
|
+
Dependency Injection
|
84
90
|
</a>
|
85
91
|
|
86
92
|
<ol type="1">
|
87
93
|
|
94
|
+
<li><a href="chapter-4.html#s1">Overview</a></li>
|
95
|
+
|
96
|
+
<li><a href="chapter-4.html#s2">Setup</a></li>
|
97
|
+
|
88
98
|
</ol>
|
89
99
|
</li>
|
90
100
|
|
91
101
|
<li><strong>
|
92
102
|
<a href="chapter-5.html">
|
93
|
-
|
103
|
+
Interceptors
|
94
104
|
</a>
|
95
105
|
</strong> <big>←</big>
|
96
106
|
<ol type="1">
|
@@ -100,7 +110,7 @@
|
|
100
110
|
|
101
111
|
<li>
|
102
112
|
<a href="chapter-6.html">
|
103
|
-
|
113
|
+
Service Models
|
104
114
|
</a>
|
105
115
|
|
106
116
|
<ol type="1">
|
@@ -110,6 +120,16 @@
|
|
110
120
|
|
111
121
|
<li>
|
112
122
|
<a href="chapter-7.html">
|
123
|
+
Logging
|
124
|
+
</a>
|
125
|
+
|
126
|
+
<ol type="1">
|
127
|
+
|
128
|
+
</ol>
|
129
|
+
</li>
|
130
|
+
|
131
|
+
<li>
|
132
|
+
<a href="chapter-8.html">
|
113
133
|
Creating Libraries
|
114
134
|
</a>
|
115
135
|
|
@@ -143,7 +163,7 @@
|
|
143
163
|
|
144
164
|
<div id="content">
|
145
165
|
|
146
|
-
<h1>5.
|
166
|
+
<h1>5. Interceptors</h1>
|
147
167
|
|
148
168
|
|
149
169
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
<html>
|
2
2
|
<head>
|
3
|
-
<title>Needle Manual :: Chapter 6:
|
3
|
+
<title>Needle Manual :: Chapter 6: Service Models</title>
|
4
4
|
<link type="text/css" rel="stylesheet" href="manual.css" />
|
5
5
|
</head>
|
6
6
|
|
@@ -14,8 +14,8 @@
|
|
14
14
|
</div>
|
15
15
|
</td><td valign='middle' align='right'>
|
16
16
|
<div class="info">
|
17
|
-
Needle Version: <strong>0.
|
18
|
-
Manual Last Updated: <strong>2004-10-
|
17
|
+
Needle Version: <strong>0.9.0</strong><br />
|
18
|
+
Manual Last Updated: <strong>2004-10-28 15:34 GMT</strong>
|
19
19
|
</div>
|
20
20
|
</td></tr>
|
21
21
|
</table>
|
@@ -70,27 +70,37 @@
|
|
70
70
|
|
71
71
|
<li>
|
72
72
|
<a href="chapter-3.html">
|
73
|
-
|
73
|
+
Service Locator
|
74
74
|
</a>
|
75
75
|
|
76
76
|
<ol type="1">
|
77
77
|
|
78
|
+
<li><a href="chapter-3.html#s1">Overview</a></li>
|
79
|
+
|
80
|
+
<li><a href="chapter-3.html#s2">Conventional Architecture</a></li>
|
81
|
+
|
82
|
+
<li><a href="chapter-3.html#s3">Locator Pattern</a></li>
|
83
|
+
|
78
84
|
</ol>
|
79
85
|
</li>
|
80
86
|
|
81
87
|
<li>
|
82
88
|
<a href="chapter-4.html">
|
83
|
-
|
89
|
+
Dependency Injection
|
84
90
|
</a>
|
85
91
|
|
86
92
|
<ol type="1">
|
87
93
|
|
94
|
+
<li><a href="chapter-4.html#s1">Overview</a></li>
|
95
|
+
|
96
|
+
<li><a href="chapter-4.html#s2">Setup</a></li>
|
97
|
+
|
88
98
|
</ol>
|
89
99
|
</li>
|
90
100
|
|
91
101
|
<li>
|
92
102
|
<a href="chapter-5.html">
|
93
|
-
|
103
|
+
Interceptors
|
94
104
|
</a>
|
95
105
|
|
96
106
|
<ol type="1">
|
@@ -100,7 +110,7 @@
|
|
100
110
|
|
101
111
|
<li><strong>
|
102
112
|
<a href="chapter-6.html">
|
103
|
-
|
113
|
+
Service Models
|
104
114
|
</a>
|
105
115
|
</strong> <big>←</big>
|
106
116
|
<ol type="1">
|
@@ -110,6 +120,16 @@
|
|
110
120
|
|
111
121
|
<li>
|
112
122
|
<a href="chapter-7.html">
|
123
|
+
Logging
|
124
|
+
</a>
|
125
|
+
|
126
|
+
<ol type="1">
|
127
|
+
|
128
|
+
</ol>
|
129
|
+
</li>
|
130
|
+
|
131
|
+
<li>
|
132
|
+
<a href="chapter-8.html">
|
113
133
|
Creating Libraries
|
114
134
|
</a>
|
115
135
|
|
@@ -143,7 +163,7 @@
|
|
143
163
|
|
144
164
|
<div id="content">
|
145
165
|
|
146
|
-
<h1>6.
|
166
|
+
<h1>6. Service Models</h1>
|
147
167
|
|
148
168
|
|
149
169
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
<html>
|
2
2
|
<head>
|
3
|
-
<title>Needle Manual :: Chapter 7:
|
3
|
+
<title>Needle Manual :: Chapter 7: Logging</title>
|
4
4
|
<link type="text/css" rel="stylesheet" href="manual.css" />
|
5
5
|
</head>
|
6
6
|
|
@@ -14,8 +14,8 @@
|
|
14
14
|
</div>
|
15
15
|
</td><td valign='middle' align='right'>
|
16
16
|
<div class="info">
|
17
|
-
Needle Version: <strong>0.
|
18
|
-
Manual Last Updated: <strong>2004-10-
|
17
|
+
Needle Version: <strong>0.9.0</strong><br />
|
18
|
+
Manual Last Updated: <strong>2004-10-28 15:34 GMT</strong>
|
19
19
|
</div>
|
20
20
|
</td></tr>
|
21
21
|
</table>
|
@@ -70,27 +70,37 @@
|
|
70
70
|
|
71
71
|
<li>
|
72
72
|
<a href="chapter-3.html">
|
73
|
-
|
73
|
+
Service Locator
|
74
74
|
</a>
|
75
75
|
|
76
76
|
<ol type="1">
|
77
77
|
|
78
|
+
<li><a href="chapter-3.html#s1">Overview</a></li>
|
79
|
+
|
80
|
+
<li><a href="chapter-3.html#s2">Conventional Architecture</a></li>
|
81
|
+
|
82
|
+
<li><a href="chapter-3.html#s3">Locator Pattern</a></li>
|
83
|
+
|
78
84
|
</ol>
|
79
85
|
</li>
|
80
86
|
|
81
87
|
<li>
|
82
88
|
<a href="chapter-4.html">
|
83
|
-
|
89
|
+
Dependency Injection
|
84
90
|
</a>
|
85
91
|
|
86
92
|
<ol type="1">
|
87
93
|
|
94
|
+
<li><a href="chapter-4.html#s1">Overview</a></li>
|
95
|
+
|
96
|
+
<li><a href="chapter-4.html#s2">Setup</a></li>
|
97
|
+
|
88
98
|
</ol>
|
89
99
|
</li>
|
90
100
|
|
91
101
|
<li>
|
92
102
|
<a href="chapter-5.html">
|
93
|
-
|
103
|
+
Interceptors
|
94
104
|
</a>
|
95
105
|
|
96
106
|
<ol type="1">
|
@@ -100,7 +110,7 @@
|
|
100
110
|
|
101
111
|
<li>
|
102
112
|
<a href="chapter-6.html">
|
103
|
-
|
113
|
+
Service Models
|
104
114
|
</a>
|
105
115
|
|
106
116
|
<ol type="1">
|
@@ -110,7 +120,7 @@
|
|
110
120
|
|
111
121
|
<li><strong>
|
112
122
|
<a href="chapter-7.html">
|
113
|
-
|
123
|
+
Logging
|
114
124
|
</a>
|
115
125
|
</strong> <big>←</big>
|
116
126
|
<ol type="1">
|
@@ -118,6 +128,16 @@
|
|
118
128
|
</ol>
|
119
129
|
</li>
|
120
130
|
|
131
|
+
<li>
|
132
|
+
<a href="chapter-8.html">
|
133
|
+
Creating Libraries
|
134
|
+
</a>
|
135
|
+
|
136
|
+
<ol type="1">
|
137
|
+
|
138
|
+
</ol>
|
139
|
+
</li>
|
140
|
+
|
121
141
|
</ol>
|
122
142
|
|
123
143
|
<h2>API Reference</h2>
|
@@ -143,7 +163,7 @@
|
|
143
163
|
|
144
164
|
<div id="content">
|
145
165
|
|
146
|
-
<h1>7.
|
166
|
+
<h1>7. Logging</h1>
|
147
167
|
|
148
168
|
|
149
169
|
|