Catalyst::Model::MultiAdaptor - use plain classes as a Catalyst Model
Given a good old perl class like:
package MyApp::Service::SomeClass; use Moose; has 'id' => ( is => 'rw', ); 1; package MyApp::Service::AnotherClass; use Moose; has 'host' => ( is => 'rw', ); 1;
Wrap them with a Catalyst Model. The package parameter is base package for plain old perl classes. The lifecyce parameter is lifcycle for wrapped class instance. You can set lifecyle listed as below: * Singleton - create instance per applciation * PerRequest - create instance per request * Prototype - create instance per every time.
use Catalyst::Model::MultiAdaptor; package MyApp::Web::Model::Service; use base 'Catalyst::Model::MultiAdaptor'; __PACKAGE__->config( package => 'MyApp::Service', lifycycle => 'Singleton', config => { 'SomeClass' => { id => 1, }, 'AnotherClass' => { host => 'example', }, }, ); 1;
Then you can use Wrapped models like below:
sub action: Whatever { my ($self, $c) = @_; my $someclass = $c->model('Service::SomeClass'); $someclass->method; #yay } sub another_action: Whatever { my ($self, $c) = @_; my $anotherclass = $c->model('Service::AnotherClass'); $anotherclass->method; #yay }
Note that MyApp::Service::SomeClass is instantiated at application statup time.
MyApp::Service::SomeClass
This modules aims to integrate POPO models into Catalyst model.
Application models should be plain old perl class (POPO). Separating model classes from Catalyst makes Model classes more resusable and testable.
Catalyst::Model::Adaptor is very good module for this purpose. but we need to create multiple adaptors if we have multiple plain perl classes.
This modules can easily integrate multiple plain models as Catalyst model.
dann <techmemo@gmail.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Catalyst::Model::MultiAdaptor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Model::MultiAdaptor
CPAN shell
perl -MCPAN -e shell install Catalyst::Model::MultiAdaptor
For more information on module installation, please visit the detailed CPAN module installation guide.