Κυριακή 20 Σεπτεμβρίου 2009

Object relational mapping: Top down vs bottom up mapping

One of the greatest hypes in modern enterprise software development is the term object relational mapping.

Back in the old days (before 2002), development of database applications was done primarily using the traditional way: SQL queries, JDBC resultsets, ADO datasets, etc. That time I was using Borland Delphi. I was using plain sql queries and datasets (along with databound widgets).

Some day I noticed that Delphi Enterprise (I think since version 6) had a very bizzard feature called Delphi Bold. I was trying to read what was the purpose of this tool and found out that it was an object relational mapping framework! It was completely integrated with Delphi and there were even ORM-bound widgets to help you out in RAD development! The query language was the OCL (Object Constraint Language) and had nothing to do with SQL. Anyway, it seemed quite compilcated to me and decided to drop my curiocity needs on this tool!

However, I had already realized that developing software using SQL queries and databound controls in the long term results to error prone applications that are difficult to manage. It it was then I decided to develop a simple ORM framework in Delphi using data transfer objects and encapsulating the sql queries in a data access layer. The result was worth the trouble since I managed to separate the sql queries and the whole data access code from the rest of the application.

When I entered the Java arena in 2003, I did a search on ORM tools. I found some (Java Data Objects, iBatis), but only one really stood up: Hibernate! I quickly started to investigate hibernate features and decided that it was an amazing tool that could be used by the whole team and completely get rid of manual sql query handling. And I was right, the team loved the tool, especially as a cool term used in their resume.

However, a dilima came up. What is the best approach to create your java model classes?
1. Create your database schema (tables, foreign keys) and use a tool (Hibernate includes one) to automatically create a java class for each of the tables.
or
2. Create your java class model completly ignoring the term "Database" and focus on using the rich expressiveness of class modeling and object oriented paradigm.

Well, from the first moment I selected the second choice, but I then saw many people not feeling comfort at all with me. I sounded a little bit crazy when I was saying create your class model using an object oriented approach and after your are COMPLETELY finished then think how to map your classes to tables.
They all said that there were no reason why to use this tecnique since everything can be done just be carefully designing your database schema and object oriented modeling is too complicated to use without adding any real value.

Even today the majority of the programmers and few (I believe) software architects create the domain model by designing a database schema. The database schema design terminology includes only four terms: table, primary key, unique key, foreign key.
Of course, they make a very elegant database design, and then they automatically create the classes from the database. A table will be mapped to a class, the table columns to class properties of which the primary key will be properly marked and the foreign keys will be mapped as class associations and class properties. Everything seems to be ok or isn't it?

The answer to this question is based on the following idea: A man's thoughts are bounded by the richness of his vocabulary. What this means is that a smaller vocabulary results to simpler thoughts.

Now in our case. The database design model official uses only four terms (table, primary key, unique key, foreign key). The object oriented domain model uses the following terms:
Class, class attributes, class inheritance, class polymorphism, one to one class association (bidirectional or unidirectional), one to many class association (bidirectional or unidirectional), many to many class association (bidirectional or unidirectional). Associations can be specified using sets or lists (lists of unique items or just lists). Also we can put cardinality attributes to these associations (i.e. an order can have at most ten orders items and no less than one).

As you can see our objected oriented vocabulary is using more than 15 terms! This in turn allows to make more complex domain model thoughts.

I am a strong follower of the "be practical" attitude in software programming. I use the technology to make me life better. It is nice to say that I use ORM tools but it has to have a positive result in my productivity. My five year experience in domain class modeliing lead me to the result that the productivity impact in favor of the object oriented domain modeling is absolutely HUGE.

I encourage everyone to put the effort in changing his way of thinking in domain modeling. After all, the exact same problem arised when object oriented programming came out and everyone was thinking procedural. Now the object oriented programming background is a must for every enterprise project.

Κυριακή 13 Σεπτεμβρίου 2009

Why Java - Part 1

This is my first blog entry and I decided to share my thoughts on a vague question: why I use Java and why I recommend it to everyone.

