Home > Blockchain >  why should I use @Inject annotation? (android)
why should I use @Inject annotation? (android)

Time:09-30

    class DataRepository @Inject constructor(private val dataDao: DataDao) { }

    class DataRepository constructor(private val dataDao: DataDao) { }

I don't know what's diffence between two classes. Can any one tell me please?

CodePudding user response:

The main difference is - @Inject is an annotation used by Dagger (or Koin), and it will automatically create the DataRepository class given that a DataDao is provided (or bound).

The other one is just a regular constructor.

You can still manually instantiate classes by calling their inject constructors by hand, although - when using a DI framework (dependency injection) there is no need to, that's what DI is for. To construct and instantiate things for you

  • Related