私は、ある程度モジュール化されたアーキテクチャでアプリを開発しようとしています (誰も知らないが、誰もが通信できる)。例から始めます。
内部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));
}
Authenticator
Dagger を介して多くのものに依存します。
@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
が、新しいパターンを模索しています。我慢してください。