[Dbix-class] How to inject more then one ResultSetClass in one ResultSet?

Matt S Trout dbix-class at trout.me.uk
Wed Oct 31 19:08:06 GMT 2007


On Wed, Oct 31, 2007 at 11:33:12AM +0000, Jonas Alves wrote:
> On 31/10/2007, Matt S Trout <dbix-class at trout.me.uk> wrote:
> > On Tue, Oct 30, 2007 at 03:15:03PM +0000, Jonas Alves wrote:
> > > On 30/10/2007, Jonathan Rockway <jon at jrock.us> wrote:
> > > > Jonas Alves wrote:
> > > > > Hi,
> > > > >
> > > > > I want my dbic components to be able to extend the ResultSet by adding
> > > > > base classes when they are loaded.
> > > >
> > > > How about:
> > > >
> > > >    package MyApp::ResultSet::FooAndBar
> > > >    use base qw/MyApp::ResultSet::Foo MyApp::ResultSet::Bar/;
> > > >    1;
> > > >
> > > > If you want something more dynamic:
> > > >
> > > >    package MyApp::RS::Base;
> > > >    use Moose;
> > > >    extends 'DBIx::Class::ResultSet';
> > > >    1;
> > > >
> > > >    package MyApp::RS::Role::Foo;
> > > >    use Moose::Role;
> > > >    sub foo { }
> > > >    1;
> > > >
> > > >    <same idea for bar>
> > > >
> > > > Then you can say:
> > > >
> > > >    __PACKAGE__->resultset_class('MyApp::RS::Base');
> > > >    __PACKAGE__->resultset_class->meta->apply_role('MyApp::RS::Role::Foo');
> > > >
> > > > Have I tested any of this?  No.  YMMV.
> > > >
> > > > (Dynamically building classes at runtime is probably a Bad Idea anyway.
> > > > I suggest just going with the inheritance method shown first; just
> > > > create a separate RS class for each case where you need one.)
> > > >
> > > > Regards,
> > > > Jonathan Rockway
> > > >
> > >
> > > Hi Jonathan,
> > > I really like your Moose approach, but i would like  a solution that
> > > just require the user to load_component my  extensions and with that
> > > get the ResultSet methods for free.
> >
> > I think the idea of his approach was your common class would generate the
> > resultset_class invocation.
> >
> > That's exactly how resultset plugins are going to work once DBIC goes
> > Moose.
> >
> 
> Yes, I understood. :) But DBIx::Class::ResultSet doesn't use Moose
> yet, and I don't control the schemas where my extensions will be used.
> They might have user defined ResultSets already set and I can't simply
> discard them. I can however distribute a ResultSet base class like the
> one Jonathan suggests, then at runtime I can inject that base class in
> the user defined ResultSets  and after that apply my roles to it. Is
> this a better approach?

apply_role and load_components are not dissimilar.

The important thing is to ensure before doing that that you create your own
subclass to inject stuff into.

It doesn't really matter whether they have user deifned resultsets already
- whatever you find, DBIx::Class::ResultSet or Their::Custom::RS::Thing,
just subclass the bugger, inject an empty marker component so a quick isa
check afterwards will prove that you've already done this, and reset
resultset_class to that. Once you've subclassed it, it really doesn't
matter whether you use load_components or role application - it's your
class and you can fuck with it if you want to :)

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the DBIx-Class mailing list