[CDBI] Different subclasses for one table / Factory Pattern?

Stephan Brunner stephan.brunner at gmx.de
Thu Aug 3 21:31:46 BST 2006


Am Donnerstag 03 August 2006 18:43 schrieb Perrin Harkins:
> On Sun, 2006-07-30 at 21:19 +0200, Stephan Brunner wrote:
> > 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?
>
> You can look at Class::DBI::Relationship::ISA or you can try making
> something yourself.  When I needed to do something like this, I made a
> class that just had the primary key and the class name for columns and
> provided a method which would fetch the correct class of object.  That
> does mean extra database fetches.

Perrin, thank you for the hint on ...::ISA. I looked at it, but, well, its 
docs read a bit like "better don't use this module";-)

Right now, I'm thinking about giving up my subclasses in favour of a method 
File->type() that tells me what kind of file I'm dealing with. I dislike the 
idea because I'll have methods in package File that are invalid for certain 
objects (for objects of wrong ->type()). But I like it because I can handle 
all this in my own code and get full control without digging into any 
inheritance internals. There are no additional database queries, and in the 
application itself, I don't really mind whether to use 
$file->isa('File::Foo') or $obj->type to decide what to do with a specific 
file.

Do I overlook something essential with this approach?

> - Perrin

Stephan



More information about the ClassDBI mailing list