MVC Architecture – What is a Model View Controller Framework? (2024)

/ #Web Applications
MVC Architecture – What is a Model View Controller Framework? (1)
Jessica Wilkins
MVC Architecture – What is a Model View Controller Framework? (2)

Model–View–Controller is a popular software pattern used to break up the logic of your application into three different components.

In this article, I will break down the three components behind the MVC pattern, provide some history, and show you how it can be used in an application.

History of the Model–View–Controller Pattern

The MVC pattern was first introduced in 1979 by computer scientist Trygve Mikkjel Heyerdahl Reenskaug. He wanted to come up with a solution on how to break up a complex user application into smaller manageable components.

The MVC pattern was first used in the programming language Small Talk. One of the original names for the pattern was going to be Model-View-Editor but it was changed to Model-View-Controller.

Throughout the 1980's and early 90's, the MVC pattern was primarily used in desktop applications. But by the late 1990's, it became pretty popular within web applications.

In today's web applications, the MVC pattern is a popular design choice for organizing your code.

Here is a list of a few popular web frameworks that use the MVC pattern.

  • Ruby on Rails
  • ASP.NET MVC
  • Laravel
  • Angular

What are the three components behind the Model–View–Controller?

This is a basic breakdown of the MVC pattern:

  • Model – This is responsible for the data logic behind the application
  • View – This is what the user sees and interacts with in the application
  • Controller – This acts as the brains behind the application and communicates with the Model and View.

How does the MVC pattern work in a web application?

To better understand how the MVC pattern works, it would be best to show you in a demo application.

This MERN (MongoDB, Express, React, Node) stack application greets a fictional office manager and shows them a table of recently hired high school coaches.

MVC Architecture – What is a Model View Controller Framework? (3)

It also shows which coaches have not completed their TB tests, Covid vaccines, new coach application, and background checks.

MVC Architecture – What is a Model View Controller Framework? (4)

The office manager can send reminder emails to those coaches who are missing their documents.

MVC Architecture – What is a Model View Controller Framework? (5)

The Model Component

The Model is responsible for the data logic of our application. I am using MongoDB for the database of coaches.

I first had to define the properties that will be applied to each coach in the database. Each coach will have a name, email, program, application, backgroundCheck, tbTest and covidTest.

const coachSchema = new Schema({ name: { type: String, trim: true, maxLength: 32, required: true }, email: { type: String, trim: true, maxLength: 32, required: true, unique: true }, program: { type: String, trim: true, maxLength: 32, required: true }, application: { type: Boolean, required: true }, backgroundCheck: { type: Boolean, required: true }, tbTest: { type: Boolean, required: true }, covidTest: { type: Boolean, required: true }}, { timestamps: true })

type:Boolean represents a true or false value for the application, backgroundCheck, tbTest and covidTest properties.

If the coach has any of those four properties marked as false, then that means they haven't completed that part of the application process.

I created seven entries for our coach database and that information is being stored in MongoDB Atlas.

Here is an example of one of the database entries.

MVC Architecture – What is a Model View Controller Framework? (6)

The Controller component is going to communicate with the database and get the necessary information to send to the View component.

The View Component

The View component is responsible for all of the visual aspects of the application. I used React to display this data to the user.

When the application first loads, you see a welcome message displayed on the screen.

MVC Architecture – What is a Model View Controller Framework? (7)

When you click on the View Dashboard button, it takes you to the coaches table and missing documents list.

The View is not communicating directly with the database because our Controller is doing that. The Controller provides that information to the View so it can be displayed on the page.

MVC Architecture – What is a Model View Controller Framework? (8)

This is what the code looks like when the View makes a fetch call to get the data from the Controller:

await fetch('https://mvc-project-backend.herokuapp.com/coaches')

We then use the map() method to go through each coach and display their name, email address, and program in the table.

 coachData.map(data => ( <tr key={data._id}> <td>{data.name}</td> <td>{data.email}</td> <td>{data.program}</td> </tr> ))

For the missing documents section, we fetch data from the backend to get the list of coaches who are missing applications, TB tests, Covid vaccines and Background Checks.

We use the map() method again to display the names for each category.

MVC Architecture – What is a Model View Controller Framework? (9)

When the Send reminder email button is clicked, that information is sent from React to the backend. The Controller is responsible for sending the email and communicating with the View on whether or not the message went through.

Based on the information it receives from the Controller, the View will display a success message or failure message to the user.

MVC Architecture – What is a Model View Controller Framework? (10)
MVC Architecture – What is a Model View Controller Framework? (11)

The Controller Component

The Controller communicates with both the Model and View components and takes care of all the logic for our application. This section of the code was built in Node.JS and Express.

The Controller is going to get that complete list of coaches from the Model and send that information to the View.

The Controller is also responsible for filtering through the Model and providing the lists of coaches who have not completed the four missing document categories.

All of that data is sent to the View so it can be displayed to the user.

For the email functionality, the Controller is responsible for checking to make sure the sender's email is valid before sending the email.

