Archive for the ‘Java’ Category

Comparing between Guice and PicoContainer

  • Guice

The first thing u’ll notice is that Guice makes extensive use of Java 5 language features. ie, generics and annotations. While generics lead to the extraordinary type safety mentioned by Kevin Bourrillion, annotations couple your code to the Guice framework. If u’ve really picky about loose coupling you might ask why you should use a framework that adds a dependency to itself? You end up with lots of imports of the Inject annotation scattered all over your code base. And it’s getting worse if you have to use Singleton, ImplementedBy and so on and so forth. You may want to define that elsewhere because your classes shouldn’t know any thing about their usage or where dependencies should be Inject – That’s what inversion of control is all about, isn’t it :).

However, for those who aren’t so pickly there are some really cool things about Guice:
– it is type safe
– it reports same error messages
– it is small and very fast

Well it is really type safe: if you ask Guice to give you an object of type Bar, it’ll do so – no cast to Bar required. The authors of Guice don’t want it to be used as a simple service locator for a good reason: your code would still be coupled very tightly. That being said you want to call Guice only in some places in your code where it hand you a top-lever class where all the dependencies are injected. Whether you’ll have to do a cast on these few occasions shouldn’t be that bad; this puts the big feature of type safety into another perspective (read: isn’t that important). But this is definitely a plus for Guice though.

Reasonable error messages are one point for Guice: if something goes wrong you can identify the problem easily. From a user’s perspective this should be the case anyway but Guice manages this very well – as far as I noticed it.

  • Pico Container

PicoContainer is a nice piece of software that facilitates dependency injection without much overhead: no external configuration files (read XML files) and no annotations needed. All you have to do is register your components with the container; for small projects this is an effortless thing.

As with Guice you write everything in Java and can reap the benefits of your IDE’s refactoring capabilities: if you change a class’s name all references will be changed too

Categories: Java