Storz icon indicating copy to clipboard operation
Storz copied to clipboard

Decoupling controller

Open Onesco opened this issue 2 years ago • 4 comments

Description

Added the readFileSync method, fixed the User.findOne().select() error.

Fixes # (issue)

Type of change

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

Checklist:

  • [ ] My code follows the style guidelines of this project
  • [ ] I have performed a self-review of my own code
  • [ ] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] My changes generate no new warnings
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [ ] New and existing unit tests pass locally with my changes
  • [ ] Any dependent changes have been merged and published in downstream modules

Onesco avatar Sep 26 '22 10:09 Onesco

@Onesco why are the controllers' methods all statics methods?

ezehlivinus avatar Oct 03 '22 01:10 ezehlivinus

@ezehlivinus. The controllers method are static method so that you can call them without having anyway within your code witgout having to create an Instance of the Object. Since it was meant to replace the Original Mongoose methods there is no need getthing to instantiate the Metthod Object before calling the method so to mentain consistency across board need to use static method was intuitive to me. Although if you have any other view i am open to hear and learn to. Thanks for asking!

Onesco avatar Oct 03 '22 06:10 Onesco

@Onesco I understand. Static method are used so that one can call the method directly. This is how I do it and it is popular among express developers. Between what you used and this below I don't know which have more advantage.

class UserController {
 create () { }
}

//
module.exports = new UserController ()

So, when importing into the route files it is an instance of it that get import.

//es6 module
import userController from '../controller/user.controller.js'

// Or commonJS
const userController = '../controller/user.controller.js'

router.post('/', userController.create )

But I think we don't need to make static methods at this point, if so we could have used plain Object to define the functions.

module.exports = {
 create() { }
}

@ezehlivinus. The controllers method are static method so that you can call them without having anyway within your code witgout having to create an Instance of the Object. Since it was meant to replace the Original Mongoose methods there is no need getthing to instantiate the Metthod Object before calling the method so to mentain consistency across board need to use static method was intuitive to me. Although if you have any other view i am open to hear and learn to. Thanks for asking!

ezehlivinus avatar Oct 03 '22 07:10 ezehlivinus

@ezehlivinus you are right either ways are used...the only difference here is that we have no need for the compiler to instantiate an instance of our object. The choice of static method over regular method for the ontrollers is just to avoid creating an instance of the controller object and eliminating the likelihood of one exporting the class instead of the class object.

class UserController {
 create () { }
}

// likely omission
module.exports = UserController ()

instead of this

class UserController {
 create () { }
}

//
module.exports = new UserController ()

Onesco avatar Oct 03 '22 08:10 Onesco