Switching injected implementations based on runtime parameters

56 views
Skip to first unread message

tew88

unread,
Sep 8, 2014, 4:51:37 PM9/8/14
to google...@googlegroups.com
Hi,

An approximation of my problem as follows:

Based upon a value that I will parse from an incoming request, I wish to inject one of two implementations of my interface.  Where is this logic best placed?

More specifically:

public interface NetworkCommunicator {
    public void sendCommandToNetwork(Command command, NetworkElement recipient);
}

--

public class OldNetworkCommunicatorImpl implements NetworkCommunicator {
    @Override
    public void sendCommandToNetwork(Command command, NetworkElement recipient) {
        ...
    }
}

--

public class NewNetworkCommunicatorImpl implements NetworkCommunicator {
    @Override
    public void sendCommandToNetwork(Command command, NetworkElement recipient) {
        ...
    }
}

--

public class NetworkCommandIssuer {

    private final NetworkCommunicator networkCommunicator;

    @Inject
    public NetworkCommandIssuer(final NetworkCommunicator networkCommunicator) {
        this.networkCommunicator = networkCommunicator;
    }
}

--

Based on some arbitrary mapping, I'd like to wire up my NetworkCommandIssuer to use my NewNetworkCommunicatorImpl implementation instead of my OldNetworkCommunicatorImpl implementation.  This will change from request to request.  So, where should I define this behaviour?

I'm new to Guice and DI in general, so please assume as little as possible in your responses!

Thanks in advance,
tew88

Martin Grajcar

unread,
Sep 8, 2014, 5:55:16 PM9/8/14
to google...@googlegroups.com
I had a similar. Just

bind(NetworkCommunicator.class)
    .toProvider(NetworkCommunicator.class);

Inject all needed information into your provider and let it take the decision. 

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/1122d17f-b029-4c04-be4c-2b6ba6485295%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Wadeson

unread,
Sep 8, 2014, 6:26:19 PM9/8/14
to google...@googlegroups.com

Hi Martin,

Thanks for the reply.

Could I trouble you for a minimal example of what that provider might look like?

Cheers,
tew88

You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/WtZjeFPp2b8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-guice...@googlegroups.com.

To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.

Martin Grajcar

unread,
Sep 8, 2014, 7:43:52 PM9/8/14
to google...@googlegroups.com
On Tue, Sep 9, 2014 at 12:26 AM, Tom Wadeson <tom...@gmail.com> wrote:

Hi Martin,

Thanks for the reply.

Could I trouble you for a minimal example of what that provider might look like?

A running example (needs Lombok, but that's trivial to resolve):

Regards,
Martin.

Tom Wadeson

unread,
Sep 9, 2014, 1:43:58 AM9/9/14
to google...@googlegroups.com

Thanks very much, Martin.  Just what I needed!  :)

--
You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/WtZjeFPp2b8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
Reply all
Reply to author
Forward
0 new messages