简体   繁体   English

为什么我仍然需要使用带有依赖注入库 (Hilt) 的 singleton 类?

[英]Why do I still need to use singleton classes with a dependency injection library (Hilt)?

One of the reasons the official android documentation recommends using a dependency injection library such as Hilt instead of manual dependency injection is it's difficult to reuse objects.官方 android 文档建议使用诸如Hilt之类的依赖注入库而不是手动依赖注入的原因之一是难以重用对象。 And I'd have to apply the singleton pattern, which makes testing more difficult because all tests share the same singleton instance.而且我必须应用 singleton 模式,这使得测试更加困难,因为所有测试共享相同的 singleton 实例。 So Hilt should replace singleton classes.所以 Hilt 应该替换 singleton 类。

But I was checking the sunflower sample and I noticed it still uses the singleton pattern with Hilt.但是我在检查向日葵样品时发现它仍然使用带有 Hilt 的 singleton 图案。

The App database is declared as a singleton: App 数据库声明为 singleton:

// For Singleton instantiation
@Volatile private var instance: AppDatabase? = null

fun getInstance(context: Context): AppDatabase {
    return instance ?: synchronized(this) {
        instance ?: AppDatabase.buildDatabase(context).also { instance = it }
    }
}

DatabaseModule is also scoped to the application container: DatabaseModule也适用于应用程序容器:

@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {

    @Singleton
    @Provides
    fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase {
        return AppDatabase.getInstance(context)
    }

    //...
}

Using the singleton pattern here doesn't make sense to me because by annotating @Provides provideDatabase() method with @Singleton .在这里使用 singleton 模式对我来说没有意义,因为通过使用@Singleton注释@Provides provideDatabase()方法。 Hilt makes sure only one instance is used across the app lifetime. Hilt确保在整个应用生命周期中只使用一个实例。

Shouldn't Hilt replace the singleton pattern as well? Hilt不应该也替换 singleton 模式吗?

One of the reasons the official android documentation recommends using a dependency injection library such as Hilt instead of manual dependency injection is it's difficult to reuse objects.官方 android 文档建议使用诸如Hilt之类的依赖注入库而不是手动依赖注入的原因之一是难以重用对象。 And I'd have to apply the singleton pattern, which makes testing more difficult because all tests share the same singleton instance.而且我必须应用 singleton 模式,这使得测试更加困难,因为所有测试共享相同的 singleton 实例。 So Hilt should replace singleton classes.所以 Hilt 应该替换 singleton 类。

But I was checking the sunflower sample and I noticed it still uses the singleton pattern with Hilt.但是我在检查向日葵样品时发现它仍然使用带有 Hilt 的 singleton 图案。

The App database is declared as a singleton: App 数据库声明为 singleton:

// For Singleton instantiation
@Volatile private var instance: AppDatabase? = null

fun getInstance(context: Context): AppDatabase {
    return instance ?: synchronized(this) {
        instance ?: AppDatabase.buildDatabase(context).also { instance = it }
    }
}

DatabaseModule is also scoped to the application container: DatabaseModule也适用于应用程序容器:

@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {

    @Singleton
    @Provides
    fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase {
        return AppDatabase.getInstance(context)
    }

    //...
}

Using the singleton pattern here doesn't make sense to me because by annotating @Provides provideDatabase() method with @Singleton .在这里使用 singleton 模式对我来说没有意义,因为通过使用@Singleton注释@Provides provideDatabase()方法。 Hilt makes sure only one instance is used across the app lifetime. Hilt确保在整个应用生命周期中只使用一个实例。

Shouldn't Hilt replace the singleton pattern as well? Hilt不应该也替换 singleton 模式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM