An MVC is a popular software design pattern which is used to make applications by dividing the user interface(UI) and the logic of the program into different components. MVC stands for Model, View and Controller.In simple terms, it is used to divide the server code and the client code into different parts.
How I used to think an MVC structure was at first. But don't be scared its not that complex okay !
If you already using React, then you know how writing code into 'components' has made our life easier than before. An MVC architecture has an almost similar approach - meaning we can write code into 3 main components which has the following benefits:
- Easy to maintain and expand since each important parts are in separate files namely Model,View and Controller.
- Can easily be tested individually without affecting one another.
- There are already many frameworks which uses MVC frameworks to build out web apps e.g Express, Ruby on Rails etc.
- Consists of the UI(What the User actually sees and interacts with)
- Contains all the logic of how the data should structure inside a database. e.g Mongoose Schemas (if you are familiar with it) or in simple terms the structure in which data is to be stored in the Database.
- Handles all the behind the scenes activities of storing data into the Database(Inserting,deleting,updating data etc.)
- The controller is like the intermediary through which all the requests and data between the View and the Model go through. Also do notice that: the View and the Model never meet or pass data with each other,every request must pass through the controller making it such an important component in an MVC architecture.
- It basically handles all the requests like (GET,POST,PUT,DELETE etc) through the use of View.
So lets explain this concept with a help of a diagram:
So what happens here is there is a User, who wants to add a movie.He uses the View (which is the UI of the web app) to makes some changes to it - by inputting some movie details into an input field.
When the user submits the Data using a submit button in Step 1, a request( in this case its a HTTP POST request) is made to the Controller. So in simple terms we are sending the input value to the Controller.
The Controller receives this data from the View and then handles or passes the request (in our case it is a POST request) to the Database for storing - Because unless the data is stored,we wont be able to retrieve it later on.For this we need to send it to the Model.
The Model handles all the data logic for getting this data from the Controller and then storing it in the Database behind the scenes (e.g storing the data with the correct 'key:value' pair in the Database).In our case, we are inserting this movie details into our database which contains a list of movies. But its not finished yet, because the data is only inserted into our Database's movie list but the user is not made aware of this. That is why we have to send the "updated movie list" back to the user. How do we do this?
The Model sends the updated movie list back to the Controller with a message - "Hey I have successfully inserted the movies into the Database okay ? :)". But if the Model fails to insert the movie,then it sends an error message instead saying "Sorry but the movie couldn't be inserted into the Database :( ".
Now the Controller cannot 'control the happiness' (yep, pun intended!) of receiving the updated movie list from the Model and shares its happiness with the View.
In the View the movie list is finally updated and sends this results back to the browser that the user is using. Now the user can finally see the updated movie list on his screen including the new movie that he has created.
Learning the MVC pattern can be pretty useful when you want to big bigger apps that are easy to manage.If you havn't understood this concept in your first shot,no worries it takes some time.Please let me know if there are any mistakes or suggestions to be made in this article :)
Incase we are meeting for the time - Hi ! my name is Ridhik, I love building stuff up from scratch and jot down my learning into articles. Feel free to connect with me on Twitter(@fluffyRidz).