[CDBI] Different subclasses for one table / Factory Pattern?
stephan.brunner at gmx.de
Sun Jul 30 20:19:09 BST 2006
I'm currently setting up an application with Class::DBI and sqlite. In the
database, I will store something like a filesystem representation, so I have
a table "dir" for directories and "file" for files.
The application knows different types of files and I would like to use a base
class "File" and subclasses (like "File::Foo" for files of type foo and so
on...) to handle those file types.
Right now, I have only one table for *all* files, but need to create different
File::<something> objects depending on column values (the file name, for
example). I want $dir->files() to return a list of objects in the
per-file-specific subclass. Is it possible to set this up using Class::DBI?
My idea woud be something like a sub new() in the base class "File" (the one
representing the database table) that gets the column values and selects the
appropriate subclass (by calling File::Foo->new(...), for example). But I'm
not sure which exact sub I would have to override and whether this is safe
under all circumstances?
Another possibility seems to be Class::DBI::Plugin::Factory, which (if I
understood correctly) hooks into the select-trigger and reblesses the given
One more way to go would be to redesign the database schema and create
seperate tables for each file type, but that's not as flexible IMO (adding
another filetype would require a change to the schema).
Thank you very much for your advice to a novice,
More information about the ClassDBI