sooyeon
sooyeon

Reputation: 529

why should I use @Inject annotation? (android)

    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?

Upvotes: 1

Views: 1314

Answers (1)

Shark
Shark

Reputation: 6610

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

Upvotes: 1

Related Questions