This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
class GreetingServiceImpl @Inject constructor(private val messageData: MessageData, private val timeService: TimeService) : | |
GreetingService { | |
@Singleton | |
class TimeServiceImpl @Inject constructor() : TimeService { | |
data class MessageData @Inject constructor(val welcomeMessage: String) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(MockitoJUnitRunner::class) | |
class GreetingServiceTest { | |
private lateinit var greetingService: GreetingService | |
@Mock | |
lateinit var timeService: TimeService | |
@Before | |
fun setup() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
@Inject | |
lateinit var greetingService: GreetingService | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
(application as DaggerApplication).appComponent.inject(this) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DaggerApplication : Application() { | |
val appComponent: AppComponent by lazy { | |
DaggerAppComponent.factory().create("welcome to Dagger") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
@Component(modules = [DaggerModule::class]) | |
interface AppComponent { | |
fun inject(mainActivity: MainActivity) | |
@Component.Factory | |
interface Factory { | |
fun create(@BindsInstance welcomeMessage: String): AppComponent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
interface DaggerModule { | |
@Binds | |
fun provideTimeService(impl: TimeServiceImpl): TimeService | |
@Binds | |
fun provideGreetingSersvice(impl: GreetingServiceImpl): GreetingService | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GreetingServiceTest { | |
@Test | |
fun testGreetingInTheMorning() { | |
val testContainer = Kodein { | |
import(kodeinModule) | |
bind<TimeService>(overrides = true) with singleton { | |
object : TimeService { | |
override fun getHourOfDay(): Int = 9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity(), KodeinAware { | |
override val kodein by closestKodein() | |
private val greetingService by instance<GreetingService>() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KodeinApplication : Application(), KodeinAware { | |
override val kodein = Kodein.lazy { | |
import(kodeinModule) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val kodeinModule = Kodein.Module(name = "appmodule") { | |
bind() from singleton { MessageData() } | |
bind<TimeService>() with singleton { TimeServiceImpl() } | |
bind<GreetingService>() with singleton { | |
GreetingServiceImpl(instance(), instance()) } | |
} |