Friday, October 9, 2009

Jspx Dependency Injection





Jspx is up to latest java technologies, Dependency Injection is now used to
less the amount of code in the controller. Dependency Injection is used for better
and maintainable applications. Since the beginning of the framework, Jspx is always
aiming at removing boilerplate code.






Jspx Controller


Developing web application using Jspx is relatively easier than other frameworks; however the efforts for providing much simpler and easier did not stop just there. As we saw in the demo, interacting with HTML controls declared in view code (HTML file) requires declaring server side java controls for each of them.

Assume that we have a page with input field [UserName], if you want to interact with this control from java code in the page controller, and then you have to do the following:


  1. HTML control with a given id i.e. userName

  2. Java instant of TextBox class.

  3. This instant should have a name the same as the id given to the HTML control.

  4. This inst ant should not be null.

  5. This instant should have getter and setter.


Any missing step of these five steps will not link the HTML control to the java control and will make any changes applied to the server side control will not be reflected to the end user. Also any initial values set in the HTML page will not be visible.
Despite the fact how simple these steps are, there are several issues:


  1. There has to be a lot of code that is irrelevant to the business logic.

  2. The name of the getters and setter should match the id of the control

  3. The coupling between HTML and java is limiting the flexibility of refactoring.

  4. Initializing controls has a negative impact on performance.

The solution for this aimed at removing all these issues through simple way of declaring jspx web controls and also to remove the coupling. The solution was to simply declare the control in the page as following


As you notice a new annotation [JspxWebControl] is used to annotate the declared control.
There is no need for any getters or setters for the control.
There is no need to initialize the control or change its visibility.
                    

You may be wondering how this would clear the coupling between the id of the html control and the name of the declared object. The answer would be through using the annotation to use attributer [name] to set it to the id of the HTML control in the HTML page.


Now if the id of the HTML control changes, this will require only changing the value of the name attribute, also any refactoring in the java control will not require any change.


Dependency Injection is then used to inject the declared control with the HTML control declared in the HTML page. So at runtime, this declared control will not be null and any changes applied to the rendered HTML control.


1 comment: