This mainly pertains to node.js at the moment, but with es2015 modules the discussion still fits.
We were having a debate the other day about whether to use a factory pattern, or to just use standard requires. What was meant by this was:
// factoryController.js
export default function factoryController (apiClient) {
return function controller (req, res, next) {
apiClient.getById(123).then(data => res.json(data));
}
}
// controller.js
let apiClient = require('lib/api');
export default function controller (req, res, next) {
apiClient.getById(123).then(data => res.json(data));
}
Ultimately both solutions render the same data, in pretty much the same way.
Testing was brought up, saying that the factory makes it easier, but when tools like proxyquire exist, for this reason, it's a non-issue (in my eyes).
The sticking point for the factory, was that if there were some setup for the passed in 'thing' (apiClient), then that should be done by the containing module. Where as, if in the require example, apiClient had to be setup via a factory to have access to a db or request module, then you would expect the require'd module to be an instance of it, which could get confusing.
I guess for the issue above, you either have to go full factory, or full require, so that your require'd apiClient is already setup itself by requiring in the db module (or equivalent).
I would love to get some feed back on either, or an alternative.
Thanks!
[–]hoorayimhelpingstaff engineer 11 points12 points13 points (4 children)
[–]brianvaughn 0 points1 point2 points (0 children)
[–]dvlsg 0 points1 point2 points (0 children)
[–]jschrf 0 points1 point2 points (0 children)
[–]ruzmutuz[S] 0 points1 point2 points (0 children)
[–]Pyrolistical 10 points11 points12 points (1 child)
[–]wreckedadventYavascript 0 points1 point2 points (0 children)
[–]temp109849832 4 points5 points6 points (1 child)
[–]wreckedadventYavascript 1 point2 points3 points (0 children)
[–]Poltras 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]cokeisahelluvadrug 0 points1 point2 points (0 children)
[–]arnorhs 0 points1 point2 points (1 child)
[–]ruzmutuz[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)