Dagger 2在Application类中构建组件的良好实践

问题描述 投票:2回答:1

我知道这个问题可能没有明确的答案。

但是我想知道您的意见,也许还有新的想法。

我想知道以下哪个选项是在Application类中构建应用程序级Dagger组件的最佳/正确/正确方法。


示例1:

public class MyApp extends Application {

    private NetComponent mNetComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        mNetComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this)) 
                .netModule(new NetModule("https://api.github.com"))
                .build();
    }

    public NetComponent getNetComponent() {
        return mNetComponent;
    }
}

用法:

((MyApp) getApplication()).getNetComponent().inject(this);

示例2:

 class MyApplication extends Application {

    private static MyComponent component;

    @Override
    void onCreate() {
        component = DaggerMyComponent.builder()
                .contextModule(new ContextModule(getApplicationContext()))
                .build();
    }

    public static MyComponent getMyComponent() {
        return component;
    }
}

用法:

MyApplication.getMyComponent().inject(this)

示例3:

class CustomApplication: Application() {
    lateinit var component: SingletonComponent
        private set

    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
        component = DaggerSingletonComponent.builder()
                       .contextModule(ContextModule(this))
                       .build()
    }

    companion object {
       private var INSTANCE: CustomApplication? = null

       @JvmStatic
       fun get(): CustomApplication = INSTANCE!!

    }
}

然后:

class Injector private constructor() {
    companion object {
        @JvmStatic
        fun get() : SingletonComponent = CustomApplication.get().component
    }
}

用法:

Injector.get().catRepository()

示例4:

 class App : Application() {

    var repositoryComponent: RepositoryComponent? = null
    var appComponent: AppComponent? = null

    override fun onCreate() {
        super.onCreate()
        instance = this
        appComponent = DaggerAppComponent.builder().application(this).build()
        repositoryComponent = DaggerRepositoryComponent.builder().build()
    }

    companion object {

        private var instance: App? = null

        fun get(): App {
            return instance!!
        }
    }
}

用法:

 App.get().repositoryComponent!!.inject(this)

您对此有何看法?有没有更好/更清洁的方法可以做到这一点?也许提供的例子很好吗?也许只是其中之一?

我将感谢您提供任何好的示例/技巧/建议。

谢谢!

java android dependency-injection kotlin dagger-2
1个回答
2
投票

好吧,尽管我有偏见,但5天内没人回答,轮到我了:p

  • 选项#1

((MyApp) getApplication()).getNetComponent().inject(this);

这是做事的“好”版本,除了两件事。

首先是名字。 NetComponent并非真正用于联网,它是应用程序全局单例组件,因此应命名为SingletonComponentAppComponent。但是将其命名为NetComponent是不明确的,它通常也负责everything other

第二个问题是,您需要引用Context来访问依赖关系图,从而使Context实际上是dependency,而不是被提供给您。

  • 选项#2

MyApplication.getMyComponent().inject(this)

这是一种很好的处理方式,但是您需要知道要访问对象图,需要访问MyApplication的静态方法。

  • 选项#3

Injector.get().inject(this)

内部,此解决方案实际上只是调用以获取应用程序组件public static AppComponent get() { return MyApplication.getInstance().getComponent(); }

[好处是getComponent()是通过Application的实例方法公开的,因此从理论上讲它可以被换出。

而且,显然,与应用程序类相比,调用名为Injector.get()的方法更像是“注入器”。

关于您使用.catRepository()还是.inject(this),这取决于您;但是我个人更喜欢调用提供方法来获得“活动/片段”中的详细信息,因为随着时间的推移列出成员注入目标会给组件增加很多混乱。

4。)

App.get().repositoryComponent!!.inject(this)

如果!!repositoryComponent,则可以放弃lateinit var

具有相同作用域的两个组件(因此有两个不同的对象图)只会在所有选项中引起麻烦,这是最糟糕的。


我认为,第三个选项是最好的。从技术上讲,它与选项#2相同,只是通过实际返回组件的Application实例方法进行了附加的“间接”。

© www.soinside.com 2019 - 2024. All rights reserved.