开发者

Adding user class to all presenters

开发者 https://www.devze.com 2023-01-05 06:57 出处:网络
I have a hazy understanding of GIN, but have it working for injecting presenters, etc. I\'m trying to inject a self-made \"User\" class into all my presenters in order to get the currently logged-in

I have a hazy understanding of GIN, but have it working for injecting presenters, etc.

I'm trying to inject a self-made "User" class into all my presenters in order to get the currently logged-in user.

I've added @Inject to the constructor on my User class, and added User to my GIN module ... but apart from that, I'm totally lost. Do I bind it to my app presenter (tried that, but I get an error since User doesn't extend my AppPresenter)? As a singleton? Is this even the right way to get pass this data around?

I hate to post this here (not looking for free homework), but I can't find a decent tutorial/example on this anywhere else.

Much tha开发者_JS百科nks in advance.


I see two possibilities:

  • inject with the source being a @Provides method or a Provider. However, the problem is how to pass the currently logged in user to those methods, since global state is a no-no. A possible solution would be passing it via the event bus, but at this point it seems that it's kinda defeats the whole point of DI (injecting the currently logged in user is IMHO not the role of DI).
  • pass around the user via the event bus. Create a custom event that gets fired when the user logs in/changes, and some presenters listen for that event (after all, not all presenters have to know the current user).

I prefer and use the second approach - while DI is awesome, using it in the way you described seems kinda hackish to me. I strongly recommend checking out the Guice documentation - Gin is basically Guice wrapped nicely to work with GWT, so the core is the same. For some other pointers how to effectively use DI and, in general, write testable code, I strongly recommend Misko Hevery's blog and the guide to writing testable code he put up there (used internally by Google).

0

精彩评论

暂无评论...
验证码 换一张
取 消