Author: limpbizkit
Date: Mon Mar 2 21:06:03 2009
New Revision: 893
Added:
wiki/ProviderBindings.wiki
Log:
Created wiki page through web user interface.
Added: wiki/ProviderBindings.wiki
==============================================================================
--- (empty file)
+++ wiki/ProviderBindings.wiki Mon Mar 2 21:06:03 2009
@@ -0,0 +1,34 @@
+=Provider Bindings=
+When your `...@provides` methods start to grow complex, you may consider
moving them to a class of their own. The provider class implements Guice's
`Provider` interface, which is a simple, general interface for supplying
values:
+{{{
+public interface Provider<T> {
+ T get();
+}
+}}}
+Our provider implementation class has dependencies of its own, which it
receives via its `...@inject`-annotated constructor. It implements the
`Provider` interface to define what's returned with complete type safety:
+{{{
+public class DatabaseTransactionLogProvider implements
Provider<TransactionLog> {
+ private final Connection connection;
+
+ @Inject
+ public DatabaseTransactionLogProvider(Connection connection) {
+ this.connection = connection;
+ }
+
+ public TransactionLog get() {
+ DatabaseTransactionLog transactionLog = new DatabaseTransactionLog();
+ transactionLog.setConnection(connection);
+ return transactionLog;
+ }
+}
+}}}
+Finally we bind to the provider using the `.toProvider` clause:
+{{{
+public class BillingModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ bind(TransactionLog.class)
+ .toProvider(DatabaseTransactionLogProvider.class);
+ }
+}}}
+If your providers are complex, be sure to test them!
\ No newline at end of file
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice-dev" 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-dev?hl=en
-~----------~----~----~----~------~----~------~--~---