On Mon, Nov 19, 2012 at 3:11 PM, Blackrush a <[email protected]> wrote:
> Hello,
>
> I'm a newbie Java developer and I try to use Guice for my game's server. I
> use Netty 3.5 and Guice 3.0. My problem is that I've an interface
> NetworkHandler owned by an interface NetworkClient :
>
> interface NetworkClient {
> @NotNull NetworkSession getSession();
>
> @NotNull Set<NetworkHandler> getHandlers();
> }
>
> interface NetworkHandler {
> void handle(@NotNull NetworkMessage msg); // NetworkMessage is an
> abstract class
> }
>
> As you can see it, a NetworkClient has multiple NetworkHandler that should
> use dependency injection. For example :
>
> class AuthenticationHandler implements NetworkHandler {
> private final Repository<User> users;
> private final NetworkClient client;
>
> @Inject AuthenticationHandler(Repository<User> users, @Assisted
> NetworkClient client) {
> this.users = users;
> this.client = client;
> }
>
> @Override public void handle(@NotNull NetworkMessage message) {
> // [...]
> }
> }
>
> Since NetworkClient has multiple NetworkHandler, I'd like to use
> guice-multibindings and use guice-assistedinject to create NetworkHandlers
> depending on NetworkClient. Here is the expected factory interface :
>
This seems circular -- to be sure I understand, do you want the
Set<NetworkHandler> to be injected into the NetworkClient, when creation of
each NetworkHandler depends on that NetworkClient?
If so you have a few options:
(1) use a factory to create NetworkClient with setters:
class Factory {
@Inject NetworkClient client;
@Inject Set<NetworkHandler> handlers;
NetworkClient create() {
for (NetworkHandler handler : handlers) {
handler.setClient(client);
}
return client;
}
}
[but that's a bit unfortunate since your handlers have a non-final client]
(2) inject a Set<NetworkHandlerFactory> into your client and do:
@Inject NetworkClient(Set<NetworkHandlerFactory> factories) {
this.handlers = Sets.newHashSet();
for (NetworkHandlerFactory factory : factory) {
handlers.add(factory.create(this));
}
}
Be wary of any concurrency issues, but also, unfortunately be wary that you
might need some private module hoops:
http://code.google.com/p/google-guice/issues/detail?id=530
(3) break the circular dependency
farbeit from me to claim I know your architecture but a network client
seems like it sends messages; message handlers seem like they receive
messages. I'm not sure why a client would need handlers?
Fred
>
> interface NetworkHandlerFactory {
> Set<NetworkHandler> create(NetworkClient client);
> }
>
> How can I get this thing done ? Maybe is right under my nose and I haven't
> found it ...
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/mDVqcQBQsDYJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.