I am trying to develop an app with a somewhat modular architecture (nobody knows about no one, but everybody still can communicate). I will begin with an example:
Inside the 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
}
The Authenticator
catches this event and then posts back:
@Subscribe
public void onAuthorizeEvent(AuthorizeEvent event) {
// ... login
bus.post(new AuthEvent(user));
}
Authenticator
depends on a lot of stuff through 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);
}
which is provided by the 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);
}
}
My Application
class:
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);
}
}
The question is - where do I instantiate (@Inject
) the Authenticator
? I don't use it directly in any of my classes. Can it be a field in the AbsApplication
class? Should I even use Dagger for the Authenticator
? Am I not using Dagger
's modules properly?
I know that I can @Inject Authenticator
inside the LoginFragment
, but I am exploring new patterns. Bear with me, please.