Flutterando / auto_injector

Dependency injection system. But without build_runner :)
https://pub.dev/packages/auto_injector
Other
54 stars 15 forks source link

Nested Injectors: Each injector responsible for its instances #5

Closed davidsdearaujo closed 1 year ago

davidsdearaujo commented 1 year ago

The problem

I think each module should take care of its binds AND instances.

Let's suppose you create nested injectors: parentInjector and innerInjector. In this example innerInjector have a lazy singleton bind of Class1.

If you ask for Class1 for innerInjector or parentInjector, they should return the same instance, right?

Well, it's not happening like that. When the injectors are nested, the main injector make a copy of all the inner injectors to it's own binds, so when you ask to parentInjector and innerInjector the same bind, they return different instances.

Expected Behavior

The expected behavior was that the innerInjector would be able to create the instances for its binds and parentInjector would just ask it to do so.

What does this PR do

This PR makes each injector take care of it's own binds and instances. When there are nested injectors, the injector find in its own binds at first and if the bind is not found then it ask to the inner injectors.