Author: limpbizkit
Date: Sun Nov 16 16:32:51 2008
New Revision: 685
Added:
wiki/AssistedInject.wiki
Log:
Created wiki page through web user interface.
Added: wiki/AssistedInject.wiki
==============================================================================
--- (empty file)
+++ wiki/AssistedInject.wiki Sun Nov 16 16:32:51 2008
@@ -0,0 +1,69 @@
+#summary an easier way to help the Guice Injector build objects
+=!AssistedInject=
+
+==Factories by Hand==
+Sometimes a class gets some of its constructor parameters from the Guice
Injector and others from the caller:
+{{{
+public class RealPayment implements Payment {
+ public RealPayment(
+ CreditService creditService, // from the Injector
+ AuthService authService, // from the Injector
+ Date startDate, // from the instance's creator
+ Money amount); // from the instance's creator
+ }
+ ...
+}
+}}}
+The standard solution to this problem is to write a factory that helps
Guice build the objects:
+{{{
+public interface PaymentFactory {
+ public Payment create(Date startDate, Money amount);
+}
+}}}
+{{{
+public class RealPaymentFactory implements PaymentFactory {
+ private final Provider<CreditService> creditServiceProvider;
+ private final Provider<AuthService> authServiceProvider;
+ public PaymentFactory(Provider<CreditService> creditServiceProvider,
+ Provider<AuthService> authServiceProvider) {
+ this.creditServiceProvider = creditServiceProvider;
+ this.authServiceProvider = authServiceProvider;
+ }
+ public Payment create(Date startDate, Money amount) {
+ return new RealPayment(creditServiceProvider.get(),
+ authServiceProvider.get(), startDate, amount);
+ }
+}
+}}}
+...and a corresponding binding in the module:
+{{{
+ bind(PaymentFactory.class).to(RealPaymentFactory.class);
+}}}
+It's annoying to write the boilerplate factory class each time this
situation arrises. It's also annoying to update the factories when the
implementation class' dependencies change.
+
+
+==Factories by !AssistedInject==
+!AssistedInject generates an implementation of the factory class
automatically. To use it, annotate the implementation class' constructor
and the fields that aren't known by the injector:
+{{{
+public class RealPayment implements Payment {
+ @AssistedInject
+ public RealPayment(
+ CreditService creditService,
+ AuthService authService,
+ @Assisted Date startDate,
+ @Assisted Money amount);
+ }
+ ...
+}
+}}}
+Then bind a `Provider<Factory>` in the Guice module:
+{{{
+bind(PaymentFactory.class).toProvider(
+ FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));
+}}}
+The `FactoryProvider` maps the `create()` method's parameters to the
corresponding [EMAIL PROTECTED] parameters in the implementation class'
constructor. For the other constructor arguments, it asks the regular
Injector to provide values.
+
+With `FactoryProvider`, it's easier to create classes that need extra
arguments at construction time:
+ # Annotate the constructor and assisted parameters on the implementation
class (such as `RealPayment`)
+ # Create a factory interface with a `create()` method that takes only
the assisted parameters. Make sure they're in the same order as in the
constructor
+ # Bind that factory to a provider created by `FactoryProvider`.
\ 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
-~----------~----~----~----~------~----~------~--~---