Skip to content

Commit edef858

Browse files
committed
Expanded di codegen section
1 parent bcd947a commit edef858

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

Chapter_07/README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,67 @@ generates static code for creating new instances and resolving dependencies.
118118
You can find an example of how to use the di generator in
119119
```bin/generator.dart``` file.
120120

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+
121182
## AngularDart Parser Generator
122183

123184
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
126187
access fields and methods, so it doesn't have to use mirrors.
127188

128189
You can find an example of how to use the parser generator in
129-
```bin/generator.dart``` file.
190+
`bin/generator.dart` file.
130191

131192
## Code Generators and Development Mode
132193

@@ -177,4 +238,4 @@ so make sure you are aware of those limitations before you start using it.
177238
[shadowdom-limitations]: https://github.com/polymer/ShadowDOM#known-limitations
178239
[shadow-dom-polyfill]: http://pub.dartlang.org/packages/shadow_dom
179240
[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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy