Skip to content

Correct way of disposing registered resources. #302

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
luizpaulofranz opened this issue Nov 15, 2022 · 1 comment
Closed

Correct way of disposing registered resources. #302

luizpaulofranz opened this issue Nov 15, 2022 · 1 comment

Comments

@luizpaulofranz
Copy link

We are using get_it here to totally handle our dependencies instances, and we are using clean architecture as suggested by reso coder tutorial, so we have several layers (viewModel -> usecase -> repository -> data_source), let me show some snippets.

File structure:

-lib
--feature
----data
------remote_data_source.dart
------concrete_repository.dart
----domain
------abstract_repository.dart
------usecase.dart
----presentation
------my_screen.dart
------my_view_model.dart

we have get_it registration file, like this:

  getIt.registerLazySingleton(
    () => MyViewModel(getIt()),
  );
  getIt.registerLazySingleton(
    () => Usecase(getIt()),
  );
  getIt.registerLazySingleton<AbstractRepository>(
    () => ConcreteRepository(getIt()),
  );
  getIt.registerLazySingleton(
    () => RemoteDataSource(getIt()),
  );

  //this is the last one
  getIt.registerLazySingleton(
    () => CustomHttpClient(),
  );

And what we are doing is: on the screen, we call final viewModel = getIt<MyViewModel>(), and all the instances are retrieved by a cascading call, lets see:

  • Our ViewModel needs a UseCase on it's constructor,
  • UseCase needs a Repository
  • Repository needs a DataSource
  • DataSource needs an ApiClient.

And all of these are being resolved by get_it, so when we call: final viewModel = getIt<MyViewModel>().
get_it is creating instances of MyViewModel, Usecase, ConcreteRepository, RemoteDataSource and CustomHttpClient, right?

This is really fantastic, because we don't have to worry about objects instantiations and it works well.

My questions

1 - If I call resetLazySingleton<MyViewModel>() on dispose method of my screen, will get_it cascading reset all the other dependencies?

The real question is:
What is the correct way to dispose these objects when we don't need them anymore?

@escamoteur
Copy link
Collaborator

I know that it's been a long time, but I had to make a longer break due to health issues.
The short answer to your question is 'no', reset is not going to cascade.

Two questions come to my mind. if your objects are only that short-lived, why not using registerFactory and let the garbage collector take care about it.
The other option would be using a GetIt scope, meaning you register the objects on creation in a new scope and pop that scope in the widgets dispose function. The get_it_mixin offers an automatic function for that.

thinking a bit more about your scenario, it might be a nice addition to GetIt to add a resetLazySingeltonsInScope.
#320

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

2 participants