Koin: Assisted injection?

Created on 4 Oct 2017  路  4Comments  路  Source: InsertKoinIO/koin

Hi! Nice library, really like it!
But I can't get how we can do so called assisted injection. Imagine I have MyPresenter(val someId: Int, val interactor: SomeInteractor), where someId is dynamic parameter that is known only by the time MyView (a Fragment) is created.

How I can provide the MyPresenter in this case?

Most helpful comment

Hi Jeevuz! Thanks :)

I think, you have 2 ways to do it:
In Koin module

  • declare a property injection for your MyPresenter component with:
    provide {MyPresenter(getProperty("someId"),get())
  • provide this property from your activity or your fragment, before using any injection of MyPresenter: getKoin().setProperty("someId",/* your value */)
  • when you will get the injected MyPresenter component, constructor will be filled with the given property - resolution will be done with the last value of your property. Be aware to drop it (release scope) if it needs any change.

Or if you use an MVP approach, you could directly set your id on your MyPresenter, by your fragment. Once your presenter is injected, fill your value presenter.someId = someId

try the first one if it fits your needs.

All 4 comments

Hi Jeevuz! Thanks :)

I think, you have 2 ways to do it:
In Koin module

  • declare a property injection for your MyPresenter component with:
    provide {MyPresenter(getProperty("someId"),get())
  • provide this property from your activity or your fragment, before using any injection of MyPresenter: getKoin().setProperty("someId",/* your value */)
  • when you will get the injected MyPresenter component, constructor will be filled with the given property - resolution will be done with the last value of your property. Be aware to drop it (release scope) if it needs any change.

Or if you use an MVP approach, you could directly set your id on your MyPresenter, by your fragment. Once your presenter is injected, fill your value presenter.someId = someId

try the first one if it fits your needs.

Thanks for the quick response!
Interesting way with the property.
Don't sure I understood your last point correctly. Is there the way to release the property? (It would be good for using the same property many times for different presenters. For example if I create first and put general PresenterParams for this and later release it and use for the second presenters creation.)

Properties cannot be dropped for now. But you overwrite existing property, each time you use getKoin().setProperty() on the same key.

袨泻, I got it. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ILAgent picture ILAgent  路  3Comments

iRYO400 picture iRYO400  路  3Comments

TedHoryczun picture TedHoryczun  路  4Comments

guymclean picture guymclean  路  3Comments

AHarazim picture AHarazim  路  3Comments