Any one who knows me will know that I have for a long time been a skeptic when it comes to object relational frameworks, this could be because I am getting old and can't deal with all this newfangledness or it could be because it never really sat well with the way my brain sees software, but whatever. I first saw Michael Schwern talk about Class::DBI in 2000 and was impressed with the coolness of the idea, but have assiduously avoided using it in my own programs ever since. I've seen it get installed as a dependency of something else I wanted to install but never once typed 'use Class::DBI' in a text editor.
So recently I was half way through writing a set of data classes for a medium sized web application and was beginning to lose the will to live, everything worked fine but I was getting really bored of preparing and executing the necessary SQL statements and using the data to variously populate the classes, hand defining the relationships and then passing the data to Template as required. I'd already demonstrated the first cut to the person who had commmisioned the application and I just needed to wrap up some more details and add a little more functionality to the frontend. It felt like a chore - this isn't my day job you understand, now if only something could take all the hassle and ugly DBI code away and just make this stuff work as nicely as the hand crafted classes... I was being lured into the Object-Relational crack-den.
But I'm still going to have to create a bunch of classes by hand, even if I do use Class::DBI and I am going to have to replicate all those foreign key definitions I had carefully put into the database in a whole bunch of 'has_a' and 'has_many' calls. Surely there must be something that could do all this for me, write out the code based on the database schema or something. Oh there was something better.
I'd seen Class:DBI::Loader referred to before but was never particularly interested in examing it closer, but now it fit my requirement perfectly: it examines my database and dynamically creates the data classes and the relations between them; I don't have any annoying module files to deploy and when I change the structure of the database the data classes, being dynamically created, reflect the changes without any effor on my part. A couple of little test programs to verify that it did actually work with the structure of my database and to familiarise myself with the way things worked and then I set about redoing the whole application to use this new (to me) wonder. A little jigging around in the templates to deal with the new accessors, a couple of changes of case from the names of my original classes and some aesthetic changes of column names in the database from FooKey to Foo and it was all done in an hour or so and I had excised some 700 lines of code in the process.
I would thoroughly recommend this module (and by extension Class::DBI itself) to anyone. There are mutterings in various quarters about these modules but these seem to be about things unrelated to the actual functionality or if they aren't they are mostly so vague as to be best to ignore.