Are there advantages or disadvantages between using closures or classes to achieve Javascript dependency injection?

Asked

Viewed 69 times

2

Let’s say I have a function that depends on a logger to work. To follow the "good practice" and achieve a certain level of decoupling, I decide to make use of dependency injection so that this function does not depend on any logger concrete, only from a predefined interface.

In Javascript, there are two main "manual" forms (i.e. without depending on frameworks D.I.) for this purpose. To illustrate, consider the two code snippets below, equal in functionality:

  1. Classes

    One of the most common options for this is to use classes and inject the dependency through the constructor:

    interface ILogger {
      log(data: string): void;
    }
    
    export class DoStuffClass {
      constructor(logger: ILogger) {
        this._logger = logger;
      }
    
      doStuff() {
        // ...
        this._logger.log('Deu certo!');
      }
    }
    

    And to instantiate, in the main:

    // Implementação concreta de ILogger:
    import { Logger } from './logger.ts';
    
    // Nossa classe:
    import { DoStuffClass } from './do-stuff-class.ts';
    
    const logger = new Logger();
    
    const instance = new DoStuffClass(logger);
    instance.doStuff(); // OK. ✔︎
    
  2. Factory functions

    Another option is to use a Factory Function using the mechanism of closures javascript:

    type LogFn = (data: string) => void;
    
    interface Deps {
      log: LogFn;
    }
    
    export function DoStuff({ log }: Deps) {
      return function doStuff() {
        // ...
        log('Deu certo!');
      }
    }
    

    And for "instantiation", in the main:

    // Implementação concreta da função `LogFn`
    import { log } from './log.ts';
    
    // Nossa factory function:
    import { DoStuff } from './do-stuff.ts';
    
    const doStuff = DoStuff({ log });
    doStuff(); // OK. ✔︎
    

Note that the scope of the question is only relative to dependency injection. Initially I do not intend to maintain status, so it is indifferent to compare classes or Factory functions to that end, although it is possible with the two constructions.

Finally, I do not want opinions; if possible I would like to know the facts:

  • What are the advantages of using a classes for addiction injection? And the disadvantages?
  • And the advantages (and disadvantages) of targeting addiction injection with Factory functions?
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.