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

Marc Chantreux khatar at phear.org
Sat Jun 5 23:13:58 GMT 2010


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 = @_;
    my ( $schema, $connected_to ) = @{ $conf{schema} };
    $cnx ||= $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 = $source . '::';
	no strict 'refs';
	*{"${pkg}resultset"} = sub {
	    my $rs = $cnx->resultset($source, at _);
	    $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
	    $rs;
	};
	for my $method (qw/search find /) {
	    *{"${pkg}$method"} = 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 => [ 'MyApp::Schema' =>  Schema->connect( sub { ... } ) ]
, class  => 'HashRefInflator' # not implemented yet 
);

class Borrowers {
    method members_of_branch( $class: Str $branchcode ) {
	$class->search( { branchcode => $branchcode } ); 
    }
}

my $rs =  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 



More information about the DBIx-Class mailing list