You could move the call to the ConnectionFactory to the
HttpClientImpl: class WebCrawler { private final HttpClientFactory httpClientFactory;
@Inject public WebCrawler(HttpClientFactory httpFactory) {...}
public crawlDataFrom(Uri uri) {
HttpClient
client = httpClientFactory.createClient(uri);
client.doMagicStuff();
}
}
interface HttpClientFactory { HttpClient createClient(Uri uri); } class HttpClientImpl implements HttpClient { private final Connection conn; @AssistedInject public HttpClientImpl(@Assisted Uri uri, ConnectionFactory connectionFactory) { conn = connectionFactory.create(uri.getHost(), uri.getPort()); } } Or like this: class HttpClientImpl implements HttpClient { private final Connection conn; @AssistedInject public HttpClientImpl(@Assisted Uri uri, ConnectionFactory connectionFactory) { this(connectionFactory.create(uri.getHost(), uri.getPort())); } public HttpClientImpl(Connection conn) { this.conn = conn; } } And in you module you would bind the assisted inject as follows: install(new FactoryModuleBuilder() .implement(HttpClient.class, HttpClientImpl.class) .build(HttpClientFactory.class)); Hope this helps. On 02/22/2013 04:14 PM, David Hoffer
wrote:
-- 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+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. Visit this group at http://groups.google.com/group/google-guice?hl=en. For more options, visit https://groups.google.com/groups/opt_out. |
- Problems implementing Guice / DI the proper way - I end ... Bram
- Re: Problems implementing Guice / DI the proper way... Cédric Beust ♔
- Re: Problems implementing Guice / DI the proper... Christian Gruber
- Re: Problems implementing Guice / DI the proper way... Bram
- Re: Problems implementing Guice / DI the proper... David Hoffer
- Re: Problems implementing Guice / DI the pr... Bram
- Re: Problems implementing Guice / DI th... David Hoffer
- Re: Problems implementing Guice / ... Stephan Classen
- Re: Problems implementing Guice / DI the proper way... Bram