[Dbix-class] best way to share schema elements?
Charlie Garrison
garrison at zeta.org.au
Mon Nov 18 01:31:07 GMT 2013
Good afternoon,
On 12/11/13 at 9:28 AM -0800, Seth Daniel
<dbix-class at sethdaniel.org> wrote:
>> I Moosified all my DBIC classes and used the standard `extends`
>> functionality. In your example you will have 3 classes:
>>
>> Schema::RBase
>> Schema::Result::TableA
>> Schema::Result::TableB
>>
>> TableA and TableB will both `extend` RBase. All the shared column
>> definitions (& methods) go into RBase while everything table specific goes
>> into one of the other classes.
>
>This works? I know I've tried this and I could never get it to work.
>DBIx::Class always complained about not being able to locate
>result_source_instance. DBix::Class::Manual::Troubleshooting mentions this
>error and what it means but I could never get sub-classing to work
>properly. I'd be very interested in seeing a working example. I clearly
>was doing something very wrong.
It certainly works for me. I don't have a simple example to
share. Maybe you could share the code that's not working and
someone may be able to assist.
>The only problem with moosifying my own result classes is that I'm already
>using DBIx::Class::Candy. I love Candy because the result classes are so
>much easier to read and simply look prettier. But it doesn't seem to play
>well with Moose. But Moose isn't a requirement of sub-classing so probably
>not an issue.
If I had to choose between pretty code with DBIC::Candy and
Moose; I'd go with Moose. The extra functionality makes it well
worth it.
Regarding the inheritance problem, maybe that is due to
DBIC::Candy; this is from POD:
By default DBIx::Class::Candy:
- sets your parent class
But since I haven't used it, I have no idea whether that is relevant.
>> Put the shared code/devinitions into a Moose role, and use that role
>> `with` your result class. I did that to share the same (postal) address
>> functionality across multiple classes. (I've become a big fan of Moose
>> roles. :-) )
>
>Using roles (or components) is how I was thinking of doing it. I wasn't
>certain if there was a better or more preferred way.
I don't know about best-practice, but using roles is working out
great for my usage. Sounds like Moose will give you more
benefits than DBIx::Class::Candy. Splitting functionality into
roles will make your code easier to manage; I prefer that over
easier to read.
Charlie
--
Ꮚ Charlie Garrison ♊ <garrison at zeta.org.au>
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
〠 http://www.ietf.org/rfc/rfc1855.txt
More information about the DBIx-Class
mailing list