2

私は、ある程度モジュール化されたアーキテクチャでアプリを開発しようとしています (誰も知らないが、誰もが通信できる)。例から始めます。

内部LoginFragment:

@OnClick
void login() {
    bus.post(new AuthorizeEvent(email, password)); // bus is a Bus from Otto
}

@Subscribe
public void onAuthorizedEvent(AuthEvent event) {
    // ... user was authorized
}

Authenticatorこのイベントをキャッチし、ポスト バックします。

@Subscribe
public void onAuthorizeEvent(AuthorizeEvent event) {
    // ... login
    bus.post(new AuthEvent(user));
}

AuthenticatorDagger を介して多くのものに依存します。

@Inject
public Authenticator(ApiService apiService, Context context, Bus uiBus, Scheduler ioScheduler,
                     Scheduler uiScheduler) {
    this.apiService = apiService;
    this.context = context;
    this.uiBus = uiBus;
    this.ioScheduler = ioScheduler;
    this.uiScheduler = uiScheduler;

    uiBus.register(this);
}

によって提供されますAuthModule

@Module(
    complete = false,
    library = true,
    includes = {
            ApiModule.class,
            ApplicationModule.class
    }
)
public class AuthModule {
    @Provides
    @Singleton
    Authenticator provideAuthenticator(ApiService apiService, @ForApplication Context context,
                                       @UIBus Bus uiBus, @IOScheduler Scheduler ioScheduler,
                                       @UIScheduler Scheduler uiScheduler) {
        return new Authenticator(apiService, context, uiBus, ioScheduler, uiScheduler);
    }
}

Applicationのクラス:

public class AbsApplication extends Application {
    private ObjectGraph graph;

    @Override
    public void onCreate() {
        super.onCreate();
        graph = ObjectGraph.create(getModules());
    }

    public Object[] getModules() {
        return new Object[]{
                new ApplicationModule(this),
                new ApiModule(),
                new AuthModule()
        };
    }

    public void inject(Object object) {
        graph.inject(object);
    }
}

問題は、どこでインスタンス化 ( @Inject) するAuthenticatorかです。どのクラスでも直接使用しません。AbsApplicationクラスのフィールドにすることはできますか?にダガーを使用する必要がありAuthenticatorますか?Daggerのモジュールを正しく使用していませんか?


@Inject Authenticatorの中に入れることができることはわかっていますLoginFragmentが、新しいパターンを模索しています。我慢してください。

4

0 に答える 0