First of all let's see what are the alternatives. Well that depends on what you want to do. If you want a general purpose language that can cover the needs of an average software company which is a mixture of web and desktop applications then you really have two alternatives: Java and .NET and probably Delphi. Unfortuantely I will take Delphi out of the equation because it is kind of obsolete. There are also the increasingly popular dynamic languages like Ruby, Perl, Python and PHP to name a few. But these languages are mostly used for web site applications.

I won't go into a comparison of specific features, because to me Java and .NET are roughly equivalent in technical features. However, I will address some other not so technical issues. I will start from the most important: freedom.

Freedom is the power to choose, the existence of alternatives. Some people think this is not good and they can argue on that. Some others, like me, think it is really good and, believe me, I can argue on this.

Java is all about freedom, alternatives, open source. In Java you have to choose almost everything: the IDE (Integrated Development Environment), the application server, the operating system, the web framework, the persistence framework, and now even the language itself, and many other things. For every item in the previous list there are many, many options to choose and most of them are open source!

On the other hand, .NET is very restrictive in choices. Officially it runs only on Microsoft windows operating system, it has only one IDE (Visual Studio which is very good), only one application server (Windows Server), only one web framework, only one persistence framework, but supports many languages! I admit that there are some alternatives (like Spring .NET and Hibernate .NET which are ports from Java) but they are used by very few people and to many are not even considered an option. The good thing is that all of the above are very good and will help you write awsome piece of software.

So if .NET is such a complete solution without alternatives (or to be exact few of not so good quality), what's so wrong about it? Actually most .NET programmers find nothing wrong with it, probably because they don't have and have not used any alternatives. They all bring up the same argument: If you have two many choices how do you choose which one is better? In contrast, if you only have one choice and everyone believes it is very good, then you follow this choice and you are sure that you won't regret in the future. What is more is that you learn something today that you can use it everywhere you work. You can invest reading on .NET without being afread that you might read something else in the future.

Java unfortunately has many alternatives, and if you choose one you may have to learn another one or more in the future. So the bottom line is that Java requires a lot of reading which is not so good if your goal is to make money from writing software.

The good thing with alternatives and especially open source alternatives is that by studying different solutions of the same problem you become a better and more mature programmer. You also save a lot of money because most of the alternatives in Java are open source. Ask a .NET programmer if they have licences for all the .NET features they use for every developer. Ask them when they deploy the application to multiple servers if they buy proper licences? In Java you can deploy an application to thousand of servers and not pay a penny. You can set up a development environment for a large team of developers and not pay a penny also.

A professional Java programmer should have evaluated and used more than one alternative because on different projects some alternatives apply better. A .NET programmer does not have any alternatives and relies on Microsoft to provide him with better solutions to his problem.

Most Java open source alternatives are of highest quality and outer perform .NET in some issues. Take Spring framework for example. Spring is one of the most amazing frameworks ever conceived. It was not conceived by Sun architects but by Rod Johnson and this shows that although Sun, an advanced high tech company, invented such a powerfull language as Java they did not bother to create a simple and powerfull framework like Spring (Sun J2EE is highly complex). In .NET there is no alternative application framework from Microsoft. However thanks to Spring team a port was made to .NET delivering Spring features to the .NET community. But how many people use Spring.NET? Very few because most of them they learned to rely on Microsoft for the framework. This is a closed minded approach.

Consider another thing, many people on Java community might thought that after Oracle bought out Sun, Java's openess is questionable. But this is not true simply because Java is such an established technology that Oracle would not dare to destroy this golden egg trying to make money by putting an end to Java openess. Java wide acceptance is based on two things: it's very good design and performance, and a vast open source community constantly expanding Java.

On the other hand, if something similar happened to Microsoft (!!!) what would happen to .NET. Well .NET is already closed so there would be no problem at all. So people using .NET tend to feel more secure in terms of vendor support whereas Sun did not go well because it made litle money out of Java due to its openess. Regarding this, the future will show the truth but I am confident that Java will never die because of Sun. There are so many communities willing to take over Java from Sun.

To put an end to this article, the moral about Java vs .NET is: open or closed minded? Which one do you choose? Fortunately things in .NET begin to change but still .NET is years behing when compared to Java in terms of openess. However, the existence of .NET is a very good thing. You should know why: it gives another alternative to choose from, it makes you more mature by studying different solutions to the same problem (some of them are better conceived in .NET).