Injector silently ignoring definitions
Closed this issue · 0 comments
sellmair commented
One Issue I found when creating PR #4 is that I had to add an Injector.clearDependencies()
in front of the Singleton test to get it working like:
test('Register singleton / Get singleton - Test', () {
injector.clearDependencies();
...
The reason is, that trying to register a singleton dependency will silently be ignored if the dependency is already declared as factory!
Check out this snippet from the registerSingleton:
void registerSingleton<T>(Factory<T> factory) {
int key = T.hashCode;
if (T == dynamic) {
throw Exception(
"No type specified !\nCan not register dependencies for type \"$T\"");
}
// TODO: Should also check the factory map!
if (_singletonMap.containsKey(key)) {
throw Exception("type \"$T\" already defined !");
}
_singletonFactoryMap[key] = factory;
}
The factory definition will be used because it is preferred in getDependency
T getDependency<T>() {
// ...
if (_factoryMap.containsKey(type)) {
// get it from the factory
} else {
// get it from the singletons (factory or instance)
}