Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: third_party/pkg/di/lib/module.dart

Issue 107003007: Adding Angular's DI package & Dart unittests which test it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/pkg/di/lib/mirrors.dart ('k') | third_party/pkg/di/lib/reflected_type.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 part of di;
2
3 typedef dynamic FactoryFn(Injector injector);
4
5 /**
6 * Creation strategy is asked to return an instance of the type after
7 * [Injector.get] locates the defining injector that has no instance cached.
8 * [directInstantation] is true when an instance is created directly from
9 * [Injector.instantiate].
10 */
11 typedef dynamic CreationStrategy(
12 Injector requesting,
13 Injector defining,
14 dynamic factory()
15 );
16
17 /**
18 * Visibility determines if the instance in the defining module is visible to
19 * the requesting injector. If true is returned, then the instance from the
20 * defining injector is provided. If false is returned, the injector keeps
21 * walking up the tree to find another visible instance.
22 */
23 typedef bool Visibility(Injector requesting, Injector defining);
24
25
26 /**
27 * A collection of type bindings. Once the module is passed into the injector,
28 * the injector creates a copy of the module and all subsequent changes to the
29 * module have no effect.
30 */
31 class Module {
32 final Map<Type, _Provider> _providers = <Type, _Provider>{};
33 final List<Module> _childModules = <Module>[];
34
35 Map<Type, _Provider> _providersCache;
36
37 /**
38 * Compiles and returs bindings map by performing depth-first traversal of the
39 * child (installed) modules.
40 */
41 Map<Type, _Provider> get _bindings {
42 if (_providersCache == null) {
43 _providersCache = <Type, _Provider>{};
44 _childModules.forEach((child) => _providersCache.addAll(child._bindings));
45 _providersCache.addAll(_providers);
46 }
47 return _providersCache;
48 }
49
50 /**
51 * Register binding to a concrete value.
52 *
53 * The [value] is what actually will be injected.
54 */
55 void value(Type id, value,
56 {CreationStrategy creation, Visibility visibility}) {
57 _providersCache = null;
58 _providers[id] = new _ValueProvider(value, creation, visibility);
59 }
60
61 /**
62 * Register binding to a [Type].
63 *
64 * The [implementedBy] will be instantiated using [new] operator and the
65 * resulting instance will be injected. If no type is provided, then it's
66 * implied that [id] should be instantiated.
67 */
68 void type(Type id, {Type implementedBy, CreationStrategy creation,
69 Visibility visibility}) {
70 _providersCache = null;
71 _providers[id] = new _TypeProvider(
72 implementedBy == null ? id : implementedBy, creation, visibility);
73 }
74
75 /**
76 * Register binding to a factory function.abstract
77 *
78 * The [factoryFn] will be called and all its arguments will get injected.
79 * The result of that function is the value that will be injected.
80 */
81 void factory(Type id, FactoryFn factoryFn,
82 {CreationStrategy creation, Visibility visibility}) {
83 _providersCache = null;
84 _providers[id] = new _FactoryProvider(factoryFn, creation, visibility);
85 }
86
87 /**
88 * Installs another module into this module. Bindings defined on this module
89 * take precidence over the installed module.
90 */
91 void install(Module module) {
92 _childModules.add(module);
93 _providersCache = null;
94 }
95 }
96
97 /** Deafault creation strategy is to instantiate on the defining injector. */
98 dynamic _defaultCreationStrategy(Injector requesting, Injector defining,
99 dynamic factory()) => factory();
100
101 /** By default all values are visible to child injectors. */
102 bool _defaultVisibility(_, __) => true;
103
104
105 typedef Object ObjectFactory(Type type, Injector requestor);
106
107 abstract class _Provider {
108 final CreationStrategy creationStrategy;
109 final Visibility visibility;
110
111 _Provider(this.creationStrategy, this.visibility);
112
113 dynamic get(Injector injector, Injector requestor, ObjectFactory getInstanceBy Type, error);
114 }
115
116 class _ValueProvider extends _Provider {
117 dynamic value;
118
119 _ValueProvider(this.value, [CreationStrategy creationStrategy,
120 Visibility visibility])
121 : super(creationStrategy, visibility);
122
123 dynamic get(Injector injector, Injector requestor, ObjectFactory getInstanceBy Type, error) =>
124 value;
125 }
126
127 class _TypeProvider extends _Provider {
128 final Type type;
129
130 _TypeProvider(this.type, [CreationStrategy creationStrategy,
131 Visibility visibility])
132 : super(creationStrategy, visibility);
133
134 dynamic get(Injector injector, Injector requestor, ObjectFactory getInstanceBy Type, error) =>
135 injector.newInstanceOf(type, getInstanceByType, requestor, error);
136
137 }
138
139 class _FactoryProvider extends _Provider {
140 final Function factoryFn;
141
142 _FactoryProvider(this.factoryFn, [CreationStrategy creationStrategy,
143 Visibility visibility])
144 : super(creationStrategy, visibility);
145
146 dynamic get(Injector injector, Injector requestor, getInstanceByType, error) = >
147 factoryFn(injector);
148 }
OLDNEW
« no previous file with comments | « third_party/pkg/di/lib/mirrors.dart ('k') | third_party/pkg/di/lib/reflected_type.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698