This commit is contained in:
Tobe O 2019-04-10 15:20:02 -04:00
parent bdf8e79d1d
commit 0144ebe34e
4 changed files with 28 additions and 1 deletions

View file

@ -1,3 +1,6 @@
# 1.0.1
* Added `hasNamed`.
# 1.0.0
* Removed `@GenerateReflector`.

View file

@ -21,6 +21,7 @@ class Container {
return new Container._child(this);
}
/// Determines if the container has an injection of the given type.
bool has<T>([Type t]) {
var search = this;
t ??= T == dynamic ? t : T;
@ -38,6 +39,21 @@ class Container {
return false;
}
/// Determines if the container has a named singleton with the given [name].
bool hasNamed(String name) {
var search = this;
while (search != null) {
if (search._namedSingletons.containsKey(name)) {
return true;
} else {
search = search._parent;
}
}
return false;
}
/// Instantiates an instance of [T].
///
/// In contexts where a static generic type cannot be used, use

View file

@ -1,5 +1,5 @@
name: angel_container
version: 1.0.0
version: 1.0.1
author: Tobe O <thosakwe@gmail.com>
description: "A better IoC container and dependency injector for Angel, ultimately allowing Angel to be used without dart:mirrors."
homepage: https://github.com/angel-dart/container.git

View file

@ -7,6 +7,7 @@ void main() {
setUp(() {
container = new Container(const EmptyReflector())
..registerSingleton<Song>(new Song(title: 'I Wish'))
..registerNamedSingleton('foo', 1)
..registerFactory<Artist>((container) {
return new Artist(
name: 'Stevie Wonder',
@ -15,6 +16,13 @@ void main() {
});
});
test('hasNamed', () {
var child = container.createChild()..registerNamedSingleton('bar', 2);
expect(child.hasNamed('foo'), true);
expect(child.hasNamed('bar'), true);
expect(child.hasNamed('baz'), false);
});
test('has on singleton', () {
expect(container.has<Song>(), true);
});