For `kotlin-inject-anvil`, should I expect `Featu...
# kotlin-inject
k
For
kotlin-inject-anvil
, should I expect
FeatureFlagService
and
FeatureFlagSyncWorker
to be singletons here?
Copy code
@Origin(value = FeatureFlagServiceImpl::class)
@ContributesTo(scope = AppScope::class)
public interface FeatureFlagServiceImplComponent {
  @Provides
  @SingleIn(scope = AppScope::class)
  public fun provideFeatureFlagService(implementation: FeatureFlagServiceImpl): FeatureFlagService = implementation

  @Provides
  @SingleIn(scope = AppScope::class)
  public fun provideFeatureFlagSyncWorker(implementation: FeatureFlagServiceImpl): FeatureFlagSyncWorker = implementation
}
In final merged components a new instance is created on each injection -> uses getter property
Copy code
private val featureFlagServiceImpl: FeatureFlagServiceImpl
    get() = FeatureFlagServiceImpl(
      featureFlags = listFeatureFlagFeatureFlagValue,
Copy code
featureFlagSyncWorker = _scoped.get("build.wallet.feature.FeatureFlagSyncWorker") {
  provideFeatureFlagSyncWorker(
    implementation = featureFlagServiceImpl
  )
},
I have a custom annotation that generates that component ^
Copy code
@Inject
@ContributesSingleton(AppScope::class)
class FeatureFlagServiceImpl(
  private val featureFlags: List<FeatureFlag<out FeatureFlagValue>>,
  private val featureFlagSyncer: FeatureFlagSyncer,
) : FeatureFlagService, FeatureFlagSyncWorker
I suspect it's because
FeatureFlagServiceImpl
itself is not annotated with
@SingleIn
?
I think I understand now...
any way to make this work without adding scope to implementation class?
r
I believe you can generate a provider method for
FeatureFlagServiceImpl
. It should take precedence over the inject constructor and not result in a duplicate binding. At least this was the case with Dagger. In our custom annotation we avoid adding
@Inject
to these class altogether, e.g. we have:
Copy code
@ContributesRobot(AppScope::class)
class MyRobot : Robot
Notice that there’s no
@Inject
and we generate the provider method instead with the right scope.
🤔 1
k
So the whole provider?
Copy code
@Provides
fun provideFeatureFlagServiceImpl(
  a: ..., 
  b: ...
): FeatureFlagServiceImpl = FeatureFlagServiceImpl(a, b)
this works 👌
Copy code
@Origin(value = FeatureFlagServiceImpl::class)
@ContributesTo(scope = AppScope::class)
public interface FeatureFlagServiceImplComponent {
  @Provides
  @SingleIn(scope = AppScope::class)
  public fun provideFeatureFlagServiceImpl(featureFlags: List<FeatureFlag<out FeatureFlagValue>>, featureFlagSyncer: FeatureFlagSyncer): FeatureFlagServiceImpl = FeatureFlagServiceImpl(featureFlags, featureFlagSyncer)

  @Provides
  public fun provideFeatureFlagService(implementation: FeatureFlagServiceImpl): FeatureFlagService = implementation

  @Provides
  public fun provideFeatureFlagSyncWorker(implementation: FeatureFlagServiceImpl): FeatureFlagSyncWorker = implementation
}
which makes sense