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.
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?
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!
| 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.
I think controller, service layer have the following responsibilities:
This concept is used not only for NestJS, and also used in Spring (Java framework). So, the service layer concept is not only own of NestJS.
If you want not to use the service layer for any reasons, that might be ok. But, then your controller codes will be complicated more and more, and that might lower the maintenance of your code. So, if you want to write concrete code, I recommend to segregate the controller layer and service layer.
NestJS manage the object with using module system. and the module system packages the relative codes and manage the singleton-like objects. Module decorator @Module has property provides. Provide property means, Objects in the provides property are managed by NestJS Module and Inject that objects to the other objects within the module. So Injectable means this object can be injected to the other object by dependency injection (DI). If you want to study more about it, search Inversion of Control.
The purpose of the service layer is only for decoupling the business logics from the controller layer. This enhances the maintenance of your codes. (Imagine that all of the business logic are in controller layer. then number of lines of only one method block can have high possibility to be over 200 lines)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With