I tend to make "mini MVC's" with Mason. I find that overabstraction leads to the exact inverse of what you are trying to avoid -- redundant code. And that inverse is a God object that tries to do everything for everybody. So, what i do from time to time is create a top level component that handles CRUD, so my update sub would first call a custom validation sub located in the same package. I set up a messaging system so that if a user enter invalid data, they see the same form with their error messages so they may correct and resubmit. I determine what the "response page" is by calling the proper component. If they entered valid data, that component is the list view. If not, that component is the add_update view again.
This method has worked very well for -- giving me enough abstraction to make laborious tasks more fun while not being too rigid that i have to readdress my design over and over. Here is the update() method of one of these "objects" :
sub update { my %args = @_; my @messages = (); my @errors = validate_args( %args ); if (@errors) { # package the errors up as error messages @messages = map {{ class => 'error', data => $_ }} @errors; # override landing page back to update so user can fix errors # $comp is set to 'list' prior to this method call $comp = 'update'; } else { my $sth = $dbh->prepare(' UPDATE thingy SET foo = ?, bar = ? WHERE id = 1 '); $sth->execute( $args{foo}, $args{bar}, ); push( @messages, { class => 'message', data => 'Thingy successfully updated.' } ); } return @messages; }
This method is called by a dispatcher which then determines what to fetch:
For every CRUD method in the "object" there is a corresponding Mason component that acts as the "template." I try to treat these component like HTML::Template templates -- only variable substitutions, conditions and loops -- but i will throw in display code from time to time like TT would allow me to do.$m->comp( "comps/thingy/$comp.md", messages => \@messages, %ARGS );
I hope this adds to the conversation -- I kinda missed what the point was. :/
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
In reply to Re: CGI::Prototype criticism 1 - cannot use POST-redirect-GET paradigm
by jeffa
in thread CGI::Prototype criticism 1 - cannot use POST-redirect-GET paradigm
by metaperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |