loading...

Day 16 - #100DaysofCode - Understanding MVC

sincerelybrittany profile image Brittany Updated on ・3 min read
Table Of Contents
What is MVC?
Model
View
Controller
Advantages of MVC framework
Resources

What is MVC?

MVC is stands for Model-View-Controller and is pretty much how an application can be designed. It is not required when building an application, but is a format that you may come across while building applications. Each part interacts with each other and has a particular use.

Model

The Model stores data (usually from your database) and handles the logic. It represents data that is being transferred between controller components or any other related business logic. 

In a simple Sinatra app, this is where you will inherit ActiveRecord::Base and your activerecord associations. In addition, you would set and authenticate against a BCrypt password inside of the models class . 

View

The view is where the user can input information and is what the user can see. So usually your .erb or .html files. It is everything your user sees and interacts with when visiting your application.

Controller

The Controller is the middleman. It handles and updates both the models (database) and views(user input). It can accept user input and perform CRUD actions. What is CRUD? CRUD is Create, Read, Update, and Delete -- I have reviewed CRUD in past posts. But I feel like this code academy article puts it all together.

Do you remember RESTful routing? Well, the Controller interacts with the views to ensure restful routing is successfully used for user accessibility and our sanity.

In short, with MVC everything interconnects -- The model handles the data (usually from the database), the view is the user interface, and the controller processes all of the inputs from the database and the user interface.

What are the Advantages of MVC framework and why do we use it?

I read two articles whizsolutions and Brainvire and both indicated that these are some of the top advantages of using the MVS framework:

  1. Saves Time and effective use of resources – saving time, money, resources and managing the resources effectively.
  2. Facilitates multiple views, separates data, and makes duplication of code is certainly less.
  3. Modification does not affect the entire model
  4. SEO Friendly Platform providing ease to develop SEO friendly URL’s in order to generate more visits on a specifies pages.

Be sure to keep in mind that although MVC is a great tool and design to use, it is not the only way to build an application and not everything will necessarily fit into the MVC design. For example, while building my application I needed a different class for my API manager that was not necessarily a model, view, or controller. It is okay to create your own classes and to do what is best for your particular needs.

Resources

MVC resources
MVC Wikipedia
MVC Guru
MVC Tech Terms
MVC Tom Dalling Blog

CRUD resources
Code Academy Article
Day 9: #100DaysofCode - ActiveRecord and a Database
Day 10: #100DaysofCode - ActiveRecord and a Database

RESTful resources
RESTful routing

Song of the day:

Discussion

pic
Editor guide