FAQ

[Catalyst] Making Controllers thin and Models thick

John Napiorkowski
Jul 18, 2007 at 5:00 pm

--- Bill Moseley wrote:

On Wed, Jul 18, 2007 at 07:49:50AM -0700, John
Napiorkowski wrote:
I've come to the reluctant conclusion that for
anything other than trivial applications you will need
to validate in a couple of places. Obviously the
database should be properly designed to enforce
integrity rules. If you are using a database that
let's you create custom types, like Postgresql, you
can take it a bit further and actually create self
validating types for your columns. I do this for very
common things like email addresses. Postgresql is
nice for this since you can create custom types and
domains using Perl as your procedural language.
Yes, validation is a bit generic of a term.

For me, the database should try and enforce a valid
state of
the application. An order row better reference a
customer row.
An order status better reference a valid value in
the status table or
have a valid check constraint.

Doesn't mean a bit of raw DBI can't hose the
application, of course.

For application state changes I try and abstract
that into a method in
the model. I rarely have ORM specific code in the
controller.

I tend to not validate things like email addresses
or phone numbers at
the RDBMS level -- it's not critical to the state of
the application
typically. That's left to an I/O layer that is my
user input
validation code. That code can be used outside of
Catalyst -- and
as I commented before, it's not tied to the
model/ORM either.

Application state changes are typically caused by
user input. And user
input is often multiple fields. So, it makes sense
to defined
forms for handling a set of fields all as one unit.
That validation
is also not easily defined in a static configuration
file (or by
constraints on the database) so it also makes sense
the forms are
bundled with code.

I doubt there's any best approach. But, if the idea
is thin
controllers then I like using one line of code. ;)

--
Bill Moseley
moseley@hank.org
Considering that you can set actions and action
classes via the central configuration file for a
controller you could eventually get down to not
needing any code, just configuration declarations.
Still need to write a stub for your controllers
though.

I'm going to trying using the Workflow module
(http://search.cpan.org/~jonasbn/Workflow-0.28/) on my
next project and see if that can fill a missing piece
for me conceptually. I think it can help cover the
state issues you mentioned. Plus it has a validation
framework as part of it.

I end up with more in the database because the
applications I'm writing will end up being used by
other systems and I can't count on the PHP people
validating their incoming stuff properly :)

--John

_______________________________________________
List: Catalyst@lists.rawmode.org
Listinfo:
http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive:
http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/



____________________________________________________________________________________
Need a vacation? Get great deals
to amazing places on Yahoo! Travel.
http://travel.yahoo.com/
reply

Search Discussions

