Closed marcguilera closed 5 years ago
What do you mean by
registered Hello as dynamic?
Could you show a bit more code please?
Sure I'm sorry. I have an injector builder that looks like this:
import 'package:quiver/check.dart';
import 'iinjector.dart';
import 'injector_impl.dart';
typedef Type Factory<Type>(IInjector injector);
class InjectorBuilder {
final InjectorImpl _injector = new InjectorImpl();
InjectorBuilder registerSingleton<Type>(Factory<Type> factory) {
checkNotNull(factory, message: () => "factory is null");
_injector.registerLazySingleton(() => factory(_injector));
return this;
}
InjectorBuilder registerFactory<Type>(Factory<Type> factory) {
checkNotNull(factory, message: () => "factory is null");
_injector.registerFactory(() => factory(_injector));
return this;
}
InjectorBuilder registerAlias<Type, Alias extends Type>() {
_injector.get<Type>(); // Will throw if type is not declared
_injector.registerFactory<Alias>(() => _injector.get<Type>());
return this;
}
IInjector build() {
return _injector;
}
}
InjectorImpl is just extending the GetIt class.
When I run tests on it this is what I find:
name: lib.core.di
description: A starting point for Dart libraries or applications.
version: 0.0.1
#homepage: https://www.example.com
#author: marc <email@example.com>
environment:
sdk: '>=1.20.1 <2.0.0'
dependencies:
get_it: ^1.0.0+2
lib.core.common:
path: ../common
dev_dependencies:
test: ^0.12.0
For the record:
dart --version
Dart VM version: 2.0.0-dev.58.0 (Wed May 23 20:44:51 2018 +0200) on "macos_x64"
May I ask why you do this? Seems you destroy the slick use of GetIt that way.
Still not sure what your problem is. Could you make a simple repro where you register and then show me the problem?
One important detail that just came up today:
Extremely important if you use GetIt: ALWAYS use the same style to import your project files either as relative paths OR as package which I recommend. DON'T mix them because currently Dart treats types imported in different ways as two different types although both reference the same file.
Otherwise it won't be able to lookup a type.
Hi thanks for the work, this is exactly what I was looking for.
I am tinkering with this and ran some unit tests. Basically calling
getIt.registerFactory<Hello>(() => new Hello());
registered Hello asdynamic
so it fails when doinggetIt.get<Hello>()
plus it throws if I add something else becausedynamic
has already been registered.What am I missing?