[Catalyst] Catalyst::Model::Adaptor - because models shouldn't *do* anything

Jonathan Rockway jon at jrock.us
Sun Oct 7 09:50:13 GMT 2007


Hi all,

I've been noticing that more and more people seem to be writing Catalyst
models that do things that other modules on CPAN don't.  This is not
good.  Instead of writing a reusable module, people are tying their
logic to Catalyst, which is bad for everyone.  When you tie your Model
to Catalyst, you're stuck with Catalyst.  To test it, you have to mock
Catalyst.  To use it outside of Catalyst, ... you can't.  It just makes
everything painful, and for absolutely no reason.

Hopefully you see the problem.

What you should do instead is write a class that does your Model stuff
without referring to Catalyst.  Once that's written and tested, you just
need a few lines of code to glue your class to Catalyst.  Now your logic
is available inside AND outside of Catalyst. =


If you are looking for something on CPAN that does this, look at
DBIx::Class and Catalyst::Model::DBIC::Schema.  You'll notice that
C::M::D::Schema doesn't do anything logic-related.  It's 100% glue to
make using your DBIC schema from Catalyst easy.  All the important stuff
works exactly the same without Catalyst.

To encourage people to design their models this way, I've released
Catalyst-Model-Adaptor, which will do all the gluing for you:

http://search.cpan.org/~jrockway/Catalyst-Model-Adaptor-0.01/lib/Catalyst/M=
odel/Adaptor.pm

There are actually three variants, Adaptor, Factory, and
Factory::PerRequest.  The Adaptor variant will create one instance of
your class at application startup time, and will return that every time
you call $c->model.  The Factory variant will create a fresh instance of
your class every time you call $c->model (and you have an opportunity to
pass data about the request to your model, although you should avoid
this if possible).  Finally, the Factory::PerRequest variant will return
a new instance of your class for each request.

If none of these patterns meet your needs, just write the Catalyst model
part yourself.  These modules are only about 30 lines of code combined,
so nothing complicated is going on.  They just make it really easy to
glue plain classes to Catalyst.

Anyway, I hope this makes it easier to write clean, reusable code for
use with Catalyst.  Have fun!

Regards,
Jonathan Rockway



-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 370 bytes
Desc: OpenPGP digital signature
Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20071007/886a=
5d4a/signature.pgp


More information about the Catalyst mailing list