I'm working on a project using some Perl classes (regular hash-based objects) as well as some MySQL database tables, where the two often mirror each other. I'm hoping for some feedback from those here in the Monastery on proper ways to handle the relationship between the classes and the tables.
For now, my tables are named after the classes at the bottom of my inheritance hierarchy. For example, given the 4 classes:
Car FastCar Cadillac NashRambler
the db tables for that would be "cadillacs" and "nash_ramblers".
One thing I immediately notice though is that the two tables have a lot of duplication (ex., both classes inherit their flux_capacitor_present instance attribute from FastCar, and so each have a flux_capacitor_present field in their respective db tables). Should there be a fast_cars table, where the cadillacs and nash_ramblers tables would each have fast_car_id fields holding foreign keys?
Is there a general rule you might recommend for how to estimate how much to abstract, and how much db duplication to allow? How closely should your db table layout follow your class layout?
The other thing I'm finding myself doing is writing a lot of code to extract data from a db table to build my objects. For example:
# Inside the Garage class, which will contain a list of Nash Ramblers. +.. # Get info on some of them that the garage cares about. my $query = 'select owner, mileage, color, ... from nash_ramblers ...' +; my $result_ref = $dbh->selectall_arrayref( ... ); $self->{nash_ramblers} = []; # Store the list in this instance attribu +te. for ( @{$result_ref} ) { my $a_ref = $_; my $car_ref = NashRambler->new(); $car_ref->set_owner( $a_ref->[0] ); $car_ref->set_mileage( $a_ref->[1] ); $car_ref->set_color( $a_ref->[2] ); # ... snip a bunch of similar setter calls ... push @{ $self->{nash_ramblers} }, $car_ref; } # Ok, now for the Cadillacs...
The Garage constructor has a lot of that sort of thing to generate the lists of objects it contains. Is there a common Perl idiom folks use to avoid that sort of repetition?
In reply to Design help: OO Perl and db tables by j3
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |