Model-View-Controller MVC program - what is it, features and description

The Model-View-Controller (MVC) program allows you to develop, implement and test each part of the program independently of any other, keeping the code organized. Saving organized code means being able to quickly find what you need to verify, quickly fix, change, and add new features. It also means more efficient code and a better way to reuse it for faster applications.

Without good reason, it’s difficult for many developers to use the new structure, technology or trend, primarily because they cannot find the motivation to study a new topic. But not with respect to MVC, whose architecture is very important, and it is necessary to apply MVC methods for the web.

Basic types of architecture functionality

Probably one of the biggest advantages is that many developers understand and use the MVC framework to build web applications. Due to this consistency, project management between several developers becomes easier. Mostly a web application or piece of software follows the MVC framework. If the structure is represented by three main types of functionality, then it is clear that this is MVC:

  1. Model code usually reflects real things. This code may contain raw data or define the main components of the application. For example, if a user created the Todo application, the model code would determine what the “task” is and what the “list” is, since these are the main components of this application.
  2. View, or view - viewing the code consists of all functions that directly interact with the user. This is the code that makes the application beautiful and otherwise determines how the user sees and interacts with it.
  3. The controller acts as a link between the model and the view, accepting user input and deciding what to do with it. It is the brain of the application and links the model and the view. The controller is considered "average." He interacts with the user, collecting data, contacts the model, receiving the necessary data, and then with the view to respond to the user.

When the user performs an action, he first goes to the controller. It will take any data, for example, $ _GET, $ _POST variables in PHP, and determine what to do with this data. In short, models relate to data processing and advanced functionality. Therefore, the task of the controller at this point is to determine which model should be called, and then open the corresponding function inside this model. After calling the function, it will find the result, usually in a variable environment.

A model is a simple idea of ​​what a user is doing in an application. The MVC model is what they should present in the code, for example, the user's books, their bank account, or something else. The model is responsible for storing functions and variables that are related to what it represents.

You can think of the logic of the model as the basic concept of object-oriented programming. Here the models are just “classes”. Not to be confused with classes in controllers that are technically structured as classes too. Finally, after the controller requests information from the model, it sends it to the view.

A view is similar to a system of application templates and may exist for a certain type of page layout, mobile view, or for a specific theme. The view will display all the markup and CSS that are traditionally used to create a static web page.

MVC is what the user sees when the controller accesses it. The controller simply redirects the user to the correct view after they receive data from the model and redirect this information to the view. Then, the view displays the information provided by it in the format in which it is structured.

Code Organization Structure

The big idea of ​​MVC is that each piece of code has its own goal, and these goals are different. Some of the codes contain application data, some make the application enjoyable, and some of them control functionality.

It is believed that this is MVC, a program that is able to organize the main functions of the code into its neatly organized boxes. The file structure for using MVC in the standard way is quite simple - there are just folders for views, models and controllers, and they are all connected to each other through one directory. Of course, with any web application, the user will also have other folders and files, such as an index file and a folder for images.

Below is a simple MVC directory structure with some sample files. Each developer can have their own names, it is important to adopt a reasonable standard agreement on them. Therefore, the files in the structure below are just examples.

Files in the structure

ASP.NET Core MVC framework is a lightweight presentation environment. It features open source. It is highly tested, optimized for use with Asp Net Core MVC.

ASP.NET Core MVC Structure

MVC Architecture

The model section determines what data the application should contain. If the state of this data changes, then the model usually notifies the view, and sometimes the controller, if other logic is required to control the updated view. For example, for a shopping list application, the model will indicate which data should contain the list items - the item, price, and others, and which list items are already present.

The view determines how the application data should be displayed. In the application of the shopping list, the view will be defined as the list presented to the user and will receive data for display from the model. The controller contains logic that updates the model in response to user input from the application.

For example, a shopping list may contain input forms and buttons that allow you to add or remove items. These actions require updating the model, so the input is sent to the controller, which then controls the model accordingly, which sends the updated data to the view. However, you can also simply refresh the view to display the data in a different format, for example, reorder items in alphabetical order or from lowest to highest prices. In this case, the controller can handle this directly without updating the model.

The analogy of the model in the modern world

MVC is a way to understand how the web works, which is very useful when planning, as it gives the user a general understanding of how his ideas should be organized into actual code. For example, a to-do list application. This application will allow users to create tasks and organize them into lists.

A model in a ToDo application can determine what a “task” is and that a “list” is a set of tasks. The View code will determine how ToDo and lists look visually. Tasks can have a large font or be of a certain color. Finally, the controller can determine how the user adds the task or notes when it is completed. The controller connects the Add button to the Model, so when the user clicks Add Task, the model adds a new task.

Just about the design pattern

Just about the design pattern

The capabilities of the MVC application can be shown in a nutshell, using the example of the address book. The model is a list of Person objects. The view is a graphical user interface window that displays a list of people. And the controller processes such actions as “Delete a person’s address”, “Add a person’s address”, “Email of a person”.

In the above example, the Person class knows nothing about the view. The viewing window informs the controller of the user's actions. Unnecessary complexity is inherent in this principle of software development. Complexity makes software expensive to maintain.

The easiest way to make code simple is to remove unnecessary dependencies. Then the code becomes less useless and easier to maintain, so it can be reused without changes.

Key element dependencies

The purpose of the controller is to remove the dependence of the view from the model. For example, the project manager requires the developer to create not only a contact window, but another one that displays all contacts only by their photos. Photos should be in the layout of the table, five pieces per line. For MVC, this task is quite simple.

There are currently three classes:

  1. Person
  2. PersonListController.
  3. PersonListView.

