dependency injection mastery
4
总安装量
0
周安装量
#49256
全站排名
安装命令
npx skills add https://github.com/fwrite0920/android-skills --skill 'Dependency Injection Mastery'
Skill 文档
Dependency Injection Mastery (ä¾èµæ³¨å ¥ä¸ç²¾)
Instructions
- 确认é®é¢å±äº DI æ¶ææ Scope 设计
- ä¾ç §ä¸æ¹ç« è顺åºå¥ç¨
- 䏿¬¡åªè°æ´ä¸ç§æ³¨å ¥æ¨¡å¼æ module
- 宿åå¯¹ç § Quick Checklist
When to Use
- Scenario Aï¼æ°é¡¹ç® DI æ¶æå建
- Scenario Cï¼æ§é¡¹ç®ç°ä»£å䏿¨¡åæå
- Scenario Fï¼KMP å ±ç¨æ¨¡åç DI è°æ´
Example Prompts
- “请åè Assisted Injectionï¼æ¿ ViewModel å å ¥å¨æåæ°”
- “ä¾ç § Custom Hilt Components å建 User Session Scope”
- “è¯·ç¨ Multi-binding ç« è设计æä»¶å¼æ¯ä»æ¨¡å”
Workflow
- å 确认 Assisted Injection ä¸ Scope éæ±
- åæ´ç Module Organization ä¸ Qualifier
- æåç¨ Quick Checklist éªæ¶
Practical Notes (2026)
- 夿¨¡åé API/impl å离ï¼é¿å 跨模åç´æ¥ä¾èµå®ä½
- 跨模å导èªä¸ Service 以 interface + EntryPoint ç»ä¸
- Scope 设计å ç»åºçå½å¨æï¼åè½å°å° Module
Minimal Template
ç®æ :
Scope:
Module ç»æ:
注å
¥æ¨¡å¼:
éªæ¶: Quick Checklist
Assisted Injection
å½ ViewModel æ Worker éè¦åæ¶æ¥æ¶ DI çä¾èµä¸ Runtime åæ°æ¶ä½¿ç¨ã
ViewModel with SavedStateHandle + Custom Args
@HiltViewModel(assistedFactory = DetailViewModel.Factory::class)
class DetailViewModel @AssistedInject constructor(
@Assisted private val productId: String,
@Assisted savedStateHandle: SavedStateHandle,
private val repository: ProductRepository
) : ViewModel() {
@AssistedFactory
interface Factory {
fun create(productId: String, savedStateHandle: SavedStateHandle): DetailViewModel
}
}
// å¨ Compose ä¸ä½¿ç¨
@Composable
fun DetailScreen(productId: String) {
val viewModel = hiltViewModel<DetailViewModel, DetailViewModel.Factory> { factory ->
factory.create(productId, createSavedStateHandle())
}
}
WorkManager with Assisted Injection
@HiltWorker
class SyncWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted params: WorkerParameters,
private val repository: Repository
) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
repository.sync()
return Result.success()
}
}
Custom Hilt Components (Scopes)
å建èªå®ä¹ççå½å¨æèå´ï¼ä¾å¦ User Sessionã
å®ä¹ Custom Scope
@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class UserSessionScope
@DefineComponent(parent = SingletonComponent::class)
@UserSessionScope
interface UserSessionComponent
@DefineComponent.Builder
interface UserSessionComponentBuilder {
fun setUser(@BindsInstance user: User): UserSessionComponentBuilder
fun build(): UserSessionComponent
}
管ç Component çå½å¨æ
@Singleton
class UserSessionManager @Inject constructor(
private val componentBuilder: Provider<UserSessionComponentBuilder>
) {
private var component: UserSessionComponent? = null
fun login(user: User) {
component = componentBuilder.get().setUser(user).build()
}
fun logout() {
component = null
}
fun <T> getService(clazz: Class<T>): T {
return EntryPoints.get(component!!, UserSessionEntryPoint::class.java)
.let { /* get service */ }
}
}
Multi-binding (Set & Map)
å®ä½ Plugin æ¶æï¼ä¾å¦å¤ç§æ¯ä»æ¹å¼ã
Set Multibinding
interface PaymentProcessor {
fun process(amount: Double): Result
}
@Module
@InstallIn(SingletonComponent::class)
abstract class PaymentModule {
@Binds
@IntoSet
abstract fun bindCreditCard(impl: CreditCardProcessor): PaymentProcessor
@Binds
@IntoSet
abstract fun bindPayPal(impl: PayPalProcessor): PaymentProcessor
}
// 注å
¥ææå®ä½
class PaymentService @Inject constructor(
private val processors: Set<@JvmSuppressWildcards PaymentProcessor>
) {
fun processAll(amount: Double) {
processors.forEach { it.process(amount) }
}
}
Map Multibinding (with Key)
enum class PaymentType { CREDIT_CARD, PAYPAL, GOOGLE_PAY }
@MapKey
annotation class PaymentTypeKey(val value: PaymentType)
@Module
@InstallIn(SingletonComponent::class)
abstract class PaymentModule {
@Binds
@IntoMap
@PaymentTypeKey(PaymentType.CREDIT_CARD)
abstract fun bindCreditCard(impl: CreditCardProcessor): PaymentProcessor
}
// æ Key åå¾ç¹å®å®ä½
class PaymentService @Inject constructor(
private val processors: Map<PaymentType, @JvmSuppressWildcards PaymentProcessor>
) {
fun process(type: PaymentType, amount: Double) {
processors[type]?.process(amount)
}
}
Module Organization
å屿¶æ
di/
âââ AppModule.kt # Application-level
âââ NetworkModule.kt # Retrofit, OkHttp
âââ DatabaseModule.kt # Room
âââ RepositoryModule.kt # Repository bindings
âââ UseCaseModule.kt # UseCase bindings
Qualifier 使ç¨
@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class IoDispatcher
@Module
@InstallIn(SingletonComponent::class)
object DispatcherModule {
@Provides
@IoDispatcher
fun provideIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
}
Quick Checklist
- ViewModel å¨æåæ°ä½¿ç¨ Assisted Injection
- é¿å
å¨ Module ä¸ä½¿ç¨
@Providesåå»ºå¤æé»è¾ - Qualifier ç¨äºåºåç¸åç±»åçä¸åå®ä¾
- é¿å Circular Dependencies
- æµè¯æ¶ä½¿ç¨
@UninstallModules+@TestInstallIn