[Catalyst] Mapping parameters for web services

Matt S Trout dbix-class at trout.me.uk
Wed Oct 24 21:37:32 GMT 2007


On Wed, Oct 24, 2007 at 12:52:21PM -0700, Bill Moseley wrote:
> On Wed, Oct 24, 2007 at 07:10:09PM +0100, Matt S Trout wrote:
> > > The C::P::Server::XMLRPC plugin allows adding attributes to existing
> > > controllers, so you can do:
> > > 
> > >     sub user_detail : Local : XMLRPC {
> > >         my ( $self, $c, $user_id ) = @_;
> > > 
> > >         # validate user_id and place user object in stash.
> > > 
> > >     }
> > 
> > Yes, and I'm saying that that's cute and all, but don't do that.
> 
> Why don't you like that?

Because dealing with N parameter types becomes messy really quickly.

Using OO + polymorphism is much more elegant.
 
> > If you want to share controller code, use a common base class.
> 
> Ok, but In which way?
> 
> So, there's a bunch of existing actions spread around a handful of
> controllers.  Now you want to add in XMLRCP where each request must
> alter in the input parameters to use the existing controller code.
> 
> Do you create new controller classes just for the XMLRPC requests
> where they rearrange the parameters and then call the web controller
> action?
> 
> Or do you factor out the common code into a base class for each
> controller and then have web and XMLRPC (and what ever else comes
> along) to handle those requests?
> 
>     package App::Controller::User;
>     use Base 'App::Contoller::User::Base';
> 
>     sub user_detail : Local {
>         my ( $self, $c, $user_id ) = @_;
>         return $self->fetch_user_detail( $c, $user_id );
>     }
> 
>     package App::Controller::XMLRPC::User;
>     use Base 'App::Contoller::User::Base';
> 
>     sub user_detail : XMLRPC {
>         my ( $self, $c ) = @_;
>         my $user_id = $c->req->params->{user_id};
>         return $self->fetch_user_detail( $c, $user_id );
>     }

Pretty much exactly that. Bit more typing up front but much easier to
maintain and test.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the Catalyst mailing list