diff --git a/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilder.kt b/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilder.kt index c9f656c..f90eaa7 100644 --- a/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilder.kt +++ b/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilder.kt @@ -71,12 +71,8 @@ public class DIBuilder(dependencies: Dependencies) { name: String? = null, crossinline factory: DI.() -> T ) { - val key = DependencyKey( - type = typeOf(), - name = name - ) val provider = DependencyProvider.Singleton { di -> di.factory() } - register(key, provider) + register(name, provider) } public inline fun singleton( @@ -89,6 +85,24 @@ public class DIBuilder(dependencies: Dependencies) { ) } + public inline fun provider( + name: String? = null, + crossinline factory: DI.() -> T + ) { + val provider = DependencyProvider { di -> di.factory() } + register(name, provider) + } + + public inline fun provider( + noinline factory: DI.() -> T + ): DIBuilderProviderDelegate { + return DIBuilderProviderDelegate( + di = this, + type = typeOf(), + factory = factory + ) + } + public fun build(): DI = DI( dependencies = Dependencies(dependencies.toList()) ) diff --git a/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilderDelegates.kt b/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilderDelegates.kt index 22cc1d7..8818dc1 100644 --- a/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilderDelegates.kt +++ b/core/src/commonMain/kotlin/app/meetacy/di/builder/DIBuilderDelegates.kt @@ -17,10 +17,7 @@ public class DIBuilderConstantDelegate( property: KProperty<*> ): ReadOnlyProperty { di.register( - key = DependencyKey( - type = type, - name = property.name - ), + key = DependencyKey(type, property.name), provider = { value } ) return ReadOnlyProperty { _, _ -> } @@ -37,12 +34,26 @@ public class DIBuilderSingletonDelegate( property: KProperty<*> ): ReadOnlyProperty { di.register( - key = DependencyKey( - type = type, - name = property.name - ), + key = DependencyKey(type, property.name), provider = DependencyProvider.Singleton { di -> di.factory() } ) return ReadOnlyProperty { _, _ -> } } } + +public class DIBuilderProviderDelegate( + private val di: DIBuilder, + private val type: KType, + private val factory: DI.() -> T +) { + public operator fun provideDelegate( + thisRef: Any?, + property: KProperty<*> + ): ReadOnlyProperty { + di.register( + key = DependencyKey(type, property.name), + provider = { di -> di.factory() } + ) + return ReadOnlyProperty { _, _ -> } + } +}