MVC Architecture and Types of  Logic

MVC Architecture and Types of Logic

Hi, I'm Hasnain. In this article, we're going to talk about the MVC Architecture pattern in Software Development. Also, we'll see what are the different types of logic, and how I implemented MVC in my project 'Natours'.

There are three popular architecture patterns in Software Development, MVC (Model-View-Controller), MVP(Model-View-Presenter) and MVVM(Model-View-ViewModel).

There are many different ways of implementing MVC architecture, but we'll do it in a very straightforward way here.

In this Architecture, the model layer is concerned with the application data and the business logic. The controller, on the other hand, interacts with the models, handles the application's request and sends back responses to the client. And all of that is called application logic.

The View layer is necessary if we have a graphical interface in our app. In my application, the View layer consists basically of the templates used to generate the view, so the website that we're going to send back to the client. and that is The Presentation Logic

Using a pattern or an architecture allows us to write a more modular application, which is way easier to scale.

Let's have a look at MVC in the context of my app and the request-response cycle.

It starts with a request. It will hit one of my routers because I had set up multiple routers, basically one for each resource.

The goal of the router is to delegate the request to the correct handler function which will be in one of the Controllers. There will be one controller for each resource to keep my app nicely separated.

Then, the controller interacts with one of my models for example to retrieve a certain document from the database or to create a new one.

After getting the data from the model the controller might be ready to send back a response to the client. now in case, we want to render a website there is one more step involved. The controller will select one of the view templates and inject the data into it.

That rendered website will be sent back as a response.

Thanks for reading! will see you at the next one!