[Dbix-class] Enforcing read-only objects

Ronald J Kimball rkimball at pangeamedia.com
Thu Aug 12 14:34:55 GMT 2010


On Wed, Aug 11, 2010 at 6:09 PM, Ian Docherty (icydee)
<dbix-class at iandocherty.com> wrote:

> Ignoring for the moment the artists, cds, tracks etc. I could see it
> something like the following. (top of my head, first impressions, probably
> totally borked)
>
> In your application code.
> ----
>
> my $factory = MyApp::Business->new({schema => $schema});
> my $label   = $factory->find_label($id);
>
> $label->set_active;
>
> ----
> The factory code might have something like the following
> ----
> package MyApp::Business;
> use Moose;
>
> has 'schema' => (is => 'ro', required => 1);
>
> sub find_label {
>    my ($self, $id) = @_;
>
>    return $self->schema->resultset('DB::Label')->find($id);
> }
> ----
> Your Label class would be something like.
> ----
> package MyApp::Business::Label;
> use Moose;
>
> has 'dbic_obj' => (is => 'ro', required => 1);
>
> sub set_active {
>    my ($self) = @_;
>
>    die "Is Readonly" if $self->dbic_obj->is_readonly;
>
>    $self->dbic_obj->active( 1 );
>    $self->dbic_obj->update;
> }

> Note. The above code is totally untested and likely to be broken and is only
> an indication of the general method! Please don't flame me for syntax errors
> but feel free to suggest alternative ways of doing this. I am still learning
> too!

Please don't consider this a flame.  :)  I just wanted to point out
that your find_label() method is returning a DBIC row object, but I
presume you intended for it to return a MyApp::Business::Label object.
 I believe that would look something like this:

sub find_label {
   my ($self, $id) = @_;

   return MyApp::Business::Label->new(
     dbic_obj => $self->schema->resultset('DB::Label')->find($id)
   );
}

(Also untested, etc.)

Ronald



More information about the DBIx-Class mailing list