<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Cubeia Blog &#187; ioc</title>
	<atom:link href="http:///index.php/blog/archives/tag/ioc/feed" rel="self" type="application/rss+xml" />
	<link>http://www.cubeia.com/index.php/blog</link>
	<description>Tech Lust and Lust of Words</description>
	<lastBuildDate>Tue, 31 Jan 2012 13:11:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Guice Support in Firebase</title>
		<link>http://www.cubeia.com/index.php/blog/archives/57</link>
		<comments>http://www.cubeia.com/index.php/blog/archives/57#comments</comments>
		<pubDate>Thu, 04 Mar 2010 16:46:23 +0000</pubDate>
		<dc:creator>larsan</dc:creator>
				<category><![CDATA[firebase]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[ioc]]></category>

		<guid isPermaLink="false">http://www.cubeia.com/index.php/blog/archives/57</guid>
		<description><![CDATA[I&#8217;ve always wanted to add dependency injection support to Firebase, and today we released a candidate for Guice! And if you ask me, it&#8217;s very cool indeed.
The documentation is a bit sparse at the moment, but can be found on our wiki. The rest of the post I&#8217;ll just show how a small fictional game [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always wanted to add dependency injection support to <a href="http://www.cubeia.org/">Firebase</a>, and today we released a candidate for <a href="http://code.google.com/p/google-guice/">Guice</a>! And if you ask me, it&#8217;s very cool indeed.</p>
<p>The documentation is a bit sparse at the moment, but can be found on our <a href="http://www.cubeia.org/wiki/index.php/Dev/guice">wiki</a>. The rest of the post I&#8217;ll just show how a small fictional game would look using Guice.</p>
<p>To start with, the Guice support comes in a set of abstract base classes, one for each Firebase artefact. And to use those you&#8217;d have to add a dependency to you Maven build (I&#8217;ll assume Maven here, you can of course use whatever you&#8217;d like):</p>
<pre>&lt;dependency&gt;
  &lt;groupId&gt;com.cubeia.firebase&lt;/groupId&gt;
  &lt;artifactId&gt;guice-support&lt;/artifactId&gt;
  &lt;version&gt;1.0-RC.1&lt;/version&gt;
&lt;/dependency&gt;</pre>
<p>And if you haven&#8217;t already got it, you&#8217;d need our repository as well:</p>
<pre>&lt;repository&gt;
  &lt;id&gt;cubeia-nexus&lt;/id&gt;
  &lt;url&gt;http://m2.cubeia.com/nexus/content/groups/public/&lt;/url&gt;
  &lt;releases&gt;
    &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;/releases&gt;
  &lt;snapshots&gt;
    &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;/snapshots&gt;
&lt;/repository&gt;</pre>
<p>Now your all set to go, just extend <em>GuiceGame</em> and return the class of you game processor within the configuration, like so:</p>
<pre>public class MyGame extends GuiceGame {
    public Configuration getConfigurationHelp() {
        return new ConfigurationAdapter() {
            public Class getGameProcessorClass() {
                return MyProcessor.class;
            }
        };
    }
}</pre>
<p>So what&#8217;s the magic then? It is this: The class <em>MyProcessor</em> will be instantiated by Guice and can therefore contain injections. And further, it will be done in a custom scope, per event, thus isolating instances nicely.</p>
<p>You can also add your own modules to the injection context, again by overriding a method in <em>GuiceGame</em>:</p>
<pre>protected void preInjectorCreation(List list) {
    list.add(new MyGameModule());
}</pre>
<p>Which means, you can inject not only stuff from the current table but also, your own classes. So if we continue:</p>
<pre>public class MyProcessor implements MyProcessor {

    /*
     * This is probably configured in the "MyGameModule" configured
     * in the guice game extension.
     */
    @Inject
    private MyHandler handler;

    /*
     * This is a speciality, you can inject Firebase services
     * right into your classes.
     */
    @Service
    private ScriptSupport support;

    /*
     * And another shortcut, if you use Log4j, we have a
     * a helper annotation for you...
     */
    @Log4j
    private Logger log;

    public void handle(GameDataAction action, Table table) {
        // do something here eh?
    }

    [...]

}</pre>
<p>That should give you the idea. You can inject Firebase services as well as a logger (and remember, if you don&#8217;t use Log4j, Guice support the Java utility logging package from scratch). There&#8217;s a couple of things not shown here, for example, you can inject table members directly into the classes and the state object, so you don&#8217;t have to pass those around.</p>
<p>Any catch? Well, when you create your own modules you&#8217;ll need to keep in mind that the processor will only work in a custom scope, called <em>EventScope</em>. So if you have something which needs to be bound not as a singleton or in the default scope, you&#8217;ll probably need to do something like this:</p>
<pre>bind(MyHandler.class).to(MyHandlerImpl.class).in(EventScoped.class);</pre>
<p>And that&#8217;s it! In a few days we&#8217;ll release our script support, which is as you might imagine built on top of the Guice support. And so far? I love it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cubeia.com/index.php/blog/archives/57/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