Two classes must be created: PersonPhotoGridView and PersonPhotoGridController. The Person class remains the same and is easily inserted into two different points of view. The developer must modify the Person class to accommodate the new PersonPhotoGridView and ultimately complicates the model (example 3).

Key element dependencies

With MVC Person, a class can be displayed by various GUI tools without any changes. They simply create the controller and view using the new tools, as with the old set of tools. The code may look like this.

MVC Person class

In setPicture, this moment of the method is basically spaghetti code. The MVC design pattern inserts a controller class between the view and the model to remove model dependencies. When removing dependencies, the model and, possibly, the view can be made reusable without changes. This makes introducing new features and maintenance easy. Users quickly get stable software, the company saves money, and developers work under normal conditions.

Principle of operation

Principle of operation

The principle of MVC is to divide the application into 3 main parts, known as Model, View (view) and Controller. Visible in the diagram are direct associations (red arrows) and inferred associations (blue arrows). Derived associations are those that may seem obvious from the point of view of the user, and not based on the actual software design.

A simple way to fulfill the condition:

  1. The user interacts with the presentation - by clicking on the link or submitting the form.
  2. The controller processes user input and passes information to the model.
  3. The model receives information and updates its state, adds data to the database, for example, calculates today's date.
  4. The view checks the status of the Model and responds accordingly, listing the recently entered data.
  5. The view awaits the next user interaction.

This is a simple concept - Business Logic - computing the logical processes of an application. For example, the business logic of a simple calendar would have to calculate what date, what day of the week, and what day of the month, if you want to represent all the days in that month. Or maintain web content using Spring MVC, which makes it possible to build an application with a static home page that accepts HTTP GET requests.

Compliance with the DRY principle

Many MVC frameworks use a template system to enforce the DRY principle, making it very convenient to reuse code without the need for rewriting. There are MVC frameworks that work on Smarty or use their own template engines. A simple warning is that some template engines have a rather complicated syntax - the programmer needs to check them before starting development.

MVC is considered to be another very good implementation of the DRY (Do not Repeat Yourself) philosophy. In essence, DRY is used by Ruby on Rails and several other implementations, and the idea is that the programmer writes something once and uses the code once. The DRY principle is defined as "each part must have a single, unambiguous, authoritative representation within the system." A correct implementation of DRY means that changing one element of the system does not change unrelated elements, which is pretty logical.

Configuration Convention

This is a design paradigm that essentially attempts to remove the number of decisions a developer needs to make. This is achieved by creating a structure with conventions that usually require all the elements. The developer only needs to change what is really needed. It is pretty simple. For example, for a form that contains elements that are always required and have the same value. The form has a tag that defines the action, method, name, id and enctype. For example, if you don’t need to change anything, it’s pretty easy to get the form name, identifier and action from the URL.

You can also set all form methods to POST, unless otherwise specified. Applying this idea to all elements makes creating this type of application very fast, simple and straightforward. MVC is a really good way to start producing clean, scalable, powerful, and fast code in less time with less effort. Some MVC structures do not contain all of these functions; most of them contain only one or two.

Advantages and disadvantages of the method

MVC design pattern is used in software development, the fundamental principle of which is based on the idea that the application logic should be separated from its presentation. Simply put, this is simply the best way to separate application logic from the display. Like any programming method, it has its advantages and disadvantages.

Benefits of MVC:

  1. Fast development process, supports fast and parallel development.
  2. With MVC, one programmer can work on a presentation, and another can work on a controller to create business logic.
  3. An application developed with its application is three times faster than an application developed with other development templates.
  4. The ability to provide several types.
  5. In MVC, you can create multiple views.
  6. Copying duplicates is very limited as it separates data and logic from the display.
  7. Support for asynchronous technology that helps developers develop a fast-loading application.
  8. Modification does not affect the entire model, because part of the model does not depend on part of the views. Therefore, any changes to the Model will not affect the entire architecture.
  9. The NET MVC template returns data without any formatting, so the same components can be used and called for use with any interface.
  10. Using this platform, it’s very easy to design SEO-optimized URLs to get more visits from a specific application.

Disadvantages of MVC:

  1. Increased difficulty.
  2. Inefficiency of data access.
  3. The difficulty of using MVC with a modern user interface.
  4. Need some programmers.
  5. Requires knowledge of several technologies. The developer knows the client side code and html code.

Create the first application

Create the first application

You can develop ASP.NET MVC examples with the appropriate version of Visual Studio and .NET using MVC v5.2, 2017 Community and the .NET 4.6 platform.

Order of execution:

  1. Open Visual Studio 2017 and further: File -> Create> Project.
  2. Expand the Visual C # and Web node on the left and then select asp net MVC in the middle.
  3. Enter the name of your project MyMVCApplication, you can specify any suitable name for your application.
  4. Set the project location by clicking "Browse" and then "OK."
  5. In the window "New web application" find asp MVC core.
  6. Change authentication by clicking the appropriate button.
  7. Click OK to have MVC create the project using the template.
  8. The first application is ready.

Start the project in debug mode F5 or Ctrl + F5 without debugging.

The MVC framework project includes JavaScript and bootstrap 3.0 CSS files by default.

Creating Responsive Web Pages

This way you can create responsive web pages. This responsive user interface will change its appearance based on the screen size of various devices.

For example, the top menu bar will be changed on mobile devices.

Thus, it is easy to create your first core MVC application using Visual Studio 2013.

MVC is the basis for programming and organizing program files. To indicate the idea of ​​how code should be organized for its function, developers will create folders for each part, which provides the starting point for translating ideas into code, and also makes it easy to return to code.

Thinking about how code interacts with other code is an important part of programming, and learning to work with other developers is an important skill. Having spent time understanding how the application fits into the MVC php framework, the programmer improves his developer skills.


All Articles