Friday, September 07, 2007

Before ORMs and the LINQ:

"If only I was Bill Gates..."

I was a massive database application developer. In those times I developed Apps that interacted heavily with the back-ends (all sort of back-ends). From my first love MS SQL Server to PostgreSQL, from Oracle to HSQL and the list could grow. Initially I was passionate about doing these heavy transactional developments. But at a later time, I got almost frustrated as most of the things I do in these different apps look too much alike. Copy and paste could be cool sometimes, but not without some slight tweaks. (Even this could be worse of sometimes).

Big guns in software development might snub this article and call me a “rabbit” (Chess slogan for a novice), but truth is we all started from somewhere. This was in my past. I was a novice though, specifically to some tools, but not basically to DB-programming.

I was furious and decided to find a way to overcome this nightmare of re-inventing the wheel. We have heard about code reusability but growing up alone in the realm I found myself was a bit tasking. I had to be finding out on my own stuff I could do to make life easy for me and probably put me on the leverage with the so-called “BABAs” in the field.

Code reusability does not necessarily mean using a chunk of codes in different projects. In fact, as a matter of fact, we have found more code reusability scenarios in the different parts of the same project more often than we see in different projects. So I let my mind got blown with my fantasy and I just exploded. I taught this technique amidst my peers. (If they had understood it then, maybe I would have made a theory that would have been generally accepted).

My Technique

I built a database schema. Looked at it critically and resolved not to ever write the same lines of code twice for any application that will sit atop of it. I have heard about application tiers. I just wanted to explore and probably, explode. I was at this time in my third year in the University – Olabisi Onabanjo University. For those that know the standard back in my school was, you will understand what I mean.

I read extensively (at least to the best of materials at my disposal) and settled to use the 3-tier methodology. I felt funny about this at first considering the fact that I only had a PC (top of the range though: a P4 1.6GHz 256DDRAM). But 3-tier application could work on a single PC.

I just continued……

First, I built a .dll I called the “Data Layer”. The responsibilities of this component are:

  • To ensure a single point of entry to my DB;
  • To ensure a single point for DB query on my DB;
  • To better control DB connections.
  • To keep a persistent Dataset object to act as a virtual DB;
  • To fulfil my promise: “to ever write the same lines of code twice for any application….”

Next was….

I built a C# classes. Each of these classes resembles a database table I have in my back-end. Confused?

Ok it is like this. Assuming you have a database table that has 3 columns namely: SSN, Surname, DateOfBirth, then you’ll create a class that totally resembles that table. Your class would so have 3 fields too namely: SSN, Surname and DateOfBirth. The datatype of corresponding columns and fields must match too. I varchar mapped to string, Integer to int. And so on.

Normally there few operations you can ever perform on a db table. They are: Insertion of new records, deletions of records, search of records and Update of records. Any more? Tell me if there are more…

So I gave each class the responsibility of performing these major operations on the table they simulate. These responsibilities are basically the methods or operations of the classes.

Eureka! Eureka!! Eureka!!! I have found it

Eventually when I built the User interface, my code-behind was very concise and neat. Imagine (for example, to add a new student to the DB, you write something like “

student.Add()”.

To search for a particular student with matric number 00001, you’d wirte something like:

Student st = Student.find(0001);

And you can be sure that the values the “st” object variable is holding is everything about the student whose matriculation number is 0001

That was all.

I was elated. I just ran to compile a book (which never gets published) and was thinking about the name to give this approach I thought was new and totally developed by me. Good I thought and felt like a master. I gathered a small team in school and trying to relay this methodology to them. They did not understand. I wanted to perfect the “new” methodology so I hooked up on the net to get some more materials on Database programming. This was the point I felt “good-bitter” about my discovery. I found “Hibernate”and “NHibernate”, both ORMs and they were all I thought I discovered.

I just “smiled-crying”. Hibernate exists but I found it before I saw it. I felt good. “I am coming up” I said to myself. It was just some good stuff…

However, with the latest Beta version of the MS Visual Studio, LINQ made the spoils I have always dreamt about. Language Integrated Query: The perfect way to do DB programming easier and faster.

1 comment:

Unknown said...

Yea I made it . Cant believe I'm the first person to leave his comment on this edition of your blog but all the same I'm happy I'm doing that now.
This is one of the most interesting 'notes from the past (emphasis on the days of the little beginning)'have read and Yet still got some OOP senses from it .
Cool one from you bro.
Keep up the Good Work