Skip to content

Commit 0e650a3

Browse files
committed
refactor(injector): moved things into private libraries to reduce visibility
1 parent 7d374b1 commit 0e650a3

29 files changed

+236
-197
lines changed

lib/annotations.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ library di.annotations;
88
class Injectables {
99
final List<Type> types;
1010
const Injectables(this.types);
11-
}
11+
}
12+
13+
/**
14+
* Annotation that can be applied to a class for which type factories
15+
* should be generated to be used by StaticInjector.
16+
*/
17+
class Injectable {
18+
const Injectable();
19+
}

lib/di.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
library di;
22

3-
import 'dart:collection';
4-
import 'package:collection/wrappers.dart';
5-
import 'error_helper.dart';
3+
import 'src/provider.dart';
4+
import 'key.dart';
65

7-
part 'injector.dart';
8-
part 'injector_delagate.dart';
9-
part 'key.dart';
10-
part 'module.dart';
11-
part 'errors.dart';
6+
part 'src/injector.dart';
7+
part 'src/module.dart';
8+
part 'src/errors.dart';
129

lib/dynamic_injector.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
library di.dynamic_injector;
22

33
import 'di.dart';
4-
import 'mirrors.dart';
5-
import 'error_helper.dart';
4+
import 'key.dart';
5+
import 'src/mirrors.dart';
6+
import 'src/base_injector.dart';
7+
import 'src/error_helper.dart';
8+
9+
export 'di.dart';
610

711
/**
812
* Dynamic implementation of [Injector] that uses mirrors.

lib/generator.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ import 'dart:io';
1313

1414
const String PACKAGE_PREFIX = 'package:';
1515
const String DART_PACKAGE_PREFIX = 'dart:';
16+
const List<String> _DEFAULT_INJECTABLE_ANNOTATIONS =
17+
const ['di.annotations.Injectable'];
1618

17-
main(args) {
19+
main(List<String> args) {
1820
if (args.length < 4) {
1921
print('Usage: generator path_to_sdk file_to_resolve annotations output [package_roots+]');
2022
exit(0);
2123
}
2224

2325
var pathToSdk = args[0];
2426
var entryPoint = args[1];
25-
var classAnnotations = args[2].split(',');
27+
var classAnnotations = args[2].split(',')
28+
..addAll(_DEFAULT_INJECTABLE_ANNOTATIONS);
2629
var output = args[3];
2730
var packageRoots = (args.length < 5) ? [Platform.packageRoot] : args.sublist(4);
2831

lib/key.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
part of di;
1+
library di.key;
22

33
int _lastKeyId = 0;
4+
int get lastKeyId => _lastKeyId;
5+
46
Map<int, int> _hashToKey = {};
57

68
class Key {

lib/injector.dart renamed to lib/src/base_injector.dart

Lines changed: 19 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,18 @@
1-
part of di;
1+
library di.base_injector;
2+
3+
import 'provider.dart';
4+
import 'error_helper.dart';
5+
6+
import 'package:collection/collection.dart';
7+
import 'package:di/key.dart';
8+
import 'package:di/di.dart';
29

310
List<Key> _PRIMITIVE_TYPES = new UnmodifiableListView(<Key>[
411
new Key(num), new Key(int), new Key(double), new Key(String),
512
new Key(bool)
613
]);
714

8-
abstract class ObjectFactory {
9-
Object getInstanceByKey(Key key, Injector requester, List resolving);
10-
}
11-
12-
abstract class Injector implements ObjectFactory {
13-
/**
14-
* Name of the injector or null of none is given.
15-
*/
16-
String get name;
17-
18-
/**
19-
* The parent injector or null if root.
20-
*/
21-
Injector get parent;
22-
23-
/**
24-
* The root injector.
25-
*/
26-
Injector get root;
27-
28-
/**
29-
* List of all types which the injector can return
30-
*/
31-
Set<Type> get types;
32-
33-
/**
34-
* Indicates whether injector allows implicit injection -- resolving types
35-
* that were not explicitly bound in the module(s).
36-
*/
37-
bool get allowImplicitInjection;
38-
39-
/**
40-
* Get an instance for given token ([Type]).
41-
*
42-
* If the injector already has an instance for this token, it returns this
43-
* instance. Otherwise, injector resolves all its dependencies, instantiates
44-
* new instance and returns this instance.
45-
*
46-
* If there is no binding for given token, injector asks parent injector.
47-
*
48-
* If there is no parent injector, an implicit binding is used. That is,
49-
* the token ([Type]) is instantiated.
50-
*/
51-
dynamic get(Type type, [Type annotation]);
52-
53-
/**
54-
* Get an instance for given key ([Key]).
55-
*
56-
* If the injector already has an instance for this key, it returns this
57-
* instance. Otherwise, injector resolves all its dependencies, instantiates
58-
* new instance and returns this instance.
59-
*
60-
* If there is no binding for given key, injector asks parent injector.
61-
*/
62-
dynamic getByKey(Key key);
63-
64-
/**
65-
* Create a child injector.
66-
*
67-
* Child injector can override any bindings by adding additional modules.
68-
*
69-
* It also accepts a list of tokens that a new instance should be forced.
70-
* That means, even if some parent injector already has an instance for this
71-
* token, there will be a new instance created in the child injector.
72-
*/
73-
Injector createChild(List<Module> modules,
74-
{List forceNewInstances, String name});
75-
76-
77-
newFromParent(List<Module> modules, String name);
78-
79-
Object newInstanceOf(Type type, ObjectFactory factory, Injector requestor,
80-
resolving);
81-
}
15+
bool _defaultVisibility(_, __) => true;
8216

