7

Is it a good Idea to use classes for controller and Models in Nodejs?

If so, it is better to use one Class instance for all request or use one per request, like Laravel does? Of course Node is different. I think one would be better but I am not sure.

About if using classes, performance is priority.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
Ángel Barrios
  • 107
  • 1
  • 2
  • 12
  • Laravel is a framework, node.js is not. Node.js is very agnostic about this kind of things. Have you tried to use frameworks with node.js ? – piercus Mar 25 '18 at 00:00
  • Yes, Nodejs is not a framework, but I mean the way it instances the classes on each request. By the way, I use express ;-) – Ángel Barrios Mar 25 '18 at 02:32
  • You can have your routes wrapped in a express router... Or you can define some object defining all the properties of a route and construct them dynamically.. – Agnibha Mar 25 '18 at 05:40

2 Answers2

6

Quoting this stackoverflow issue

However, it's important to understand that OOP is very effective for things that can be modeled naturally with it, and is very ineffective for other things (e.g., crosscutting concerns or aspects).

Models are naturally modeled into classes, but Controllers are not.

Controllers

If you use express, it make sense to code routes using functional-programming, and this is the middleware-chaining coding idiom.

You can check also Sails.js, (Sails.js is a MVC express framework) Controllers implementation are not classes, but Models are.

Instanciation

If you have one Class instance for multiple requests, you will have the possiblity to share the access of the instance's scope (this) between multiples requests.

It is cleaner to use one Class instance per request, and to not share memory/scope between your requests.

When you scale up your server (add multiple instances), all requests will be handled in parallel, so you should not share any data between different requests on same nodejs thread.

If you need to share any memory across your requests, it means that you need to put this memory into a separated database, on a separated server.

About if using classes, performance is priority.

This choice is more about readability on my point of view. There won't be much performance difference and you should take care of premature optimization.

If your code is readable it will be easy to rework, and easy to study performance bottlenecks.

piercus
  • 1,136
  • 9
  • 17
  • Yes, one instance per request is cleaner, i tried it. The connection to database was shared in the instance scope. I initialized models before starting server, in fact, I was using one instance for all request. – Ángel Barrios Mar 30 '18 at 00:57
2

It makes sense if you are keeping state or if your controller instantiation takes a lot of resources and you want to save time or resources (CPU or Memory or ...) by instantiating the Class once and then using it multiple times. The same is correct for services (which is assumed they are singletones). However though using classes makes it easier in either case it is doable with functions as well.

Desphilboy
  • 962
  • 13
  • 13