[cgiapp] Re: Model design in C::A/Titanium

Joshua Miller unrtst at gmail.com
Tue Sep 23 18:25:55 EDT 2008


On Tue, Sep 23, 2008 at 5:45 PM, Richard Jones <ra.jones at dpw.clara.co.uk>wrote:

> Rhesa Rozendaal wrote:
>
>> Did you have something like this in mind:
>>>
>>> sub model {
>>>   my $c = shift;
>>>
>>>   return $c->param('model');
>>> }
>>>
>>> Seems a bit, erm,  'trivial' ?
>>>
>>
>> That may be trivial today, but it gives you the freedom to make it much
>> smarter, without having to change the calling code. You can't do that if you
>> have $c->param('model') littered around the code base.
>>
>
> Right. Thanks for that - I was really just checking I'd interpreted Marks
> suggestion correctly.
>
> Actually I quite like the idea of grouping associated db-related method
> calls into WebApp::Model::Foo, WebApp::Model::Bar, etc, but not sure what is
> the best way to get them into the call to $c->model, so I can call them as
> $c->model('Foo')->get_stuff, $c->model('Bar')->update_stuff, etc. Pointers
> appreciated.
>
>
lots of options for that, but this should cover the basic concept:
sub model {
    my $c = shift;
    my $subclass = shift or die "Required attribute missing";

    return $c->param("model::$subclass") if $c->param("model::$subclass");

    my $obj = new MyApp::DB::$subclass->new();
    $c->param("model::$subclass", $obj);
    return $obj;
}
(most of the code from my previous post could also fit in here to avoid
infinite loops, and to automatically require the correct classes - tweak as
needed).


More information about the cgiapp mailing list