add Container.has<T>

This commit is contained in:
Tobe O 2018-08-26 18:55:21 -04:00
parent 63ea990185
commit 4500f33ae9
4 changed files with 66 additions and 12 deletions

View file

@ -1,3 +1,6 @@
# 1.0.0-alpha.9
* Added `Container.has<T>`.
# 1.0.0-alpha.8 # 1.0.0-alpha.8
* Fixed a bug where `_ReflectedTypeInstance.isAssignableTo` always failed. * Fixed a bug where `_ReflectedTypeInstance.isAssignableTo` always failed.
* Added `@GenerateReflector` annotation. * Added `@GenerateReflector` annotation.

View file

@ -20,6 +20,23 @@ class Container {
return new Container._child(this); return new Container._child(this);
} }
bool has<T>([Type t]) {
var search = this;
t ??= T == dynamic ? t : T;
while (search != null) {
if (search._singletons.containsKey(t)) {
return true;
} else if (search._factories.containsKey(t)) {
return true;
} else {
search = search._parent;
}
}
return false;
}
/// Instantiates an instance of [T]. /// Instantiates an instance of [T].
/// ///
/// In contexts where a static generic type cannot be used, use /// In contexts where a static generic type cannot be used, use
@ -27,22 +44,14 @@ class Container {
T make<T>([Type type]) { T make<T>([Type type]) {
type ??= T; type ??= T;
// Find a singleton, if any.
var search = this; var search = this;
while (search != null) { while (search != null) {
if (search._singletons.containsKey(type)) { if (search._singletons.containsKey(type)) {
// Find a singleton, if any.
return search._singletons[type] as T; return search._singletons[type] as T;
} else { } else if (search._factories.containsKey(type)) {
search = search._parent;
}
}
// Find a factory, if any. // Find a factory, if any.
search = this;
while (search != null) {
if (search._factories.containsKey(type)) {
return search._factories[type](this) as T; return search._factories[type](this) as T;
} else { } else {
search = search._parent; search = search._parent;

View file

@ -1,5 +1,5 @@
name: angel_container name: angel_container
version: 1.0.0-alpha.8 version: 1.0.0-alpha.9
author: Tobe O <thosakwe@gmail.com> 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." 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 homepage: https://github.com/angel-dart/container.git

View file

@ -0,0 +1,42 @@
import 'package:angel_container/angel_container.dart';
import 'package:test/test.dart';
void main() {
Container container;
setUp(() {
container = new Container(const EmptyReflector())
..registerSingleton<Song>(new Song(title: 'I Wish'))
..registerFactory<Artist>((container) {
return new Artist(
name: 'Stevie Wonder',
song: container.make<Song>(),
);
});
});
test('has on singleton', () {
expect(container.has<Song>(), true);
});
test('has on factory', () {
expect(container.has<Artist>(), true);
});
test('false if neither', () {
expect(container.has<bool>(), false);
});
}
class Artist {
final String name;
final Song song;
Artist({this.name, this.song});
}
class Song {
final String title;
Song({this.title});
}