@@ -13,7 +13,6 @@ include ../_util-fns
13
13
'dependency-injection/dart/lib/car/car.dart, dependency-injection/dart/lib/car/car_no_di.dart' ,
14
14
'car-ctor, car-ctor' ,
15
15
'lib/car/car.dart (excerpt with DI), lib/car/car.dart (excerpt without DI)' )( format ="." )
16
- + includeShared('{ts}' , 'why-3' )
17
16
+ includeShared('{ts}' , 'why-3-1' )
18
17
+ includeShared('{ts}' , 'why-3-2' )
19
18
- var stylePattern = { otl: / (new Car. * $ )/ gm };
@@ -52,7 +51,13 @@ include ../_util-fns
52
51
+ includeShared('{ts}' , 'di-3' )
53
52
+ makeExample('dependency-injection/dart/lib/heroes/hero_service_1.dart' ,null , 'lib/heroes/hero_service.dart' )
54
53
+ includeShared('{ts}' , 'di-4' )
55
- + includeShared('{ts}' , 'di-5' )
54
+ .l-sub-section
55
+ :marked
56
+ We aren't even pretending this is a real service.
57
+ If we were actually getting data from a remote server, the API would have to be asynchronous,
58
+ returning a `Future`.
59
+ We'd also have to rewrite the way components consume our service.
60
+ This is important in general, but not to our current story.
56
61
+ includeShared('{ts}' , 'di-6' )
57
62
+ includeShared('{ts}' , 'di-configure-injector-1' )
58
63
+ makeExample('dependency-injection/dart/web/main.dart' , 'bootstrap' , 'web/main.dart (excerpt)' )( format ='.' )
@@ -77,13 +82,26 @@ include ../_util-fns
77
82
78
83
Adding a parameter to the constructor isn't all that's happening here.
79
84
+ makeExample('dependency-injection/dart/lib/heroes/hero_list_component_2.dart' , 'ctor' )( format ="." )
80
- + includeShared('{ts}' , 'di-prepare-for-injection-2' )
85
+ :marked
86
+ The constructor parameter has a type: `HeroService`.
87
+ The `HeroListComponent` class is also annotated with `@Component`
88
+ (scroll up to confirm that fact).
89
+ Also recall that the parent component (`HeroesComponent`)
90
+ has `providers` information for `HeroService`.
91
+
92
+ The constructor parameter type, the `@Component` annotation,
93
+ and the parent's `providers` information combine to tell the
94
+ Angular injector to inject an instance of
95
+ `HeroService` whenever it creates a new `HeroListComponent`.
81
96
+ includeShared('{ts}' , 'di-create-injector-implicitly-1' )
82
97
+ makeExample('dependency-injection/dart/lib/car/car_injector.dart' ,'injector-create-and-call' )( format ="." )
83
98
+ includeShared('{ts}' , 'di-create-injector-implicitly-2' )
84
99
+ includeShared('{ts}' , 'di-singleton-services' )
100
+
101
+ // Skip the testing section, for now.
85
102
// +includeShared('{ts}', 'di-testing-component-1')
86
103
// +includeShared('{ts}', 'di-testing-component-2')
104
+
87
105
+ includeShared('{ts}' , 'di-service-service-1' )
88
106
+ makeTabs(
89
107
` dependency-injection/dart/lib/heroes/hero_service_2.dart,
@@ -92,16 +110,19 @@ include ../_util-fns
92
110
` lib/heroes/hero_service (v.2),
93
111
lib/heroes/hero_service (v.1)` )
94
112
+ includeShared('{ts}' , 'di-service-service-2' )
95
- + includeShared('{ts}' , 'di-injectable' )
113
+ + includeShared('{ts}' , 'di-injectable-1' )
114
+ + includeShared('{ts}' , 'di-injectable-2' )
96
115
97
116
+ includeShared('{ts}' , 'logger-service-1' )
98
117
+ makeExample(
99
118
'dependency-injection/dart/lib/logger_service.dart' ,null , 'lib/logger_service' )
100
- + includeShared('{ts}' , 'logger-service-2' )
119
+ :marked
120
+ We're likely to need the same logger service everywhere in our application,
121
+ so we put it in the `lib/` folder, and
122
+ we register it in the `providers` array of the metadata for our application root component, `AppComponent`.
101
123
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-logger' , 'lib/app_component.dart (excerpt)' )
102
124
+ includeShared('{ts}' , 'logger-service-3' )
103
125
+ includeShared('{ts}' , 'logger-service-4' )
104
- + makeExample('dependency-injection/dart/lib/providers_component.dart' ,'import-optional' )( format ='.' )
105
126
+ includeShared('{ts}' , 'logger-service-5' )
106
127
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'provider-10-ctor' )( format ='.' )
107
128
+ includeShared('{ts}' , 'logger-service-6' )
@@ -112,14 +133,29 @@ include ../_util-fns
112
133
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-logger' )
113
134
+ includeShared('{ts}' , 'providers-2' )
114
135
+ includeShared('{ts}' , 'providers-provide-1' )
136
+ :marked
137
+ ### The *Provider* class
138
+ + includeShared('{ts}' , 'providers-provide-1-1' )
115
139
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-1' )
116
140
+ includeShared('{ts}' , 'providers-provide-2' )
117
141
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-2' )
118
- + includeShared('{ts}' , 'providers-provide-3' )
119
- + makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-3' )
120
- + includeShared('{ts}' , 'providers-provide-4' )
142
+ // Don't discuss provide function.
143
+ :marked
144
+ We supply two arguments to the `Provider` constructor.
145
+ + includeShared('{ts}' , 'providers-provide-4-2' )
121
146
+ includeShared('{ts}' , 'providers-alternative-1' )
122
147
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-4' )
148
+ .callout.is-helpful
149
+ header Dart difference: Constants in metadata
150
+ :marked
151
+ In Dart, the value of a metadata annotation must be a compile-time constant.
152
+ For that reason, you can't call functions to get values
153
+ to use within an annotation.
154
+ Instead, you use constant literals or constant constructors.
155
+ For example, a TypeScript or JavaScript program might use
156
+ `provide(Logger, {useClass: BetterLogger})` or, equivalently,
157
+ `new Provider(Logger, {useClass: BetterLogger})`.
158
+ A Dart annotation would instead use the constant value `const Provider(Logger, useClass: BetterLogger)`.
123
159
+ includeShared('{ts}' , 'providers-alternative-2' )
124
160
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'EvenBetterLogger' )
125
161
+ includeShared('{ts}' , 'providers-alternative-3' )
@@ -129,6 +165,7 @@ include ../_util-fns
129
165
+ includeShared('{ts}' , 'providers-aliased-2' )
130
166
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-6b' )( format ="." )
131
167
+ includeShared('{ts}' , 'providers-value-1' )
168
+ p <b >[PENDING: fix this code? When would you really use a value? maybe if it's initialized from a string?]</b >
132
169
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'silent-logger' )( format ="." )
133
170
+ includeShared('{ts}' , 'providers-value-2' )
134
171
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-7' )( format ="." )
@@ -151,20 +188,35 @@ include ../_util-fns
151
188
+ includeShared('{ts}' , 'tokens-1' )
152
189
+ makeExample('dependency-injection/dart/lib/injector_component.dart' ,'get-hero-service' )( format ='.' )
153
190
+ includeShared('{ts}' , 'tokens-2' )
191
+ p <b >[PENDING: How about a better name than ProviderComponent8?]</b >
154
192
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'provider-8-ctor' )( format ="." )
155
193
+ includeShared('{ts}' , 'tokens-3' )
156
194
+ includeShared('{ts}' , 'tokens-non-class-deps-1' )
195
+ p <b >[PENDING: What's the Dart equivalent of "object hash"? How can we shorten/improve this code?]</b >
157
196
+ makeExample('dependency-injection/dart/lib/app_config.dart' ,'config' ,'lib/app_config.dart (excerpt)' )( format ='.' )
158
197
+ includeShared('{ts}' , 'tokens-non-class-deps-2' )
198
+ p <b >[PENDING: That's not right. That text is from the TypeScript version. How should the Dart code/text be different?]</b >
199
+ p <b >[PENDING: TypeScript has a whole section here on "Interfaces aren't valid tokens". But they are in Dart!]</b >
200
+ .callout.is-helpful
201
+ header Dart difference: Interfaces are valid tokens
202
+ :marked
203
+ In TypeScript, interfaces don't work as provider tokens.
204
+ Work arounds include using string tokens or `OpaqueToken` objects
205
+ as the first argument to the `Provider` constructor or `provide` function.
206
+ Dart doesn't have this problem.
207
+ Go ahead and use an interface name
208
+ (usually, that's the name of an abstract class)
209
+ as the first argument.
159
210
+ includeShared('{ts}' , 'tokens-string-1' )
211
+ p <b >[PENDING: Where did CONFIG_HASH come from? (lib/app_config.dart) Why does the code use that instead of CONFIG? (only matters if we keep the code, I suppose)]</b >
160
212
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-9a' )( format ="." )
161
213
+ includeShared('{ts}' , 'tokens-string-2' )
214
+ p <b >[PENDING: I don't like showing ProviderComponent9a. Also... can we make this code easier to read?]</b >
162
215
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'provider-9a-ctor' )( format ="." )
163
216
+ includeShared('{ts}' , 'tokens-opaque-1' )
164
217
+ makeExample('dependency-injection/dart/lib/app_config.dart' ,'token' )( format ='.' )
165
218
+ includeShared('{ts}' , 'tokens-opaque-2' )
166
219
+ makeExample('dependency-injection/dart/lib/providers_component.dart' ,'providers-9b' )( format ="." )
167
- + makeExample('dependency-injection/dart/lib/providers_component.dart' ,'provider-9b-ctor' )( format ="." )
168
220
+ includeShared('{ts}' , 'tokens-opaque-3' )
169
221
+ makeExample('dependency-injection/dart/lib/app_component.dart' ,'providers' , 'lib/app_component.dart (providers)' )( format ="." )
170
222
+ makeExample('dependency-injection/dart/lib/app_component.dart' ,'ctor' , 'lib/app_component.dart (constructor)' )( format ="." )
0 commit comments