Author: limpbizkit
Date: Tue Feb 24 23:30:38 2009
New Revision: 865

Added:
    wiki/GettingStarted.wiki

Log:
Created wiki page through web user interface.

Added: wiki/GettingStarted.wiki
==============================================================================
--- (empty file)
+++ wiki/GettingStarted.wiki    Tue Feb 24 23:30:38 2009
@@ -0,0 +1,62 @@
+#summary How to start doing dependency injection with Guice.
+=Getting Started=
+With dependency injection, objects accept dependencies in their  
constructors. To build an object, you first build it's dependencies. To  
build the dependencies, you need to _their_ dependencies, and so on. So  
when you build an object, you really need to build an *object graph*.
+
+Building object graphs by hand is labour intensive, error prone, and makes  
testing difficult. Instead, Guice can build the object graph for you. But  
first, Guice needs to be configured to build the graph exactly as you want  
it.
+
+To illustrate, we'll start the `RealBillingService` class that accepts its  
dependent interfaces `CreditCardProcessor` and `TransactionLog` in its  
constructor. To make it explicit that the `RealBillingService` constructor  
is invoked by Guice, we add the `...@inject` annotation:
+{{{
+public class RealBillingService implements BillingService {
+  private final CreditCardProcessor processor;
+  private final TransactionLog transactionLog;
+
+  @Inject
+  public RealBillingService(CreditCardProcessor processor,
+      TransactionLog transactionLog) {
+    this.processor = processor;
+    this.transactionLog = transactionLog;
+  }
+
+  public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
+    ...
+  }
+}
+}}}
+We want to build a `RealBillingService` using `PaypalCreditCardProcessor`  
and `DatabaseTransactionLog`. Guice uses *bindings* to map types to their  
implementations. A *module* is a collection of bindings specified using  
fluent, English-like method calls:
+{{{
+public class BillingModule extends AbstractModule {
+  @Override
+  protected void configure() {
+
+     /*
+      * This tells Guice that whenever it sees a dependency on a  
TransactionLog,
+      * it should satisfy the dependency using a DatabaseTransactionLog.
+      */
+    bind(TransactionLog.class).to(DatabaseTransactionLog.class);
+
+     /*
+      * Similarly, this binding tells Guice that when CreditCardProcessor  
is used in
+      * a dependency, that should be satisfied with a  
PaypalCreditCardProcessor.
+      */
+    bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);
+  }
+}
+}}}
+The modules are the building blocks of an *injector*, which is Guice's  
object-graph builder. First we create the injector, and then we can use  
that to build the `RealBillingService`:
+{{{
+ public static void main(String[] args) {
+    /*
+     * Guice.createInjector() takes your Modules, and returns a new  
Injector
+     * instance. Most applications will call this method exactly once, in  
their
+     * main() method.
+     */
+    Injector injector = Guice.createInjector(new BillingModule());
+
+    /*
+     * Now that we've got the injector, we can build objects.
+     */
+    RealBillingService billingService =  
injector.getInstance(RealBillingService.class);
+    ...
+  }
+}}}
+By building the billingService, we've constructed a small object graph  
using Guice. The graph contains the billing service and its dependent  
credit card processor and transaction log.
\ 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to