Author: limpbizkit
Date: Thu Jan 1 14:53:48 2009
New Revision: 773
Modified:
wiki/ModulesShouldBeFastAndSideEffectFree.wiki
Log:
Edited wiki page through web user interface.
Modified: wiki/ModulesShouldBeFastAndSideEffectFree.wiki
==============================================================================
--- wiki/ModulesShouldBeFastAndSideEffectFree.wiki (original)
+++ wiki/ModulesShouldBeFastAndSideEffectFree.wiki Thu Jan 1 14:53:48 2009
@@ -1,14 +1,14 @@
=Modules should be fast and side-effect free=
-Rather than using an external XML file for configuration, Guice modules
are written using regular Java code. Java is familiar, works with your IDE,
and survives automated refactoring.
+Rather than using an external XML file for configuration, Guice modules
are written using regular Java code. Java is familiar, works with your IDE,
and survives refactoring.
But the full power of the Java language comes at a cost: it's easy to do
_too much_ in a module. It's tempting to connect to a database connection
or to start an HTTP server in your Guice module. Don't do this! Doing
heavy-lifting in a module poses problems:
* *Modules start up, but they don't shut down.* Should you open a
database connection in your module, you won't have any hook to close it.
* *Modules should be tested.* If a module opens a database as a course
of execution, it becomes difficult to write unit tests for it.
* *Modules can be overridden.* Guice modules support
[http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/util/Modules.html#override(com.google.inject.Module...)
overrides], allowing a production service to be substituted with a
lightweight or test one. When the production service is created as a part
of module execution, such overrides are ineffective.
-Rather than doing work in the module itself, define an interface that can
do the work at the proper level of abstraction. In our applications we use
a simple interface:
+Rather than doing work in the module itself, define an interface that can
do the work at the proper level of abstraction. In our applications we use
this interface:
{{{
-interface Service {
+public interface Service {
/**
* Starts the service. This method blocks until the service has
completely started.
*/
@@ -20,9 +20,9 @@
void stop();
}
}}}
-After creating the Injector, we finish bootstrapping our application by
starting the services.
+After creating the Injector, we finish bootstrapping our application by
starting its services. We also add shutdown hooks to cleanly release
resources when the application is stopped.
{{{
- public static void main(String args) throws Exception {
+ public static void main(String[] args) throws Exception {
Injector injector = Guice.createInjector(
new DatabaseModule(),
new WebserverModule(),
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---