[Dbix-class] DBIx::Class::Direct: what's wrong with that ?

Sir Robert Burbridge rburbrid at cisco.com
Tue Jun 8 14:28:08 GMT 2010


On 06/05/2010 07:13 PM, Marc Chantreux wrote:
> hello all,
>
> I'm pretty new in the ORM world and i'm trying to figure out how to
> write very simple webapps as quick as possible. DBIx::Class is awesome
> and i would like to use it to add persistence to my buziness objects.
>
> For a given Users class, i would like to write Users->search({}), not
> $schema->resultset('Users')->search().
>
> I wrote a little package as a proof of concept:
>
> package DBIx::Class::Direct;
> use YAML ();
>
> # store the connexion to the schema
> our $cnx;
> sub now {
>      my %conf =3D @_;
>      my ( $schema, $connected_to ) =3D @{ $conf{schema} };
>      $cnx ||=3D $connected_to;
>
>      # foreach sources of the schema, create a package with the methods
>      # resultset (to call the resulset method of the schema )
>      # search,find (to use the resultset created above)
>
>      for my $source ( $schema->sources ) {
> 	my $pkg =3D $source . '::';
> 	no strict 'refs';
> 	*{"${pkg}resultset"} =3D sub {
> 	    my $rs =3D $cnx->resultset($source, at _);
> 	    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
> 	    $rs;
> 	};
> 	for my $method (qw/search find /) {
> 	    *{"${pkg}$method"} =3D sub { $source->resultset->$method(@_) };
> 	}
>      }
> }
> 1;
>
> so i was happy to run this code:
>
> package main;
> use Modern::Perl;
> use MooseX::Declare;
> use DBIx::Class::Direct;
> DBIx::Class::Direct::now
> ( schema =3D>  [ 'MyApp::Schema' =3D>   Schema->connect( sub { ... } ) ]
> , class  =3D>  'HashRefInflator' # not implemented yet
> );
>
> class Borrowers {
>      method members_of_branch( $class: Str $branchcode ) {
> 	$class->search( { branchcode =3D>  $branchcode } );
>      }
> }
>
> my $rs =3D  Borrowers->members_of_branch('MIR');
>
> But now i'm worry: if it was such a good idea, why didn't i found it on
> CPAN ? I see two answers:
>
> - it already exists and i missed it.
> - my idea is just stupid and i missed a point.
>
> In both cases, i really appreciate your advices.
>
> Regards
> marc
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.=
co.uk
>    =


Maybe make "Borrowers" a sub that takes connection params and permits =

defaults (loaded from a config or something).

    my $rs =3D Borrowers($dsn, $user, $pwd, $attr)->members_of_branch('MIR'=
);
    my $rs =3D Borrowers->members_of_branch('MIR');

You could stick 'em into a namespace for tidyness:

    my $rs =3D DB::Borrowers(...)->members_of_branch('MIR');

or since it's not willy-nilly construction of methods, but a controlled =

set (with an initial capital letter, to boot), you could stick 'em in main:

    my $rs =3D ::Borrowers(...)->members_of_branch('MIR');

It isn't *actually* class-based, but might be a decent emulation of it.

I've got controlled, uniform environment at work and use something =

similar; I have a package that lets my app associate names with db info, =

and connect easily, and objects can tie into it as a role:

    package My::Object;
    use Moose;
    with 'My::DB::Package';

    sub do_something {
       my $self =3D shift;
       $self->db->resultset(...);
       $self->db('product')->resultset(...);
    }


-Sir



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100608/14b=
12706/attachment-0001.htm


More information about the DBIx-Class mailing list