How to inject a "runtime" dependency like a logged in user which is not available at application boot time?
- by Fabian
I'm just not getting this:
I use Gin in my java GWT app to do DI. The login screen is integrated into the full application window. After the user has logged in I want to inject the user object into other classes like GUI Presenters which I create, so I have some sort of runtime dependency I believe. How do i do that?
One solution I can think of is sth like:
class Presenter {
@Inject
Presenter(LoggedInUserFactory userFactory) {
User user = userFactory.getLoggedInUser();
}
}
class LoggedInUserFactoryImpl {
public static User user;
User getLoggedInUser() {
return user;
}
}
So, when the user is successfully logged in and I have the object i set the static property in LoggedInUserFactory, but this will only work if the Presenter is created after the user has logged in which is not the case.
Or should I use a global static registry? I just don't like the idea of having static dependencies in my classes.
Any input is greatly appreciated.