DEV Community

loading...

Discussion on: 💪 Express.js on steroids: an OOP way for organizing Node.js project [starring TypeScript]

Collapse
tperrinweembi profile image
tperrin

Hi Denis, I like very much your way to organise all your code using class. About class, I am confused about your way to use some of them and brings one question:

For example with the AuthController, when you start the server and load all controllers, you create an instance with new AuthController(), is it this same instance that will be shared by every user calling the server, or does each user run the whole code on his side and will have his own instances?

Also, the class AuthController : you create a new instance of the class UserService in each handler of the AuthController :

class AuthController extends Controller {
        ...
        async handleLogin(req, res, next){
            try  {
                    const userService = new UserService(username, password);
                    const data = await userService.login();
                ...
    }

        async handleRegister(req, res, next){
            try  {
            const userService = new UserService(username, password);
            const data = await userService.register();
        ...
    }
Enter fullscreen mode Exit fullscreen mode

Couldn't we instanciate it just once before the class declaration and them use each time the same instance in every handler:

import UserService from '../services/UserService';
const userservice = new UserService()

class AuthController {
    async handleLogin(req, res, next){
            const data = await userService.login()...}

    async handleRegister(req, res, next){
        const data = await userService.Register();}
}
Enter fullscreen mode Exit fullscreen mode

Is it a proble to make each handler share the same instance of the UserService?

Thanks a lot for sharing this awsome work, I totaly refactored my code using class after seing your work.

Collapse
thedenisnikulin profile image
Denis Author

Hi, sorry for a late reply. You are right, the code that I've published lacks efficient instance usage. You can implement dependency injection for services, that's what I did later when noticed that drawback. Controllers have only 1 instance per program so yes, they are shared.

Collapse
tperrinweembi profile image
tperrin

I even took the reasoning further: still for the service class calls in controller classes, what do you think about creating the methods as static, so we don't even need to instanciate the class, we just need to import it once and then call it straight, that would be something like that:

Static methods in service class:

class UserService {
    constructor() {}
    static login = async(credentials) => { ... } 
    static setUser = async(user) => { ... } 
    ....
}
Enter fullscreen mode Exit fullscreen mode

And then in the controler class

import UserService from '../services/UserService'

class AuthController exends Controller {
  async handleLogin(req, res, next) {
      const data = await UserService.login() ... }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
tperrinweembi profile image
tperrin

Ok thanks a lot for your reply. Since I don't get what's under the hood, I see the difference ways to do this but I don't know at all the pro & cons of each... Thanks again for your reply.