Bill Moseley wrote: > My MVC efforts often fall apart in the C an M separation. My M parts end > up knowing too much about each other -- typically because of error > conditions e.g. data that's passed to an M that does not validate. And I > don't want to validate too much data in the C as the C ends up doing M's work.
I agree that this is one of the thornier problems. For simple things you can just throw exceptions, as Jesse mentioned. This is all you need to do for system-level problems like failing to connect to the database. The difficult part is when you need to provide feedback to users on their incorrect input. For example, if you have a form for registering as a user which has multiple fields, you want to be able to tell them everything that was wrong with their input (zip code invalid, phone number invalid, etc.), not just the first thing you encountered. Putting that into a model object is awkward, since coding your constructor or setter methods to keep going after they've found the first error feels wrong. You can write a special validate_input() method for it which takes all the input and checks it at once returning a list of errors. You could also just punt and push this out to the controller. (Not very "pure" but simple to implement.) Either way you can use one of the convenient form validation packages on CPAN. > Anyone have links to examples of MVC Perl code (mostly controller code) > that does a good job of M and C separation, and good ways to propagate > errors back to the C? You could look at the OpenInteract code. It includes some good examples. - Perrin