匕首2:在定义子组件后注入 [英] Dagger 2: inject after define subcomponent

查看:56
本文介绍了匕首2:在定义子组件后注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循此指南为我的Android应用程序制作子组件.在这里,我定义了一个用于LoginActivity的名为 LoginComponent 的子组件:

I follow this guide for making sub components for my Android application. Here I defined one sub component named LoginComponent used for LoginActivity:

@Subcomponent(modules = LoginModule.class)
public interface LoginComponent {

    void inject(LoginActivity activity);

    @Subcomponent.Builder
    interface Builder {
        Builder requestModule(LoginModule module);
        LoginComponent build();
    }
}


@Module
public class LoginModule {
    @Provides
    LoginManager provideLoginManager(LoginManagerImpl manager) {
        return manager;
    }

    @Provides
    LoginView provideLoginView(LoginViewImpl view) {
        return view;
    }

    @Provides
    LoginPresenter loginPresenter(LoginView view, LoginManager manager) {
        return new LoginPresenterImpl(view, manager);
    }
}

我在另一个组件中定义了这个子组件:

I define this sub component inside another component:

@Module(subcomponents = LoginComponent.class)
public interface AppModule {
}

@Singleton
@Component(modules = {
        AppModule.class
})
public interface AppComponent {
}

这是我的LoginActivity:

Here is my LoginActivity:

public class LoginActivity extends AppCompatActivity  {

    @Inject LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }
}

我的问题是:

  1. @ Subcomponent.Builder 的目的.我不明白这一点,因为此组件中的所有模块都已在 @Subcomponent 批注中定义.为什么我们要再次复制.
  2. 如何在上述活动中注入LoginPresenter.
  1. purpose of @Subcomponent.Builder. I don't understand this point, because all modules in this component has been defined on @Subcomponent annotation. Why should we duplicate again.
  2. how can I injected LoginPresenter on above activity.

谢谢

推荐答案

  1. @ Subcomponent.Builder 遵循 @ Component.Builder 中记录的相同规则.:您需要为Dagger无法初始化的每个模块都有一个设置器.(允许模块具有构造函数参数或静态工厂方法,这将阻止Dagger创建其自己的实例.)

  1. @Subcomponent.Builder follows the same rules documented in @Component.Builder: You need to have a setter for every Module that Dagger can't initialize itself. (Modules are allowed to have constructor parameters or static factory methods, which would prevent Dagger from creating its own instance.)

由于Dagger可以通过调用零参数构造函数来创建LoginModule,因此可以从Builder中删除该方法;您不需要重新创建它.您可能还考虑使LoginModule的方法 static ,这将使您可以将LoginModule设置为接口或抽象类,然后Dagger完全可以避免持有对Module的引用.最后,对于绑定两个类的简单 @Provides 方法(例如, @Provides B ProvideB(A a){return a;} ),您可以切换到 @Binds ,这样可以使生成的代码更快.

Because Dagger can create a LoginModule by calling a zero-arg constructor, you can drop that method from the Builder; you don't need to recreate it. You might also consider making LoginModule's methods static, which would allow you to make LoginModule an interface or abstract class—then Dagger would be able to avoid holding a reference to the Module at all. Finally, for the simple @Provides methods that bind two classes (e.g. @Provides B provideB(A a) { return a; }) you can switch to @Binds, which would allow the generated code to be even faster.

此时,您的 @ Subcomponent.Builder 成为特定于子组件的 Provider :您可以注入 Builder 提供程序< Builder> ,并通过在其上调用 build()来获取新的子组件实例.只要不需要提供任何Module实例,就不需要在构建器上使用任何其他方法.

At that point, your @Subcomponent.Builder becomes a subcomponent-specific Provider: You can inject your Builder or a Provider<Builder> and get a new subcomponent instance by calling build() on it. As long as you don't need to provide any Module instances, you don't need to have any other methods on your builder.

您也许可以简单地注入 Provider< LoginComponent> ,但是我在实践中没有看到它,也没有亲自尝试过.

You may be able to simply inject a Provider<LoginComponent>, but I haven't seen that in practice, and haven't tried it myself.

要注入LoginPresenter,您需要进入您的AppComponent,创建一个新的LoginComponent,然后使用它通过传递它来注入您的Activity.但是,这可能很困难,因为您没有给自己任何注入您的LoginComponent.Builder的访问权限.

To inject a LoginPresenter, you need to get to your AppComponent, create a new LoginComponent, and use it to inject your Activity by passing it in. However, this may be difficult, because you haven't given yourself any access to inject your LoginComponent.Builder.

除非您想尝试使用 dagger.android ,否则,您可以添加一个调用AppComponent来创建新的LoginComponent的方法.

Unless you want to try with dagger.android instead, you can add a method to call on your AppComponent to create a new LoginComponent.

// This way...
LoginComponent createLoginComponent();

// or this way:
LoginComponent.Builder createLoginComponentBuilder();

有时,您会看到第一种名为 plus()的方式,这是Dagger 1中建立的命名约定.第一种方法还可以避免创建@ Subcomponent.Builder或在 AppModule 中添加子组件,尽管您可能希望按原样保留该结构,以便以后可以向AppModule中添加更多内容.

Sometimes you'll see the first way named plus(), which is a naming convention established in Dagger 1; that first way also lets you avoid creating a @Subcomponent.Builder or adding the subcomponent in your AppModule, though you might want to leave the structure the way you have it so you can add more to your AppModule later.

要结束此操作,请在 Activity.onCreate 中,让您的Activity获取应用程序,访问其AppComponent并进行自我注入.

To finish it off, in Activity.onCreate, you can have your Activity get your Application, access its AppComponent, and inject itself.

((YourApplication) getContext().getApplicationContext())
    .getApp()
    .createLoginComponent()
    .inject(this);

这篇关于匕首2:在定义子组件后注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