fluttercommunity / get_it

Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur
https://pub.dev/packages/get_it
MIT License
1.36k stars 149 forks source link

Registering as dynamic #2

Closed marcguilera closed 5 years ago

marcguilera commented 6 years ago

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 as dynamic so it fails when doing getIt.get<Hello>() plus it throws if I add something else because dynamic has already been registered.

What am I missing?

escamoteur commented 6 years ago

What do you mean by

registered Hello as dynamic?

Could you show a bit more code please?

marcguilera commented 6 years ago

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: pic

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
marcguilera commented 6 years ago

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"
escamoteur commented 6 years ago

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?

escamoteur commented 6 years ago

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.