My views on how to properly use objects in a MVC Framework

If you are familiar with a MVC framework you can skip this paragraph. There are two components to this term “MVC framework”. First, a framework is a code-base that strongly suggests that you code using certain methodologies. Obviously, generally frameworks are designed to help the developers code faster, more object oriented, reduce code redundancy (encourage code reuse); a bunch of good stuff. There are obvious negatives to frameworks as well, which is why finding a good framework is important. Certain frameworks might be too rigid and cause very simply things to become extremely complicated. Other frameworks might be too lax such that it no longer serves a purpose of a framework. Next, MVC is an acronym for model-view-controller. It’s coding approach that separates business logic, template, and the logic which combines them into three different components. The business logic is generally in charge of generating the data the MVC application is going to employ. The view uses the data the controller passes it to generate the things you’re supposed to display on a screen. And finally, a controller is pretty much a component that mediates between the two, basically, tying in the business logic to the template in order to generate the final page. Because the code is broken down into smaller more reusable parts, and then they’re generally reused, which saves the next developer from coding the same thing from scratch.

In an MVC framework, I believe if an instance of a model is created and then passed to the view, the instance should pass from the controller to the view unmodified. One of the reasons you’d want an object passed is because objects are supposed to have a predictable behavior. I can rely on string to be accessible like an array of characters. It gets weird when I’m supposed to access a controller modified version of the string to use something that might or might not be there depending on the logic employed by the controller. So in conclusion, if you find yourself modifying an instance of the model during run-time, you’re better off not altering the instance of the model. You can simply pass the parameters directly from the controller, or define it in the view.

Leave a comment

Your email address will not be published.