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

Michael Reece <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.class-dbi
Message-ID <[email protected]>
i for one appreciate you posting your episodes to the list, even if  
you aren't seeing responses.  i haven't had to tackle this particular  
problem yet, but i am sure it will come up and appreciate having your  
thoughts and experiences documented for future reference.

michael.

On Sep 11, 2006, at 8:03 AM, Oliver Jeeves wrote:

> Oliver Jeeves wrote:
>> Oliver Jeeves wrote:
>> ...
>> However, this leads naturally to my next question; how do I  
>> achieve this
>> sort of thing?
>>
>> Currently, I'm thinking of having the base class call retrieve on the
>> derived class, which will probably mean extra DB lookups, but will
>> definatly work. Does anyone have a better solution?
>>
>> -Oli
>
> Episode IV: A New Hope
>
> It's been awfully quite on this list recently, where has everyone  
> gone?
>
> Anyway, I think I now have a working solution; override the construct
> method.
>
> Looking at the internals of CDBI, it appears that all the constructors
> evenatually call construct (except, maybe, via iterators, but by this
> point I had gone cross eyed, so I'll leave this to someone else to
> check), so my approach is to override construct with a simple wrapper
> that just calls construct on the correct class.
>
> Initially, this caused an infinite loop as the I was calling construct
> on a derived class which didn't define its own construct method. I got
> around this by calling SUPER::construct unless the class the method  
> was
> called on was equal to __PACKAGE__.
>
> So The code looks like this:
>
> sub construct
> {
> 	my $proto	= shift; ## class or obj
> 	my $data	= shift;
> 	
> 	my $class = ref $proto || $proto;
> 	
> 	if ($class eq __PACKAGE__)
> 	{
> 		my $new_class =
> 			'My::Base::Class::' . $data->{'type'};
> 		return $new_class->construct($data);
> 	}
> 	else
> 	{
> 		return $proto->SUPER::construct($data);
> 	}
> }
>
> In my code, the (Essential) DB column 'type' determines which subclass
> the object should be.
>
> This means that my derived classes automatically inherit all the CDBI
> relationships from the base class, and can also add their own
> relationships, which you can't do if re-blessing a CDBI object after
> creation.
>
> I hope this is of help for anyone trying to do something similar,  
> and I
> hope that someone can point out any problems or potential pitfalls  
> with
> this approach before I get myself into serious trouble!
>
> -Oli
>
> _______________________________________________
> ClassDBI mailing list
> ClassDBI-Ra3b/[email protected]
> http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

---
michael reece :: software engineer :: [email protected]

_______________________________________________
ClassDBI mailing list
ClassDBI-Ra3b/[email protected]
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.