Delphi on Rails?

There is a lot of hype around the open source project Ruby on Rails these days so I spent some time looking into the framework because I’m always curious about new technology. First impression is always important and I really liked the home page for Ruby on Rails for its simplicity and David Heinemeier Hansson’s exciting 15 minutes introduction.

Personally I think it is the commercial open source framework part (read VCL if you like) of Delphi that has been one of the strongest success factors for the product over the years. One of the newest additions of frameworks in Delphi (or BDS if you prefer) is the ECO framework and that framework has some things in common with Rails.

– Yes, before someone rubs it in my face, I know we do not ship every line of source for the ECO framework like with most other parts of VCL. I agree that it is important to be able to recompile and study the source of the foundation you build your application on. I promise you that I nag on the ECO-team every day about this.

The ECO framework is one of the more secret weapons in Delphi. I must admit we have not been very good at educating our developer community on how to benefit from this fantastic framework in the product. A lot of you out there have glanced at it and either got scared because of the first step is a little to large or got disheartened by the modelling surface in the IDE before you even started to look at the framework. I encourage you to take another look!

I started to roll with Ruby on Rails by looking at the Cookbook example. With a few steps you have a simple application up and running with a classic Model-View-Controller architecture using a database but without tedious manual SQL-mapping.

rail2.PNG

Very nice! This is showing that more developers are getting ready to work with domain objects, attributes and methods instead of tables, columns and rows. I remember how hard it was to explain the benefits of model-view back in 1997 when I started to develop applications using domain objects instead of using the database approach with TQueries, etc.

One important part of the Rails framework is the mapping between Objects and the Database. This part of a framework is usually called an Object-Relational mapper (or short OR-mapper) and takes care of all basic CRUD-operations (Create, Retrieve, Update and Delete).

In Delphi.NET (and C#) the ECO-framework can do OR-mapping for you. ECO has been around for some time, it is actually version seven. We have made four major releases for Win32 under the name Bold (Business Object Layer for Delphi) and three DotNet releases. ECO is very model centric than Rails which is more database centric. I like the Rails concept “Don’t Repeat Yourself”. Maybe it is easier for adopters to understand and start with a database centric approach? ECO has a tool that can import a schema from a database but it could be simpler to use…

Anyone who has tried to write an OR-mapper knows that the first steps are very simple and then you will hit a wall of complex problem you have to solve. I tried to provoke the OR-mapper in Rails just to see how mature it is and what would happen.
The cookbook example has only two classes, recipe and category. I created a new category and then a new recipe. I assigned the category pointer in the new recipe instance to point to my new category. Then I deleted my newly created category. BANG! I got a nil pointer error in the view. It happens and is easy to fix, who has not got Access Violations in there applications when the code does not expect nil pointers?

rail1.PNG

But more worrying is that Rails OR-mapper has not updated the other end of the relation at all. Looking directly at the SQL table there is records with category_id pointing to a category that does not exist. And when I added referential integrity to that database Rails got problems updating the database isntead. There might be easy ways get this to work in Rails but I’m a Rails rookie and I have not found it yet. Can someone with deeper knowledge enlighten me?

Anyway, I know that we have spent several a lot of time perfecting the OR-mapper in the ECO framework to solve problems like this. Managing associations between objects is one complex area where much thought has been spent. If you modify one end of a relation, you need to update pointers in the other end and generating the correct order of SQL insert-, update- and delete-statements is crucial to not violate referential integrity in the database.