Skip to content

Registration ordering #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrath opened this issue May 19, 2021 · 5 comments
Closed

Registration ordering #195

chrath opened this issue May 19, 2021 · 5 comments

Comments

@chrath
Copy link

chrath commented May 19, 2021

We would like to use get_it for our project, but we are confused how the registration process works. Did we understand the documentation correctly that we need to register the dependencies manually in the correct order?

Let's say we have a ServiceA and ServiceB which depends on ServiceA. We then manually need to make sure that we first register ServiceA before we register ServiceB?

// this works
_getIt.registerSingleton<ServiceA>(ServiceA());
_getIt.registerSingleton<ServiceB>(ServiceB(_getIt<ServiceA>));

// this results in a type ServiceA is not registered inside GetIt.
_getIt.registerSingleton<ServiceB>(ServiceB(_getIt<ServiceA>));
_getIt.registerSingleton<ServiceA>(ServiceA());

Our previous IoC container was able to resolve the dependencies automatically. If we switch to get_it, we would have to manually reorder about a hundred dependencies. Even once the dependencies are ordered, each new dependency registration must also be in the right place. With many dependencies, this can be quite difficult to maintain.

Is this a current limitation of get_it or did we misunderstand the documentation? And do you know a strategy how to deal with this kind of issue? Anyways, thanks for your great work!

@mozerrr
Copy link

mozerrr commented Jun 11, 2021

You can use registerSingletonWithDependencies, check docs here.

@escamoteur
Copy link
Collaborator

In general, yes you have to ensure the corrcect sequence of registration. there is support to esure that asynchronous created objects are available before other objects need to acces them but they have to be registered in the correct sequence.
Without Reflection I think it's not really possible to avoid that.
Which IoC container did you use?

@chrath
Copy link
Author

chrath commented Jun 25, 2021

Thanks for your reply. We are currently using this package here: https://pub.dev/packages/flutter_simple_dependency_injection. I don't know in detail how it works, but we do not need to care about the ordering of registrations.

@escamoteur
Copy link
Collaborator

I think I understand the difference now. Your other package uses always factory functions which means that you can't run into a dependency problem while you register all your types. If you register all your types as lazySingletons you should get the same behavior.

@chrath
Copy link
Author

chrath commented Jul 1, 2021

Thanks for the explanation. We'll have a look and try the lazySingletons. Thanks for you help!

@chrath chrath closed this as completed Jul 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants