MVC: Where to put my codes?

Model View Controller

Over the past decade of computer science advancements, many successful architectures and design patterns surfaced and has helped many to design great software applications to solve different problem domains.

In Packfire, we follow the well known and tested Model-View-Controller (MVC) architecture. MVC enforces Separation of Concerns (SoC) which helps to break down large software application into smaller working Model, View or Controller nodes.

New solutions comes with new problems: and that I wouldn’t say is a bad thing afterall. People started to debate on where to put my codes in the MVC. Some people go for fat models and slim controllers, while others say logic is handled views and controller serves them.

Fat Models and Slim Controllers

Fat Models means that most of the application code is written in the model classes while the controllers merely call and use the appropriate models based on the request. This method will cause your models to become bloated and ending up with excessive methods and code that you probably won’t use later. You may argue that your code will be neater because you have the CRUD and higher level of CRUD methods all within the models. I beg to defer because how can code be neater if almost all your application logic are in the models?

The Packfire MVC Approach

While there are many ways to implement MVC catering to different problems, I insist on one for developers to use with application development in Packfire Framework. Let’s recap on the flow of Packfire Framework:

Packfire Framework Flow

Notice that Model almost play no part in the framework flow in an empty application? That’s simply because there is one key idea to the framework: The controller holds the application logic.

Models do what they named. They are simple classes in the application that you use to model after logical or physical objects that are of concern to the application and business logic. Using these models in your controller, you can manipulate the models to achieve what you want in your controller.

“What about CRUD in the models?” I only create what I need to in my controller. Basically I break down my application logic as much as I can into methods / actions of the controllers. This is divide et impera (“divide and conquer” in Latin). Using simple-modular controller actions, I can build application logic from these actions. So if I need to insert an object into the database, I will create a controller action for it, and let other controller actions to use it. Reusability - the basics of Object Oriented Programming.

Packfire Framework’s starter guide includes the extensive explanation of where codes should go in the MVC architecture.