9 responses

  • Ian Docherty at Jul 24, 2007 at 2:43 pm
    Thanks for pointing out the workflow CPAN module. I am very interested
    in this for my current project and must spend some time looking at it.

    Has anyone actually used workflow within a Catalyst application and can
    they give the list their opinion and experiences with it?

    Regards
    Ian Docherty

    John Napiorkowski wrote:
    Considering that you can set actions and action
    classes via the central configuration file for a
    controller you could eventually get down to not
    needing any code, just configuration declarations.
    Still need to write a stub for your controllers
    though.

    I'm going to trying using the Workflow module
    (http://search.cpan.org/~jonasbn/Workflow-0.28/) on my
    next project and see if that can fill a missing piece
    for me conceptually. I think it can help cover the
    state issues you mentioned. Plus it has a validation
    framework as part of it.

    I end up with more in the database because the
    applications I'm writing will end up being used by
    other systems and I can't count on the PHP people
    validating their incoming stuff properly :)

    --John
  • Matt S Trout at Jul 24, 2007 at 5:05 pm

    On Tue, Jul 24, 2007 at 02:43:12PM +0100, Ian Docherty wrote:
    Thanks for pointing out the workflow CPAN module. I am very interested
    in this for my current project and must spend some time looking at it.

    Has anyone actually used workflow within a Catalyst application and can
    they give the list their opinion and experiences with it?
    It's an awful horrible piece of shit. Class::Workflow was written by
    our very own Yuvla Kogman to replace it and works very nicely with Catalyst.

    --
    Matt S Trout Need help with your Catalyst or DBIx::Class project?
    Technical Director Want a managed development or deployment platform?
    Shadowcat Systems Ltd. Contact mst (at) shadowcatsystems.co.uk for a quote
    http://chainsawblues.vox.com/ http://www.shadowcatsystems.co.uk/
  • John Napiorkowski at Jul 24, 2007 at 7:05 pm

    --- Matt S Trout wrote:

    On Tue, Jul 24, 2007 at 02:43:12PM +0100, Ian
    Docherty wrote:
    Thanks for pointing out the workflow CPAN module.
    I am very interested
    in this for my current project and must spend some
    time looking at it.
    Has anyone actually used workflow within a
    Catalyst application and can
    they give the list their opinion and experiences
    with it?

    It's an awful horrible piece of shit.
    Class::Workflow was written by
    our very own Yuvla Kogman to replace it and works
    very nicely with Catalyst.
    Matt,

    I'd appreciate when you have a little time if you
    could share some more detailed thoughts on this, since
    I was literally just about to start typing code
    related to workflow and your message stopped me in my
    tracks.

    I originally thought to experiment with Workflow over
    Class::Workflow because it seemed to have a little
    more active development and examples as well as more
    development in the persistence area. Since I am a
    newbie to the area I am likely to need to play with it
    quite a bit before I am able to usefully contribute so
    I am looking for the most feature complete and
    supported system. However I am very interested in
    learning a system and hopefully contributing usefully
    to it, particularly as it can relate to Catalyst
    development.

    Thanks!

    --John
    --
    Matt S Trout Need help with your
    Catalyst or DBIx::Class project?
    Technical Director Want a managed development
    or deployment platform?
    Shadowcat Systems Ltd. Contact mst (at)
    shadowcatsystems.co.uk for a quote
    http://chainsawblues.vox.com/
    http://www.shadowcatsystems.co.uk/

    _______________________________________________
    List: Catalyst@lists.rawmode.org
    Listinfo:
    http://lists.rawmode.org/mailman/listinfo/catalyst
    Searchable archive:
    http://www.mail-archive.com/catalyst@lists.rawmode.org/
    Dev site: http://dev.catalyst.perl.org/


    ____________________________________________________________________________________
    Shape Yahoo! in your own image. Join our Network Research Panel today! http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
  • Ian Docherty at Jul 24, 2007 at 9:33 pm

    John Napiorkowski wrote:
    -
    ...

    It's an awful horrible piece of shit.
    Class::Workflow was written by
    our very own Yuvla Kogman to replace it and works
    very nicely with Catalyst.
    Matt,

    I'd appreciate when you have a little time if you
    could share some more detailed thoughts on this, since
    I was literally just about to start typing code
    related to workflow and your message stopped me in my
    tracks.

    I originally thought to experiment with Workflow over
    Class::Workflow because it seemed to have a little
    more active development and examples as well as more
    development in the persistence area. Since I am a
    newbie to the area I am likely to need to play with it
    quite a bit before I am able to usefully contribute so
    I am looking for the most feature complete and
    supported system. However I am very interested in
    learning a system and hopefully contributing usefully
    to it, particularly as it can relate to Catalyst
    development.

    Thanks!

    --John
    John
    I think we are both at about the same position. If you wish to share
    experiences and discuss Class::Workflow (or Workflow) then please drop
    me a private email and we can perhaps work through this together.

    Regards
    Ian
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070724/783832bc/attachment.htm
  • Ian Docherty at Jul 24, 2007 at 7:52 pm
    Matt
    Thank you for that succinct analysis. :) I will give Class::Workflow a
    good look in that case.

    Do you know how far off Persistence using DBIC is with this? I think it
    would be essential for the Catalyst application I have in mind.

    Since this seems to be straying from discussion of Catalyst, is there
    somewhere else where further discussion of Class::Workflow can be taken?

    Regards
    Ian

    Matt S Trout wrote:
    On Tue, Jul 24, 2007 at 02:43:12PM +0100, Ian Docherty wrote:

    Thanks for pointing out the workflow CPAN module. I am very interested
    in this for my current project and must spend some time looking at it.

    Has anyone actually used workflow within a Catalyst application and can
    they give the list their opinion and experiences with it?
    It's an awful horrible piece of shit. Class::Workflow was written by
    our very own Yuvla Kogman to replace it and works very nicely with Catalyst.
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070724/730af54f/attachment.htm
  • Matt S Trout at Jul 26, 2007 at 8:49 pm

    On Tue, Jul 24, 2007 at 07:52:46PM +0100, Ian Docherty wrote:
    Matt
    Thank you for that succinct analysis. :) I will give Class::Workflow a
    good look in that case.

    Do you know how far off Persistence using DBIC is with this? I think it
    would be essential for the Catalyst application I have in mind.
    "how far off" ?

    I know Yuval's implemented it a couple times; you just storable-ify the
    workflow object and drop it in a column.

    Should be a fairly trivial inflate_column call.

    --
    Matt S Trout Need help with your Catalyst or DBIx::Class project?
    Technical Director Want a managed development or deployment platform?
    Shadowcat Systems Ltd. Contact mst (at) shadowcatsystems.co.uk for a quote
    http://chainsawblues.vox.com/ http://www.shadowcatsystems.co.uk/
  • Jonasbn at Jul 29, 2007 at 2:52 pm
    Hello,

    I am the current maintainer of the Workflow module on CPAN and I just
    want to add my 2 cents here, since Matts' comments on Workflow is in
    my opinion are both unfair and inapproriate and not particularly
    constructive. I am sure he has more constructive criticism and I
    would like to discuss this with him the next time we meet, since Matt
    seems like a sensible guy.

    Workflow works quite nicely for other users in other setups, else I
    would not take the time to maintain it. If any of you guys are still
    interested in attempting to integrate Workflow with Catalyst I am
    offering assistance and I wil in any way possible attempt to support
    you and try make your attempts successful.

    We have a mailing list for the development of Workflow, it is very
    low traffic and you are welcome to post questions/comments there. I
    am a user of Catalyst myself so I know the basics of Catalyst.


    jonasbn
  • Matt S Trout at Jul 29, 2007 at 6:27 pm

    On Sun, Jul 29, 2007 at 03:52:58PM +0200, jonasbn wrote:
    Hello,

    I am the current maintainer of the Workflow module on CPAN and I just
    want to add my 2 cents here, since Matts' comments on Workflow is in
    my opinion are both unfair and inapproriate and not particularly
    constructive. I am sure he has more constructive criticism and I
    would like to discuss this with him the next time we meet, since Matt
    seems like a sensible guy.
    When I wrote Workflow off there hadn't been a release in some time and the
    author was available but disinterested so an active maintainer seemed unlikely
    to happen and we'd tried and failed to get patches merged.

    I'm glad to see that's no longer the case, but it still strikes me as
    massively heavy and clumsy for the job it does - anything where the synopsis
    has *four* XML files before it even gets to any code screams overengineering
    at me.

    And of course, Workflow::Factory is a magic global (this week under the guise
    of "singleton", but it's still a magic global) so I can't rely on its
    behaviour in any situation where I'm going to be running more than one app in
    the same interpreter.

    If I wanted to deal with overengineering -and- magic globals at the same time,
    I could learn J2EE. As it is,

    FACTORY->add_config_from_file(
    workflow => 'workflow.xml',
    action => [ 'myactions.xml', 'otheractions.xml' ],
    validator => [ 'validator.xml', 'myvalidators.xml' ],
    condition => 'condition.xml',
    persister => 'persister.xml'
    );

    does not compare well to

    my $wf = Class::Workflow->new;

    $wf->state(
    name => "new",
    transitions => [qw/accept reject/],
    );

    $wf->transition(
    name => "accept",
    to_state => "opened",
    body => sub {
    my ( $transition, $instance, $context ) = @_;
    return (
    owner => $context->user, # assign to the use who accepted it
    );
    },
    );

    to my mind. Perhaps it would be worth adding a way of setting up a workflow
    in code and documenting that first rather than the XML format? (no, being
    able to use four perl-syntax files instead of four XML files does -not-
    count from my POV :)

    --
    Matt S Trout Need help with your Catalyst or DBIx::Class project?
    Technical Director Want a managed development or deployment platform?
    Shadowcat Systems Ltd. Contact mst (at) shadowcatsystems.co.uk for a quote
    http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/
  • Aristotle Pagaltzis at Jul 29, 2007 at 10:49 pm

    * Matt S Trout [2007-07-29 19:35]:
    If I wanted to deal with overengineering -and- magic globals at
    the same time, I could learn J2EE.
    Yeah, Workflow was originally by Chris Winters, who seems to have
    cloned APIs he encountered in his Java work more than once.

    I took over Class::Observable (and recently finally started
    fixing it), and found that similarly weird in many ways
    (though it has a much simpler purpose and therefore much less
    overwhelming Java smell).

    Regards,
    --
    Aristotle Pagaltzis // <http://plasmasturm.org/>

Related Discussions