DEV Community

Bruno Santos
Bruno Santos

Posted on

The awesome layered architecture

Introduction

Hey Friends! Today i going to talk about a thing i'm learning in this latest times in a project that i'm working, that's the software architecture, it's funny because we don't care so much about this thing in the beginning and in fact it's useless when we are working with a simple personal projects, because we don't write so much code for lose ourselfs reading some files yet, but when us started to come into in a real project that has a bunch of funcionalities and code we finnally get it the meaning of all this... If the team doens't have in mind the architecture of the project, it really can to cause "headaches" in the future.

In that project that i recently started to work, we are going through this, we are working with Laravel in a big project, for standard everyone knows that technology works with MVC right?
A quick explans about what's MVC:

Mvc

The "Problem" with MVC

M- means model that's for working with the database, here we create tables, write and validate any data in database.
V- means view your responsibily is show the user the informations in the application and allow he to interact with our app,
C - means controller that's responsible for receive the requests, doing the validations, operating the bussiness rules, controling the model, and sending a responsive to view.

MVC is already really good for organize your code right? But in this explanantion about this architeture, we can realize there's a side is still with a lot of work, and yes the controller is the problem here, because it's with a bunch of responsabilities, because he's the "center" of everything here, and this results in giants controllers that how more we have functions more unreadble the code there becomes, for example in the project that i'm working we have (believe in me i'm not kidding!) controllers with more of 1000 LINES!! This is crazy right? So how we can solve this problem? It's in this scenario that was create the layered architecture, the solution for the controller's problem.

What's Layered Architecture?

I think the layered architecture like a evolution of MVC because it simplely takes the controller that's the problem in this architecture and split it in more two layers that are called repository and service, in other words we had only 1 layer for this responsability and now we have three layers!

Image description

Repository: it's responsible for managing the model in database search, delete and store data.

Service: it's responsible for doing the business logic, all the validations and manipulations with the data from repository stay here.

Controller: in this pattern the controller is only responsible for receiving the request and sending a response for the view.

Using layered architure means less code in the controller and now the controller don't need to know the database and manipulate the data, it's easier for testing the backend beacause if you are receiving a bad response from controller you don't have to search in 1000 lines of code to discover where the problem is. If the problem is in the request you can find the error in the controller, if the problem is in the validation search in the service, if the server isn't saving in the database probably search for the bug in repository is the best solution, briefly you going to have less headache for solve your bugs, although it's much easier to updating your backend because you won't have repetitive code for save in a table from your database in every function that you need it in controller for example, instead that you simple can call a function in repository, here goes a quick use case:

Puting dogs in the database

In this example we going to store dogs in our database for example, first the user send a request to store a dog called Rex, first we receive the data by rquest in our controller's function, the controller takes thia request, we take the $request->name and put it in the parameter for the function dogsService->store() here we are calling the service in the controller, where we receiving thos parameter and doing the validation, we verify if already exists the same dog in the database, if the name is correct, etc.. and if everything is fine we pass this data to the function dogsRepositoritory>store(), where we going to call the table dogs in database and use the ORM to store the data there.

Conclusion

In this articles we see the importance of a good architecture like the layered architecture, it's really important for improve the aplicattion's performance, i hope you enjoy the article.

Top comments (0)