我正在尝试开发一个具有某种模块化架构的应用程序(没有人知道没有人,但每个人仍然可以交流)。我将从一个例子开始:
里面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
正确使用 's 模块吗?
我知道我可以@Inject Authenticator
在里面LoginFragment
,但我正在探索新的模式。请忍受我。