[Dbix-class] Working with Moose - Best Practices

Rodrigo rodrigolive at gmail.com
Tue Aug 9 07:42:23 GMT 2011


>
>
>
> Are most developers following the double declaration path I started with =
or
> are their better ways/other modules that would make this work better? Is =
it
> best to stick with double declaration until DBIC 0.9 comes out with full
> Moose integration?
>
>
In my neck of the woods, for large applications, we're using the DataMapper
pattern (separation of business model and database layer), which translates
into having a model layer like yours:

Model::User->create('bob');


Which in turn wraps, into a single transaction if possible, calls such as:

$schema->resultset('User')->create({ username=3D>'bob', password_reset=3D>1=
 );
$schema->resultset('UsersInGroup')->create( ... );
$ldap->create_user( ... );
$email->notify_admin( "user $user created" );
$email->notify_user("your password is..." );

I don't think you need use_moose=3D>1 if you're going towards this pattern.=
 I
recommend taking a look into  MooseX::Types::DBIx::Class, which can be
combined with delegation ("handles") and method modifiers ("around") that
help you simplify method calls:

has 'row' =3D> ( is=3D>'rw', isa=3D>Row, handles=3D>[qw/insert create delet=
e/] );
has 'rs' =3D> (is=3D>'rw', isa=3D>ResultSet, handles=3D>[qw/search find/] );


In the beginning it feels like you're duplicating code a lot, ie. when the
Model::User->create_user method only has a single line
"$schema->resultset('User')->create(...)" in it. But then, as your model
complexity grows, you'll be glad you have DBIC  for data and the Model, with
unlimited Moose powers, for everything else.

OTOH, for a simpler model (ie. the MojoMojo wiki) keeping everything in DBIC
is perfectly fine and may simplify your app enormously. Although I think
conceptually Moose and DBIC don't play along well in the same Result
class: they're each a OO system on their own despite "patches" such as
use_moose=3D>1, DBIx::Class::MooseColumns and moritz's cool experimental
MooseX::DBIC. In my experience, inheritance and roles work ok, but new(),
create() and accessor incoherencies are confusing enough to make me not want
a moose in my dbic tea.

Eden's comments in this thread are it:
http://dbix-class.35028.n2.nabble.com/minimalistic-Moose-DBIC-glue-module-t=
d5100128.html

-rodrigo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110809/414=
34f90/attachment.htm


More information about the DBIx-Class mailing list