Saturday, March 20, 2010

ORM (Object relational mapping)

As developers the data we manipulate is the one represented as an object of some class and is typically in the memory of the computer. But most of the applications (typically web applications) need to have this data persisted and that's where databases come and we developers start to feel little itchy as these databases organize data in a different way then we are used to.

The databases have tables, tables have rows and columns and tables are related to each other using by some keys. In order to manipulate databases there are query languages, stored procedures etc. A totally different way to operate, code and view our data.

Traditionally web application developers have to have a good knowledge (if not master) of SQL in order to work with databases but it has always been desired that in our code any data we see is in form of objects and this is where a middleware which does the translation of view of data as an object of class to a row in the table of database is very useful. This middleware is known as Object relational mapper.

In the Java world, Hibernate is the most famous ORM library/tool. 

Read more about Hibernate and ORM here:

Model-View-Controller

MVC is such a famous programming pattern that there is no need for me to explain it so I am just providing the link to place where it is best described in the simplest possible words.

http://c2.com/cgi/wiki?ModelViewController

Tuesday, March 16, 2010

Dependency Injection

Listing 1:
public class A {
   B b;
   
   public A(B b){
      this.b = b;
   }
}

A a = new A(new B());

In the above listing we have created a Class A which depends on another Class B.

In object oriented programming it is better to use interface or abstract types so that there is a flexibility in terms of implementation. Given this lets change the code to the one shown in listing 2.

Listing 2:
public interface IB{
}

public class B implements IB { }

public class A{
       private IB b;
       public A(IB b){
          this.b = b;
       }
}

As evident from the Listing 2 we now have an interface IB and B is one of the classes that implements it. We now can have B', B1 etc implementation of IB.

Given all this we can now create an object of B and pass it to the constructor of class A.

A a = new A(new B())

Now if I tell you that the Class B is dependent on Class C which is dependent of Class D etc there will be similar type of code that we will write to pass them around in either constructors or setter methods.

While this type of code structure helps in choosing the appropriate implementation (at coding time) it is not ideal for situations when the implementations are supposed to be chosen at runtime i.e. not by the developer rather by the deployer of the application.

This is where spring framework comes and help in specifying the object graph/dependencies in an XML file. The objects are  called beans and the process of specifying the dependencies is called wiring of beans.


<beans>

<bean id="my_b" class="B">
</bean>

<bean id="my_a" class="A">
<constructor-arg ref="my_b"/> 
</bean>

</beans>


In the above XML file (mybeans.xml) we specified a bean with id "my_a" and tell that its constructor argument should be the bean "my_b "whose class is B.

This XML language is sophisticated enough to specify constructor arguments, set properties (setter/getter java methods) and many more things.

Your application code then becomes:

ClassPathApplicationContext applicationContext = new ClassPathApplicationContext("mybeans.xml")

A a = (A)applicationContext.getBean("my_a");

As evident from this example we did not create any objects in our code rather they were created by the spring framework and are accessible to us in the code using an API getBean by passing the id of the object we desire.

Read more about these topics here:

Not everybody likes too much magic

Grails not only simplifies web application development for Java platform but also reuse solid libraries and technologies that existed before it. Although it is an oversimplification of what Grails does but you can safely say that it provides nice wrappers for some of the existing technologies such Spring (MVC) framework, Dependency injection, Object-Relational mapping, View technologies etc.

Most of these wrappers are so nicely done that the development some times looks like a magic. Things just work and some times we do not understand why ?. 

Next few posts I have are for developers for whom Grails is the first Java framework they have worked with. They may not be familiar with concepts like Dependency injection or MVC etc. Now Grails does such a good job at hiding these concepts that it leaves a feeling a mystery for some. In these next posts I will try to demystify the mystery.

Grails to Rescue

This is my second rescue blog (check out Google App Engine to Rescue) where I will talk about building Java web applications using Grails.

Grails - www.grails.org

Best book for learning Grails - Grails in Action

An excellent free e-book about Grails - http://www.infoq.com/minibooks/grails-getting-started

Follow these people on Twitter to learn about how Grails framework is evolvingt:

@glaforge
@marceloverdijk 
@cdupuis 
@scottdavis99
@pledbrook
@glen_a_smith
@grailsframework
@burtbeckwith
@graemerocher


Join the grails user and developer mailing lists: http://grails.org/Mailing+lists