8317
abstract class BaseInjector implements Injector {
8418

@@ -90,7 +24,7 @@ abstract class BaseInjector implements Injector {
9024

9125
Injector _root;
9226

93-
List<_Provider> _providers;
27+
List<Provider> _providers;
9428
int _providersLen = 0;
9529

9630
final Map<Key, Object> _instances = <Key, Object>{};
@@ -120,16 +54,16 @@ abstract class BaseInjector implements Injector {
12054
BaseInjector this.parent, {this.name, this.allowImplicitInjection}) {
12155
_root = parent == null ? this : parent._root;
12256
var injectorId = new Key(Injector).id;
123-
_providers = new List(_lastKeyId + 1);
124-
_providersLen = _lastKeyId + 1;
57+
_providers = new List(lastKeyId + 1);
58+
_providersLen = lastKeyId + 1;
12559
if (modules != null) {
12660
modules.forEach((module) {
127-
module._bindings.forEach((k, v) {
61+
module.bindings.forEach((k, v) {
12862
_providers[k] = v;
12963
});
13064
});
13165
}
132-
_providers[injectorId] = new _ValueProvider(Injector, this);
66+
_providers[injectorId] = new ValueProvider(Injector, this);
13367
}
13468

13569
@override
@@ -204,7 +138,7 @@ abstract class BaseInjector implements Injector {
204138

205139
if (allowImplicitInjection) {
206140
return new _ProviderWithDefiningInjector(
207-
new _TypeProvider(key.type), this);
141+
new TypeProvider(key.type), this);
208142
}
209143

210144
throw new NoProviderError(error(resolving, 'No provider found for ${key}!', key));
@@ -228,11 +162,11 @@ abstract class BaseInjector implements Injector {
228162
@override
229163
Injector createChild(List<Module> modules,
230164
{List forceNewInstances, String name}) =>
231-
_createChildWithResolvingHistory(modules, BaseInjector.ZERO_DEPTH_RESOLVING,
165+
createChildWithResolvingHistory(modules, BaseInjector.ZERO_DEPTH_RESOLVING,
232166
forceNewInstances: forceNewInstances,
233167
name: name);
234168

235-
Injector _createChildWithResolvingHistory(
169+
Injector createChildWithResolvingHistory(
236170
List<Module> modules,
237171
resolving,
238172
{List forceNewInstances, String name}) {
@@ -247,7 +181,7 @@ abstract class BaseInjector implements Injector {
247181
assert(key is Key);
248182
var providerWithInjector = _getProviderWithInjectorForKey(key, resolving);
249183
var provider = providerWithInjector.provider;
250-
forceNew._keyedFactory(key, (Injector inj) => provider.get(this,
184+
forceNew.factoryByKey(key, (Injector inj) => provider.get(this,
251185
inj, inj, resolving),
252186
visibility: provider.visibility);
253187
});
@@ -261,7 +195,7 @@ abstract class BaseInjector implements Injector {
261195
}
262196

263197
class _ProviderWithDefiningInjector {
264-
final _Provider provider;
198+
final Provider provider;
265199
final BaseInjector injector;
266200
_ProviderWithDefiningInjector(this.provider, this.injector);
267201
}

lib/error_helper.dart renamed to lib/src/error_helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library di.error_helper;
22

3-
import 'package:di/di.dart';
3+
import 'package:di/key.dart';
44

55
String error(List resolving, message, [appendDependency]) {
66
if (appendDependency != null) {
File renamed without changes.

lib/src/injector.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
part of di;
2+
3+
abstract class ObjectFactory {
4+
Object getInstanceByKey(Key key, Injector requester, List resolving);
5+
}
6+
7+
abstract class Injector implements ObjectFactory {
8+
/**
9+
* Name of the injector or null of none is given.
10+
*/
11+
String get name;
12+
13+
/**
14+
* The parent injector or null if root.
15+
*/
16+
Injector get parent;
17+
18+
/**
19+
* The root injector.
20+
*/
21+
Injector get root;
22+
23+
/**
24+
* List of all types which the injector can return
25+
*/
26+
Set<Type> get types;
27+
28+
/**
29+
* Indicates whether injector allows implicit injection -- resolving types
30+
* that were not explicitly bound in the module(s).
31+
*/
32+
bool get allowImplicitInjection;
33+
34+
/**
35+
* Get an instance for given token ([Type]).
36+
*
37+
* If the injector already has an instance for this token, it returns this
38+
* instance. Otherwise, injector resolves all its dependencies, instantiates
39+
* new instance and returns this instance.
40+
*
41+
* If there is no binding for given token, injector asks parent injector.
42+
*
43+
* If there is no parent injector, an implicit binding is used. That is,
44+
* the token ([Type]) is instantiated.
45+
*/
46+
dynamic get(Type type, [Type annotation]);
47+
48+
/**
49+
* Get an instance for given key ([Key]).
50+
*
51+
* If the injector already has an instance for this key, it returns this
52+
* instance. Otherwise, injector resolves all its dependencies, instantiates
53+
* new instance and returns this instance.
54+
*
55+
* If there is no binding for given key, injector asks parent injector.
56+
*/
57+
dynamic getByKey(Key key);
58+
59+
/**
60+
* Create a child injector.
61+
*
62+
* Child injector can override any bindings by adding additional modules.
63+
*
64+
* It also accepts a list of tokens that a new instance should be forced.
65+
* That means, even if some parent injector already has an instance for this
66+
* token, there will be a new instance created in the child injector.
67+
*/
68+
Injector createChild(List<Module> modules,
69+
{List forceNewInstances, String name});
70+
71+
72+
newFromParent(List<Module> modules, String name);
73+
74+
Object newInstanceOf(Type type, ObjectFactory factory, Injector requestor,
75+
resolving);
76+
}

lib/injector_delagate.dart renamed to lib/src/injector_delagate.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
part of di;
1+
library di.injector_delegate;
22

3-
class _InjectorDelagate implements Injector, ObjectFactory {
3+
import 'base_injector.dart';
4+
import 'package:di/di.dart';
5+
import 'package:di/key.dart';
6+
7+
class InjectorDelagate implements Injector, ObjectFactory {
48
BaseInjector _injector;
59
List<Key> _resolving;
610

7-
_InjectorDelagate(this._injector, this._resolving);
11+
InjectorDelagate(this._injector, this._resolving);
812

913
@override
1014
bool get allowImplicitInjection => _injector.allowImplicitInjection;
@@ -36,18 +40,16 @@ class _InjectorDelagate implements Injector, ObjectFactory {
3640
@override
3741
Injector createChild(List<Module> modules,
3842
{List forceNewInstances, String name}) =>
39-
_injector._createChildWithResolvingHistory(modules, _resolving,
43+
_injector.createChildWithResolvingHistory(modules, _resolving,
4044
forceNewInstances: forceNewInstances,
4145
name: name);
4246

4347
@override
44-
newFromParent(List<Module> modules, String name) {
45-
throw new UnimplementedError();
46-
}
48+
newFromParent(List<Module> modules, String name) =>
49+
_injector.newFromParent(modules, name);
4750

4851
@override
4952
Object newInstanceOf(Type type, ObjectFactory factory,
50-
Injector requestor, resolving) {
51-
throw new UnimplementedError();
52-
}
53+
Injector requestor, resolving) =>
54+
_injector.newInstanceOf(type, factory, requestor, resolving);
5355
}
File renamed without changes.

0 commit comments

Comments
 (0)