kohesive / injekt

Dependency Injection for Kotlin
MIT License
235 stars 20 forks source link

Linked Scopes - minispec #31

Open apatrida opened 8 years ago

apatrida commented 8 years ago

Not sure if this is useful, because you can manually get pretty close (see updated docs at https://github.com/kohesive/injekt/blob/master/README.md#scopes) using something like the shown MyActivityScope or MyActivityModule examples.

  1. Scope can be created and linked to another scope (or more than one scope). The scope you create is the “called scope” and the others are “linked scopes"
  2. The linking has the following properties: is the Linked scope allowed to provide existing cached instances (true by default), is Linked scope allowed to generate new instances (false by default), are new instances are cached in the called scope or the scope who’s factory actually makes the object which is either called scope or linked scope (called scope is the default).

so you would make myActivityScope linked to Injekt global

and anything already in global can be used, but it cannot create anything else, only myActivityScope can create new instances. And anything registered in myActivityScope will override the global Injekt by default

you can change the behaviour by the settings listed in # 2

So this would be called something like LocalOverridingScope with those defaults

others will want the settings to differ, such as having the global still make new instances of types it has registered but the other scope does not.

others will want the caching of the instances to be in the called scope, others in the scope that did the factory

others will want the factory called if the local scope does not have the item, but declare the factories only in the linked scope (including singleton factories)

apatrida commented 8 years ago

see related #32, and SO http://stackoverflow.com/questions/35315548/in-the-injekt-library-for-kotlin-how-do-i-create-local-scopes-such-as-for-each

apatrida commented 8 years ago

32 might be good enough solution not requiring #31