[Dbix-class] Subclassing result classes sanity check

Bill Moseley moseley at hank.org
Mon May 9 01:34:58 GMT 2011


I have an existing set of schema and result classes used by an application.
  I now need to provide access to another very similar database that has a
sub-set of tables and where table names are renamed and some tables are
missing some columns.  But, I want to share my existing result and resultSet
methods, with the ability to override as needed.

I'd like to check that my approach is sane.

Currently I have my schema class, App::DB that only contains a call to
load_namespaces( default_resultset_class =3D> 'ResultSet' ), and inherits f=
rom
App::DB::Base which has some additional schema methods.  App::DB::Base
inherits from DBIx::Class::Schema.

I've created a new schema class, New::DB that also
calls __PACKAGE__->load_namespaces but it also inherits from App::DB::Base;


I then have New::DB::ResultSet.pm that inherits from App::DB::ResultSet so
those methods are shared.  I can add, say, New::DB::ResultSet::User if I
need to override, for example.

All this works fine.

The question I have is about my result classes.   My Result class "User"
inherits from App::DB::Result which inherits from DBIx::Class;

My existing result classes have additional row methods, for example
something like this in my App::DB::Result::User class:

sub full_name {
    my $self =3D shift;
    return join ' ', $self->first_name, $self->last_name;
}

I want to share those methods between my two result classes (i.e.
App::DB::Result::User and New::DB::Result::User).  I'm thinking the only way
to do that is with a role because my new class New::DB::Result::User needs
to inherit from New::DB::Result and that inherits from App::DB::Result.

Is there another way to do this?  Or does this seem like the correct
approach?

BTW -- I'm currently using App::DB::ResultRole::User for result roles.  I
assume that's as good of place as any.



-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110508/a20=
d2934/attachment.htm


More information about the DBIx-Class mailing list