lundi 24 avril 2023

In NestJS why does the `service` layer seems entangled with the `module` instead of been agnostic?

Context:

I'm researching NestJS as our next framework to use for our greenfield projects and this will be my first time working with it. Even though NextJS is new to me I'm not new to rest API development in multiple platform, framewosk & languages and I've seen from having frameworks very strict like NestJS where you have your model-view-controller pattern to very loose frameworks that without best practices and patterns it's like the wild west. I'm strong on development patterns specially the ones that makes our developers life easier and more productive. Meaning that I rather have a good pattern in-place with a framework that works for us rather than a so called "better" framework that we have to work for the framework.

Questions:

  1. Why does the service layer which I have considered to be agnostic to the controller and the framework should be Injectable in nature and the usual pattern that I see online is having the service within the module file structure? Should it be in theory it's own thing?

  2. If that is the right way the what is the purpose of creating the service layer on top of an already layer of controller abstraction under the module?

To be clear I'm not questioning the need of the service layer but I'm questioning the entanglement of the service layer to the framework. Otherwise the nestjs service layer would be just some sort of façade to the actual service layer that contains the logic.

Your guidance will be highly appreciated it!

My understanding of the pattern:

(please correct me if I'm wrong)
Layer Description
Controller Knows about the framework (nestjs, koa, express) and transforms whatever comes from the framework into something that the service layer understands.
Service Has the actual logic which is agnostic to the framework and the controller. In theory you could extract the layer entirely out into it's separate package and/or could be used by a different controller that might using a different framework and it should work just fine not knowing the difference.

I've tried going through NestJS examples and looking and searching for nestjs best practices. I was expecting a clear consistent pattern across the board that fulfills the mode-view-controller-service pattern.

Aucun commentaire:

Enregistrer un commentaire