[Catalyst] catalyst tutorial: MyAppDB/Book.pm vs. MyApp/Model/Book.pm

mla maurice.aubrey at gmail.com
Tue May 15 11:47:57 GMT 2007


Matt S Trout wrote:
> On Tue, May 15, 2007 at 01:19:53AM -0700, mla wrote:
>> "So how do you get some of the advantages of ORM and keep control over 
>> SQL? I employ composition and a couple basic design patterns. For 
>> example, I have a base model factory interface that uses chainable 
>> “Loader” modules (ie, DBILoader, MemcacheLoader, MogileLoader) to 
>> retrieve data and return concrete objects."
>>
>> I actually like SQL and feel no need to turn it into Perl, so I
>> wouldn't mind a light-weight approach like this. But OTOH, as
>> Dave Rolsky was arguing earlier, you do want to have the
>> data validation in the object. So it seems like we do need *some*
>> type of light-weight object interface for inserts and updates.
> 
> Well, if you're using DBIx::Class you can get the exact SQL you want 99 times
> out of 100 - I'd argue that if you don't know exactly what SQL it's going to
> generate as you type your perl code then you're not going to be taking full
> advantage of it.
> 
> -I- quite like SQL, but 'perl -c' can syntax check my DBIC code for me and
> it couldn't syntax check manual SQL, plus it's usually less typing :)

That's a good point too :-) Trade offs, trade offs.

I find the Rails ActiveRecord interface pretty appealing:

   http://ar.rubyonrails.com/classes/ActiveRecord/Base.html

I like the notion of protected fields (attr_protected and
attr_accessible) to allow mass assignment directly from the
request parameters.

I like how if you fetch columns that don't exist in the table
the object becomes read-only by default.

And the find() interface seems nice, where basically the
select is just broken into its components (e.g., condition,
order, group, limit, offset). I know they add a bunch of
helper fetch methods through their autoload mechanism.

So with that design you'd get the centralized validation, which
is most critical to me, but then you could use SQL freely
and get back a collection of read-only active record objects
if you did fancy stuff.

I'm not sure if I totally understand Radium's patterns use, but
I don't see a real problem with then having calls to find() within
the models. As long as they're restricted to the models, seems like
it's encapsulated enough.

Anything similar to ActiveRecord on CPAN?

I'll read through DBIx::Class some more. The thing where the
models are used through the schema seemed weird to me and I don't
fully understand it yet. I'd really like to be able to "use Try::User"
and be able to create and update it rather than going through an
explicit db schema layer. I guess I could create another object
that wraps around the DBIx::Class module or something.

Thanks,

Maurice



More information about the Catalyst mailing list