Senior Software Engineer @ Acorns
class Service() : ServiceInterface {
fun doStatic() {
StaticClass.doStuff()
}
}
interface ServiceInterface {
fun doStatic()
}
class DIClient(service: ServiceInterface) {
init {
service.doStatic()
}
}
class Client() {
init {
StaticClass.doStuff()
}
}
class Client constuctor(
val service: Service
) { }
class Client() {
private lateinit var service: Service
fun setService(service: Service) {
this.service = service
}
}
interface ServiceSetter {
fun setService(service: Service)
}
class Client() : ServiceSetter {
private lateinit var service: Service
override fun setService(service: Service) {
this.service = service
}
}
class ServiceInjector() {
fun inject(client: ServiceSetter) {
client.setService(Service())
}
}
@Module
interface AppModule {
@Binds
fun provideService(service: ServiceImpl): Service
}
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun inject(activity: MainActivity)
}
class MyApplication : Application() {
val appComponent: AppComponent by lazy {
DaggerAppComponent.create()
}
}
class MainActivity : Activity() {
@Inject
lateinit var service: Service
fun onCreate(savedInstanceState: Bundle?) {
(application as MyApplication).appComponent.inject(this)
}
}
@HiltAndroidApp
class MyApplication : Application() {
}
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var service: Service
}
val appModule = module {
single<Service> { ServiceImpl() }
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
modules(appModule)
}
}
}
class MainActivity : Activity() {
private val service by inject<Service>()
}
val appModule = Kodein.Module(name = "appModule") {
bind<Service>() with singleton { Service() }
}
class MyApplication : Application(), KodeinAware {
override val kodein = Kodein.lazy {
import(appModule)
}
}
class MainActivity : Activity(), KodeinAware {
override val kodein by closestKodein()
private val service by instance<Service>()
}
DI Deep Dive with Dagger
Wednesday, April 28th @ 6:30pm