- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序的数据层中使用了以下模块,它是一个普通的 Android 库。
@Module
interface MapperModule {
@Binds
fun bindDomainToDataMapper(domainToDataMapperImp: DomainToDataMapperImp)
: DomainToDataMapper<TodoTaskEntity, ToDoTaskModel>
@Binds
fun bindDataToDomainMapper(dataToDomainMapperImp: DataToDomainMapperImp)
: DataToDomainMapper<ToDoTaskModel, TodoTaskEntity>
}
我只是想知道 @InstallIn 范围应该是什么,因为这是数据层,所以不特定于任何 android 组件。
我正在考虑使用 @InstallIn(SingleComponent::class)
但我不希望这些类是单例的。
有什么想法吗?
最佳答案
Hilt 具有为您管理的适用于 Android 的预定义组件。但是,在某些情况下,标准 Hilt 组件可能与对象生命周期或特定功能的需求不匹配
Custom component limitations
Custom component definitions currently have some limitations:
Components must be a direct or indirect child of theSingletonComponent. Components may not be inserted between any of thestandard components. For example, a component cannot be added betweenthe ActivityComponent and the FragmentComponent.
要创建自定义 Hilt 组件,请创建一个用 @DefineComponent 注释的类。这将是@InstallIn 注释中使用的类。
您的组件的父级应该在 @DefineComponent 注释 的值中定义。您的 @DefineComponent 类也可以使用范围注释进行注释,以允许将对象范围限定到该组件。
@DefineComponent(parent = SingletonComponent::class)
interface MyCustomComponent
还必须定义构建器接口(interface)。如果缺少此构建器,则不会生成组件,因为将无法构建组件。该接口(interface)可从父组件注入(inject),并将成为创建组件新实例的接口(interface)。由于这些是自定义组件,一旦构建了实例,您的工作就是在适当的时候保留或释放组件实例。
Builder 接口(interface)是通过使用 @DefineComponent.Builder 标记接口(interface)来定义的。构建器必须有一个返回 @DefineComponent 类型的方法。它们还可能具有普通 Dagger 组件构建器可能具有的其他方法(如 @BindsInstance 方法)。
@DefineComponent.Builder
interface MyCustomComponentBuilder {
fun fooSeedData(@BindsInstance foo: Foo): MyCustomComponentBuilder
fun build(): MyCustomComponent
}
虽然 @DefineComponent.Builder 类可以嵌套在 @DefineComponent 中,但作为单独的类通常更好。只要它是 @HiltAndroidApp 应用程序或 @HiltAndroidTest 测试的传递依赖项,它就可以分为不同的类。由于 @DefineComponent 类在很多地方都是通过 @InstallIn 引用的,因此最好将构建器分开,这样构建器中的依赖项就不会成为组件中安装的每个模块的传递依赖项。
出于避免过度依赖的同样原因,@DefineComponent 接口(interface)上不允许使用方法。相反,应该通过入口点访问 Dagger 对象。
@入口点@InstallIn(MyCustomComponent::类)接口(interface) MyCustomEntryPoint {有趣的 getBar(): 酒吧
class CustomComponentManager @Inject constructor(
componentBuilder: MyCustomComponentBuilder) {
fun doSomething(foo: Foo) {
val component = componentBuilder.fooSeedData(foo).build();
val bar = EntryPoints.get(component, MyCustomEntryPoint::class.java).getBar()
// Don't forget to hold on to the component instance if you need to!
}
结论:即使您间接创建自定义组件,它看起来也像单例。
关于android - 用于非 Android 范围的 Dagger 刀柄预定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72102130/
我是一名优秀的程序员,十分优秀!