@@ -118,6 +118,67 @@ generates static code for creating new instances and resolving dependencies.
118
118
You can find an example of how to use the di generator in
119
119
``` bin/generator.dart ``` file.
120
120
121
+ ### Discovering Instantiable Types
122
+
123
+ Ideally, types that are instantiated by the injector should be extracted from
124
+ the module definitions, however currently di modules are dynamically defined
125
+ and are mutable, making them very hard (impossible in some cases) to analyze
126
+ statically.
127
+
128
+ The generator has to rely on some guidance from the user to mark classes that
129
+ injector has to instantiate. There are two ways of doing this: @Injectables
130
+ or custom class annotation.
131
+
132
+ ` @Injectables ` is an annotation provided by the di package which can be
133
+ applied on a library definition with a list of types that the generator
134
+ should process.
135
+
136
+ ```
137
+ @Injectables(const [
138
+ MyService
139
+ ])
140
+ library my_service_librarry;
141
+
142
+ import 'package:di/annotations.dart';
143
+
144
+ class MyService {
145
+ // ...
146
+ }
147
+ ```
148
+
149
+ @Injectables annotation should be mainly used with classes that are out of
150
+ your control (ex. you can't modify the source code -- third party library).
151
+ In all other cases it's preferable to use custom class annotation(s).
152
+
153
+ You can also define your own custom class annotations and apply them on
154
+ classes that you need to be instantiated by the injector.
155
+
156
+ ```
157
+ library injectable;
158
+
159
+ /**
160
+ * An annotation specifying that the annotated class will be instantiated by
161
+ * di Injector and type factory code generator should include it in its output.
162
+ */
163
+ class InjectableService {
164
+ const InjectableService();
165
+ }
166
+ ```
167
+
168
+ ```
169
+ @InjectableService()
170
+ class QueryService {
171
+ // ...
172
+ }
173
+ ```
174
+
175
+ You can then then configure generator with those annotations.
176
+
177
+ When configuring the generator with the custom annotation you need to pass
178
+ a fully qualified class name (including the library prefix). In case of the
179
+ above example the fully qualified name of Service annotation would be
180
+ ` injectable.InjectableService ` .
181
+
121
182
## AngularDart Parser Generator
122
183
123
184
AngularDart Parser Generator extracts all expressions from your application
@@ -126,7 +187,7 @@ expressions and while invoking the expressions it uses pre-generated code to
126
187
access fields and methods, so it doesn't have to use mirrors.
127
188
128
189
You can find an example of how to use the parser generator in
129
- ``` bin/generator.dart `` ` file.
190
+ ` bin/generator.dart ` file.
130
191
131
192
## Code Generators and Development Mode
132
193
@@ -177,4 +238,4 @@ so make sure you are aware of those limitations before you start using it.
177
238
[ shadowdom-limitations ] : https://github.com/polymer/ShadowDOM#known-limitations
178
239
[ shadow-dom-polyfill ] : http://pub.dartlang.org/packages/shadow_dom
179
240
[ dart2js ] : https://www.dartlang.org/docs/dart-up-and-running/contents/ch04-tools-dart2js.html
180
- [ mirrors-used ] : https://api.dartlang.org/docs/channels/stable/latest/dart_mirrors/MirrorsUsed.html
241
+ [ mirrors-used ] : https://api.dartlang.org/docs/channels/stable/latest/dart_mirrors/MirrorsUsed.html
0 commit comments