mardi 19 juin 2012

IoC in JavaFX

loC frameworks are very useful when you develop modular applications. I’ve been working with Guice and Tapestry IoC (which is an independent module of the Tapestry project) in the past, but I had to abandon them when I switched to JavaFX since none of them seemed to be compatible with JavaFX.
The main issue was about the use of annotations. They are both heavily depending on annotations to define the injection mechanisms - and JavaFX unfortunately does not allow us to use annotations…
However, the Tapestry framework provides another way to access services by requesting them directly to a registry without using annotations. (I didn’t find something similar in Guice, but it doesn’t mean that there is no way to get Guice working with JavaFX).
To play with tapestry injection, the first thing you need is to define an interface describing your injected service, let’s call it TestInterface:
(actually it’s an abstract class, since JavaFX does not use interfaces, but it doesn’t matter)
Next step is to define a concrete implementation of this interface that will be injected, for example TestInterfaceImpl:
The core concept of Taspestry IoC framework are the modules. Basically a module define some bindings between interfaces and their implementations. Let’s define a pure JavaFX module binding our injected service:
(Note that the concept of “autobuilding” will not be working, since it requires to define a “bind” method, which is a reserved keyword in JavaFX - not idea how to fix this for the moment)
We can now set up a registry with the defined module:
Well, we are now ready to request the service corresponding to the TestInterface simply by asking it directly to the registry:
And you should get the instance of TestInterface you defined in the corresponding module!
Finally, to get things a bit cleaner you can move all the registry stuff the a separate class:
This way, IoC.registry will give you an access to your registry in your application. Just make a call at IoC.init() before using it. This is not as “beautiful” as annotations, but still much better than no IoC. Let me know if you get a more elegant solution!
Note: This article is not intended to be a tutorial for taspestry-ioc, you will find more documentation about it in this howto and on the official website. Moreover, there might be other IoC frameworks that work with JavaFX, just let me know if you are successfully using another one!
Note 2: Tapestry IoC is following a strict “fail-fast” policy and building the registry will fail if your module contains unrecognized methods. This is a bit problematic with JavaFX modules. Actually, all JavaFX classes implements the com.sun.javafx.runtime.FXObject interface. So a JavaFX class contains systematically a bunch of methods inherited from this interface, that tapestry does not recognize. This will lead to an error message like this when you try to register your JavaFX module into the registry: “java.lang.RuntimeException: Module class injectiontest.services.TestModule contains unrecognized public methods: public boolean com.sun.javafx.runtime.FXBase.getAsBoolean$(int,int),[…]”. There is a corresponding jira entry. You can get the patch from this ticket to solve this issue for the moment.

Aucun commentaire:

Enregistrer un commentaire