How to organize Services and Repositories in a MVC application?

Asked

Viewed 76 times

0

I have a REST API application in Nodejs already organized in MVC, but I still don’t understand how to use the services and repositories in practice. Please help me to separate the layers?

//MODEL
const db = require('../db');
exports.getProducts = function() {
    try {
        const query = 'SELECT * FROM products';
        return db.execute(query);
    } catch(error){
        return error;
    }
};
//CONTROLLER
const ProductsModel = require('../models/Products');
exports.getProducts = async (req, res, next) => {
    try {  
        const result = await ProductsModel.getProducts();
        const response = {
            length: result.length,
            products: result.map(prod => {
                return {
                    productId: prod.productId,
                    name: prod.name,
                    price: prod.price,
                    request: {
                        type: 'GET',
                        description: 'Retorna os detalhes de um produto específico',
                        url: process.env.URL_API + 'products/' + prod.productId
                    }
                }
            })
        }
        return res.status(200).send(response);
    } catch (error) {
        return res.status(500).send({ error: error });
    }
};
//ROUTE
const ProductsController = require('../controllers/product-controller');
router.get('/', ProductsController.getProducts);

1 answer

1


I believe that before going out changing all your code and the structure of your project, it is necessary to first understand what you are doing, and most importantly, why you are doing it.

The MVC standard divides your project into 3 layers, but for large applications, with many business rules and entities, this is not enough to keep the system organized, without code duplication and aligned with good practices.

With this, various design standards were created to divide responsibilities, make the system more independent and testable.

To put it in a nutshell, the responsibility of Controller is to receive an HTTP request, make the appropriate validations and treat it with a response.

Already the service is the layer where there are system business rules, and can be called and used by several parts of the application, such as a web project and an API, and both calling for the same service method.

The repository is the layer responsible for communication and operation with the database. The domain, API, service, and other parts of the system should not know how to communicate with the database, because the responsibility and abstraction of this logic lies entirely in the repository, so if the database needs to be changed, only the repository will need adaptations.

With that in mind, I recommend that you search the topics of the forum, as well as in google, in more depth explanations about each layer and the responsibility of each, because the subject goes far.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.