bfreuden
bfreuden

Reputation: 95

How to achieve scope singleton with Toothpick annotations?

I have a non-Android app having many similar shards objects, and I want all objets inside each shard (DB client, DAOs...) to be singletons.

For this purpose, I have created a ShardSingleton annotation:

@Scope
@Retention(AnnotationRetention.RUNTIME)
annotation class ShardSingleton

and I am creating each shard object inside its own scope:

    var shard1: Shard = KTP.openScopes("app", "shard1")
        .supportScopeAnnotation(ShardSingleton::class.java)
        .getInstance(Shard::class.java)

For my DAO to actually be a singleton inside its shard, I have to annotate it with both @ShardSingleton and @Singleton:

@Singleton // the FooDAO is not a singleton without this annotation
@ShardSingleton
@InjectConstructor
class FooDAO(val dbClient: DBClient)

At first sight (and probably out of ignorance), I thought @ShardSingleton would have been enough.

Is it expected?

Here is a gist demonstrating the behavior: https://gist.github.com/bfreuden/a866b21c5a6342a3ce1ed26aa636f9f6

Upvotes: 1

Views: 202

Answers (0)

Related Questions