All Articles

MVC Pattern

This article was done using my notes from Frederico León (2020).

Frederico León, (2020), “Introduction to MVC”, in Introduction to industry REST microservices in Golang (Go), Udemy.

MVC Pattern

Model-view-controller is a software design pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.

MVC

Controllers

Basically, a controller is an entry point to any application. Controllers are accessed through url mappings. They only validate that the incoming requests have the required parameters. As such, they do not hold any business logic. They trust services to process each new request. Finally, controllers return the response to the client without adding any additional data.

Services

They contain the entire business logic of an application. Each service is response for handling a specific and unique entity, for instance users service or products service. A service is stateless and usually a singleton. They can invoque other services, models, external providers or any other needed data source. Finally, services handle errors, send metrics, send logs, tags and any other supported meetric required by the application.

Model (Domain or Data Access Objects)

They hold the core domain of services. Any other layer exists to support and serve these domain objets. As such, they are in charge of defining the structure of domain objects. This is the only layer where we know about persistence. Only them know where and how they should be persisted. They are in charge of abstracting logic by creating a clean and general interface.