Author: limpbizkit
Date: Thu Jan  1 15:51:04 2009
New Revision: 775

Added:
    wiki/AvoidConditionalLogicInModules.wiki

Log:
Created wiki page through web user interface.

Added: wiki/AvoidConditionalLogicInModules.wiki
==============================================================================
--- (empty file)
+++ wiki/AvoidConditionalLogicInModules.wiki    Thu Jan  1 15:51:04 2009
@@ -0,0 +1,27 @@
+=Avoid conditional logic in modules=
+It’s tempting to create modules that have moving parts and can be  
configured to operate differently for different environments:
+{{{
+public class FooModule {
+  private final String fooServer;
+
+  public FooModule() {
+    this(null);
+  }
+
+  public FooModule(@Nullable String fooServer) {
+    this.fooServer = fooServer;
+  }
+
+  @Override protected void configure() {
+    if (fooServer != null) {
+       
bind(String.class).annotatedWith(named("fooServer")).toInstance(fooServer);
+      bind(FooService.class).to(RemoteFooService.class);
+    } else {
+      bind(FooService.class).to(InMemoryFooService.class);
+    }
+  }
+}
+}}}
+Conditional logic in itself isn't too bad. But problems arise when  
configurations are untested. In this example, the`InMemoryFooService` is  
used for development and `RemoteFooService` is used in production. But  
without testing this specific case, it's impossible to be sure that  
`RemoteFooService` works in the integrated application.
+
+To overcome this, *minimize the number of distinct configurations* in your  
applications. If you split production and development into distinct  
modules, it is easier to be sure that the entire production codepath is  
tested. In this case, we split `FooModule` into `RemoteFooModule` and  
`InMemoryFooModule`. This also prevents production classes from having a  
compile-time dependency on test code.
\ 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