// DIΛಋೖ͍ͯ͠ͳ͍ίʔυ
// ApiClientͱUserDaoʹґଘͨ͠ϦϙδτϦΫϥε
class UserRepository {
private val apiClient: ApiClient = ApiClientImpl()
private val userDao: UserDao = UserDaoImpl()
fun find(id:Long): Single =
return if (userDao.cached) {
userDao.find(id)
} else {
apiClient.getUser(id)
}
}
// UserRepositoryΛ͏ଆ
val repo = UserRepository()
repo.find(123)...
Slide 11
Slide 11 text
// DIΛಋೖ͍ͯ͠ͳ͍ίʔυ
// ApiClientͱUserDaoʹґଘͨ͠ϦϙδτϦΫϥε
class UserRepository {
private val apiClient: ApiClient = ApiClientImpl()
private val userDao: UserDao = UserDaoImpl()
fun find(id:Long): Single =
return if (userDao.cached) {
userDao.find(id)
} else {
apiClient.getUser(id)
}
}
// UserRepositoryΛ͏ଆ
val repo = UserRepository()
repo.find(123)...
ґଘΦϒδΣΫτͱ
ີ݁߹͍ͯ͠Δ
Slide 12
Slide 12 text
// ApiClientͱUserDaoʹґଘͨ͠ϦϙδτϦΫϥε
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id:Long): Single =
return if (userDao.cached) {
userDao.find(id)
} else {
apiClient.getUser(id)
}
}
// UserRepositoryΛ͏ଆ
val apiClient = ApiClientImpl()
val userDao = UserDaoImpl()
val repo = UserRepository(apiClient, userDao)
repo.find(123)...
Slide 13
Slide 13 text
// ApiClientͱUserDaoʹґଘͨ͠ϦϙδτϦΫϥε
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id:Long): Single =
return if (userDao.cached) {
userDao.find(id)
} else {
apiClient.getUser(id)
}
}
// UserRepositoryΛ͏ଆ
val apiClient = ApiClientImpl()
val userDao = UserDaoImpl()
val repo = UserRepository(apiClient, userDao)
repo.find(123)...
ίϯετϥΫλͰ
ґଘΦϒδΣΫτΛ
ड͚औΔ
Slide 14
Slide 14 text
// ApiClientͱUserDaoʹґଘͨ͠ϦϙδτϦΫϥε
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id:Long): Single =
return if (userDao.cached) {
userDao.find(id)
} else {
apiClient.getUser(id)
}
}
// UserRepositoryΛ͏ଆ
val apiClient = ApiClientImpl()
val userDao = UserDaoImpl()
val repo = UserRepository(apiClient, userDao)
repo.find(123)...
͏ଆ͕ґଘΦϒδΣΫτ͔Β
Έཱ͍ͯͯΔ
Slide 15
Slide 15 text
// ґଘΦϒδΣΫτΛղܾ͢ΔͨΊͷίϯςφΦϒδΣΫτ
object Container {
val apiClient: ApiClient = ApiClientImpl()
val userDao: UserDao = UserDaoImpl()
val userRepo: UserRepository =
UserRepository(apiClient, userDao)
}
Slide 16
Slide 16 text
// ApiClientͱUserDaoʹґଘͨ͠ϦϙδτϦΫϥε
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id: Long): Single =
return if (userDao.cached) {
userDao.find(id)
} else {
apiClient.getUser(id)
}
}
// UserRepositoryΛ͏ଆ
val repo = Container.userRepo
repo.find(123)...
UserRepositoryͷґଘؔ
͕ഉআ͞Εͨ
class ApiClientImpl private constructor(): ApiClient {
class Builder {
fun build(): ApiClient {...}
}
override fun getUser(id: Long): Single {...}
}
class UserDaoImpl(): UserDao {
override fun find(id: Long): Single {...}
}
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id: Long): Single {...}
}
Slide 46
Slide 46 text
class ApiClientImpl private constructor(): ApiClient {
class Builder {
fun build(): ApiClient {...}
}
override fun getUser(id: Long): Single {...}
}
class UserDaoImpl(): UserDao {
override fun find(id: Long): Single {...}
}
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id: Long): Single {...}
}
ࣗಈੜͰ͖Δ"
=>Moduleʹॻ͘ඞཁͳ͠
ࣗಈੜͰ͖ͳ͍#
=> Moduleʹॻ͘ඞཁ͋Γ
Slide 47
Slide 47 text
@Module
class AppModule {
@Singleton
@Provides
fun provideApiClient(): ApiClient {
return ApiClientImpl.Builder().build()
}
}
Slide 48
Slide 48 text
@Module
class AppModule {
@Singleton
@Provides
fun provideApiClient(): ApiClient {
return ApiClientImpl.Builder().build()
}
}
Dagger2ͷϞδϡʔϧΛҙຯ͢Δ
Slide 49
Slide 49 text
@Module
class AppModule {
@Singleton
@Provides
fun provideApiClient(): ApiClient {
return ApiClientImpl.Builder().build()
}
}
Slide 50
Slide 50 text
@Module
class AppModule {
@Singleton
@Provides
fun provideApiClient(): ApiClient {
return ApiClientImpl.Builder().build()
}
}
͜ͷϝιουΛґଘղܾʹ͏
Slide 51
Slide 51 text
@Module
class AppModule {
@Singleton
@Provides
fun provideApiClient(): ApiClient {
return ApiClientImpl.Builder().build()
}
}
ApiClientͷείʔϓ
Slide 52
Slide 52 text
class ApiClientImpl private constructor(): ApiClient {
class Builder {
fun build(): ApiClient {...}
}
override fun getUser(id: Long): Single {...}
}
class UserDaoImpl(): UserDao {
override fun find(id: Long): Single {...}
}
class UserRepository(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id: Long): Single {...}
}
ࣗಈੜͰ͖Δ"
=>Moduleʹॻ͘ඞཁͳ͠
ࣗಈੜͰ͖ͳ͍#
=> Moduleʹॻ͘ඞཁ͋Γ
Slide 53
Slide 53 text
@Singleton
class UserRepository @Inject constructor(
private val apiClient: ApiClient,
private val userDao: UserDao
) {
fun find(id: Long): Single {...}
}
͜ͷίϯετϥΫλΛͬͯΠϯελϯεԽ͞ΕΔ
Slide 54
Slide 54 text
@Module
abstract class ActivityModule {
@ActivityScope
@ContributesAndroidInjector
abstract fun contributeHogeActivity(): HogeActivity
}
dagger.androidΛ͏ͨΊͷಛघͳϞδϡʔϧ
@ProvidesϝιουΛ࣋ͨͳ͍
Slide 55
Slide 55 text
@Module
abstract class ActivityModule {
@ActivityScope
@ContributesAndroidInjector
abstract fun contributeHogeActivity(): HogeActivity
}
HogeActivityʹೖ͢ΔͨΊͷϝιου
// ςετରͷΫϥεʢDIಋೖલʣ
class UserRepository {
private val apiClient: ApiClient = ApiClientImpl()
fun find(id: Long): Single =
apiClient.getUser(id)
}
ApiClientʹີ݁߹͍ͯ͠Δ
Slide 86
Slide 86 text
// UserRepositoryͷςετίʔυ
class UserRepositoryTest {
@Test
fun find_isSuccess() {
val repo = UserRepository()
repo.find(1)
.test()
.awaitCount(1)
.assertValue(expected)
}
}
Slide 87
Slide 87 text
// UserRepositoryͷςετίʔυ
class UserRepositoryTest {
@Test
fun find_isSuccess() {
val repo = UserRepository()
repo.find(1)
.test()
.awaitCount(1)
.assertValue(expected)
}
}
ApiClientImplͷ
ίʔυ࣮ߦ͍ͯ͠Δ
// ςετରͷΫϥεʢDIಋೖޙʣ
class UserRepository(
private val apiClient: ApiClient
) {
fun find(id: Long): Single =
apiClient.getUser(id)
}
ApiClientΛ
֎෦͔ΒೖՄೳ
Slide 92
Slide 92 text
// UserRepositoryͷςετίʔυ
class UserRepositoryTest {
@Test
fun find_isSuccess() {
val apiClient = mockk {
every { getUser(any()) } returns Single.just(user)
}
val repo = UserRepository(apiClient)
repo.find(1)
.test()
.awaitCount(1)
.assertValue(user)
}
}
Slide 93
Slide 93 text
// UserRepositoryͷςετίʔυ
class UserRepositoryTest {
@Test
fun find_isSuccess() {
val apiClient = mockk {
every { getUser(any()) } returns Single.just(user)
}
val repo = UserRepository(apiClient)
repo.find(1)
.test()
.awaitCount(1)
.assertValue(user)
}
}
ApiClientͷϞοΫΛ࡞
Slide 94
Slide 94 text
// UserRepositoryͷςετίʔυ
class UserRepositoryTest {
@Test
fun find_isSuccess() {
val apiClient = mockk {
every { getUser(any()) } returns Single.just(user)
}
val repo = UserRepository(apiClient)
repo.find(1)
.test()
.awaitCount(1)
.assertValue(user)
}
}
ϞοΫΛೖͯ͠
ςετ࣮ߦ