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 = @_;
     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,@_);
            $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

_______________________________________________
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/[email protected]

Maybe make "Borrowers" a sub that takes connection params and permits defaults (loaded from a config or something).

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

You could stick 'em into a namespace for tidyness:

   my $rs = 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 = ::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 = shift;
      $self->db->resultset(...);
      $self->db('product')->resultset(...);
   }


-Sir



_______________________________________________
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/[email protected]

Reply via email to