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

getIt.reset() should dispose Singletons in reverse registration order #334

Closed brookman closed 10 months ago

brookman commented 1 year ago

We are using get_it together with injectable to initialize a graph (DAG) of Singletons in our app. Injectable makes sure that the "parent" Singletons are initialized before its dependents.

Coming from other dependency injection frameworks/containers; when calling getIt.reset() upon shutting down the app I would expect the dispose process to work in reverse order. Starting at the "leaves" and continue with the parent dependencies so that a dependency is never disposed as long as there are others depending upon it. This would make it straight forward to dispose resources, like disconnecting from a socket.

Currently the order seems to be the same as how the Singletons were registered. I have tried both implementing Disposable and using the @disposeMethod annotation which automatically generates a dispose call-back. Am I missing something?

After taking a look at the code, it seems like flipping the order how allFactories is iterated in _Scope.reset() might just do the trick - assuming Dart Maps keep their values ordered by insertion (which might not be the case).

escamoteur commented 1 year ago

I wonder if that could also be important for resetting Scopes. To store the registration order of all objects should be  possible with an extra list

Thomas Am 26. Juni 2023, 22:01 +0200 schrieb Beni Bachmann @.***>:

We are using get_it together with injectable to initialize a graph (DAG) of Singletons in our app. Injectable makes sure that the "parent" Singletons are initialized before its dependents. Coming from other dependency injection frameworks/containers; when calling getIt.reset() upon shutting down the app I would expect the dispose process to work in reverse order. Starting at the "leaves" and continue with the parent dependencies so that a dependency is never disposed as long as there are others depending upon it. This would make it straight forward to dispose resources, like disconnecting from a socket. Currently the order seems to be the same as how the Singletons are registered. I have tried both implementing Disposable and using the @disposeMethod annotation which automatically generates a dispose call-back. Am I missing something? After taking a look at the code, it seems like flipping the order how allFactories is iterated in _Scope.reset() might just do the trick - assuming Dart Maps keep their values ordered by insertion (which not be the case). — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

brookman commented 1 year ago

Looking at this code it seems like the scopes are already iterated in reverse order upon resetting: image (Unless I misunderstand something)

escamoteur commented 1 year ago

Yeah, you are right, scopes are. I probably need to add another list to scopes to save the registration order Am 10. Juli 2023, 11:32 +0200 schrieb Beni Bachmann @.***>:

Looking at this code it seems like the scopes are already iterated in reverse order upon resetting:

(Unless I misunderstand something) — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

escamoteur commented 10 months ago

was implemented by https://github.com/fluttercommunity/get_it/pull/350