I used Nodemailer here to send the emails.

 transporter.sendMail(mailOptions, (err) => { if (err) { console.log(`Applications: There was an error sending the message: ${err}`) res.json({ status: 'Email failure' }) } else { console.log(`Applications Success: Email was sent`) res.json({ status: "Email sent" }); } })

If the email was successful in going through, then the user is notified and the email message shows up in the demo email account.

MVC Architecture – What is a Model View Controller Framework? (12)

If there is an error in sending the message, then the Controller will send that information to the View so the error message can be displayed to the user.

Conclusion

Model–View–Controller is a popular software pattern used to break up the logic of your application into three different components.

While the MVC pattern was initially used in desktop applications, it became popular to use in web applications during the late 1990's.

The Model is responsible for the data logic behind the application.

The View is what the user sees and interacts with in the application.

The Controller acts as the brains behind the application and communicates with the Model and View.

Web frameworks that use the MVC pattern include, Ruby on Rails, ASP.NET MVC, Laravel, and Angular.

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

MVC Architecture – What is a Model View Controller Framework? (13)
Jessica Wilkins

I am a musician and a programmer.

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

MVC Architecture – What is a Model View Controller Framework? (2024)

FAQs

MVC Architecture – What is a Model View Controller Framework? ›

The Model-View-Controller (MVC) framework is an architectural pattern

architectural pattern
An architectural pattern is a general, reusable resolution to a commonly occurring problem in software architecture within a given context. The architectural patterns address various issues in software engineering, such as computer hardware performance limitations, high availability and minimization of a business risk.
https://en.wikipedia.org › wiki › Architectural_pattern
that separates an application into three main logical components Model, View, and Controller. Hence the abbreviation MVC.

What is the Model-View-Controller framework? ›

The MVC framework is a design pattern that divides an application into three interconnected components namely: model, view and controller. Component separation enables programmers to keep the codebase separate. Every MVC component deals with a particular aspect of an application.

What is the meaning of MVC in architecture? ›

In programming, model-view-controller (MVC) is an architectural design pattern that organizes an application's logic into distinct layers, each of which carries out a specific set of tasks.

What is the meaning of MVC? ›

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display.

What is the main purpose of MVC? ›

MVC is primarily used to separate an application into three main components: Model, View, and Controller. This level is considered the lowest level when compared with the View and Controller. It primarily represents the data to the user and defines the storage of all the application's data objects.

What is MVC and an example? ›

The MVC framework separates an application into three main components: models, views, and controllers. Models are used to store data and business logic; views display data from a model on the screen; and controllers manage user interactions with these two components.

What are the basics of MVC? ›

MVC is a pattern used to design user interfaces, data, and application logic to achieve separation of concerns. MVC separates applications into three groups of components: Model, View, and Controller. It helps applications to reduce their complexity and makes them more accessible in coding, debugging, and testing.

What is a controller in MVC with an example? ›

A controller determines what response to send back to a user when a user makes a browser request. A controller is just a class (for example, a Visual Basic or C# class). The sample ASP.NET MVC application includes a controller named HomeController. cs located in the Controllers folder.

What are the three components of MVC architecture? ›

MVC is an architectural pattern that is used to divide the application into three components, namely - Model, View, and Controller. Separating the application into these three components makes it easier to scale the application and makes it more extensive and easier to maintain.

Is MVC a good architecture? ›

When building web apps, is the MVC design pattern always best? MVC allows separation of views and business logic. If you web app is big enough that separation matters, then yes MVC is the answer. You can choose any framework which implements MVC and lets you build your app on top.

How to use MVC architecture? ›

How MVC Architecture works. First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data.

Is MVC good or bad? ›

The MVC pattern is an excellent method for developing software applications. MVC frameworks are simple due to the numerous benefits, including organizing code and managing multiple views.

What does MVC stand for quizlet? ›

Model, View & Controller. c. Model, ViewData & Controller.

What is a framework in MVC? ›

The Model-View-Controller (MVC) framework is an architectural/design pattern that separates an application into three main logical components Model, View, and Controller. Each architectural component is built to handle specific development aspects of an application.

What is the primary purpose of a model in MVC? ›

The model is the part of your code that handles the data and the business logic of your application. It defines the structure, rules, and operations of your data, and it communicates with databases, APIs, or other sources of information.

When should MVC be used? ›

  1. We can use MVC when our screen has a One-Direction-Flow of actions, all interactions by the user do affect the Model, but it's result does not affect the UI.
  2. We can use MVC when our screen is simple enough (similar to the no architecture example), but it need to communicate with our Model layer ...
Feb 26, 2018

Is MVC backend or frontend? ›

MVC provides front and back ends for the database, the user, and the data processing components.

Is Django an MVC framework? ›

Django appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”.

Is MVC outdated? ›

MVC is an old pattern, but it's still relevant to web apps. From a user's perspective, a software application, whether it's mobile, desktop, or web based, is something that you interact with and it does stuff.

Is React a MVC framework? ›

React takes a different approach with its component-based architecture and unidirectional data flow pattern, which offers several advantages over MVC. Component-Based: React breaks down the UI into reusable components, each responsible for its state and rendering logic.

References

Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5929

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.