Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Help seperating business logic from controller (catalyst/dbic)

by stonecolddevin (Parson)
on Sep 09, 2008 at 04:18 UTC ( [id://709966]=note: print w/replies, xml ) Need Help??


in reply to Help seperating business logic from controller (catalyst/dbic)

That looks like a mighty fine controller method to me.

Is there something specific you're looking for help with concerning Catalyst and MVC?

meh.
  • Comment on Re: Help seperating business logic from controller (catalyst/dbic)

Replies are listed 'Best First'.
Re^2: Help seperating business logic from controller (catalyst/dbic)
by uG (Scribe) on Sep 09, 2008 at 04:41 UTC
    <mst> ...everything that's business logic rather than UI flow logic should live in the model. Write methods in your DBIC ->table classes. Use load_namespaces and write methods in your resultset classes. If there's an extra layer of logic that needs to be in front, then -that- should be a model that's exposed to your app. This way everything can be re-used in batch jobs, cron scripts, command line tools, etc...

    It was this tidbit that triggered my post. But perhaps i'm just confused with the difference between business and ui flow logic.

      There are many ways to interpret MVC in a web context. One of them is to say that all of the actual form processing should be handled in some kind of "action" classes, separate from the controller, which you would then say are a part of the model. (Does it make sense to put these in DBIC table classes? Only if the action is reasonably a method of the object type that class is representing. Otherwise, a separate action class makes more sense.)

      Another way to think about it is to say that the "controller" classes in a web framework are already abstract from the web itself and call Catalyst itself the controller and your controller class part of the model. This is a practical point of view and by far the most popular way to do things. However, things like the redirect in your example are obviously web-specific.

      The point of all this is separation of concerns and code reuse. Ask yourself, is there any business logic in here that I might want to use from a non-web context, like a cron job? If I did want to, could I do it?

      In the example you show here, it fails that test. You have rules expressed here (e.g. you can't create a nation with a name that is already taken) which can't be reused outside of this web context. To solve this, you could create a method (maybe in DB::Nation, maybe in Action::NationManager, etc.) which takes the relevant parameters (name, user, etc.) and either returns a new nation or throws an exception. Your controller code calls it and handles the exception by redirecting.

      Sound like a lot of work? It can be. It's definitely worth doing in situations where the code would genuinely be reused, but debatable otherwise. It's up to you to find the sweet spot on the purity <--> practicality continuum for your application.

        This is what I was getting at. Originally I had taken the rule part and did just what you had said, put it in DB::Nation and set it up to handle exceptions. My problem at that point was I wasn't quite sure how to call a method from a Schema file (or to put it in the same namespace). I'll admit i'm pretty lost, but I can't find a good example on how to do this :(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://709966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found