add Container.has<T>
This commit is contained in:
parent
63ea990185
commit
4500f33ae9
4 changed files with 66 additions and 12 deletions
|
@ -1,3 +1,6 @@
|
|||
# 1.0.0-alpha.9
|
||||
* Added `Container.has<T>`.
|
||||
|
||||
# 1.0.0-alpha.8
|
||||
* Fixed a bug where `_ReflectedTypeInstance.isAssignableTo` always failed.
|
||||
* Added `@GenerateReflector` annotation.
|
||||
|
|
|
@ -20,6 +20,23 @@ class Container {
|
|||
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].
|
||||
///
|
||||
/// In contexts where a static generic type cannot be used, use
|
||||
|
@ -27,22 +44,14 @@ class Container {
|
|||
T make<T>([Type type]) {
|
||||
type ??= T;
|
||||
|
||||
// Find a singleton, if any.
|
||||
var search = this;
|
||||
|
||||
while (search != null) {
|
||||
if (search._singletons.containsKey(type)) {
|
||||
// Find a singleton, if any.
|
||||
return search._singletons[type] as T;
|
||||
} else {
|
||||
search = search._parent;
|
||||
}
|
||||
}
|
||||
|
||||
// Find a factory, if any.
|
||||
search = this;
|
||||
|
||||
while (search != null) {
|
||||
if (search._factories.containsKey(type)) {
|
||||
} else if (search._factories.containsKey(type)) {
|
||||
// Find a factory, if any.
|
||||
return search._factories[type](this) as T;
|
||||
} else {
|
||||
search = search._parent;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel_container
|
||||
version: 1.0.0-alpha.8
|
||||
version: 1.0.0-alpha.9
|
||||
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
|
||||
|
|
42
angel_container/test/has_test.dart
Normal file
42
angel_container/test/has_test.dart
Normal 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});
|
||||
}
|
Loading…
Reference in a new issue