jeudi 19 juillet 2018

OOP: Reusable classes with dependency injection

I have been struggling to understand how/where to initialize a class when using a dependency injection container. I am having trouble find the words to explain so i will give an example.

I am building a queue consumer that retrieves data from some message system and passes that message to a handler function for processing. I also have a configuration object that is loaded with things like queue names and idle timeouts.

I started with a constructor that looked something like this:

constructor ( @inject("config") config: IConfig ) {
    this.queueName = config.getQueueName();
    this.prefetch = config.getPrefetch();
    this.idleTimeout = config.idleTimeout();
}

I don't like this. I know it is not correct. The class is not reusable, at least, not easily. This leads me to something like this:

constructor ( 
    private queueName: string, 
    private prefetch: number, 
    private idleTimeout: number 
) { }

I feel much more comfortable with this, i can reuse the class and it is much more flexible. But now i cannot inject it directly. This seems to be the case every time I attempt to directly inject a class (by directly, I mean without a factory or a provider or something)

At this point i find myself with 2 options.

  1. Make a factory to create instances for me.
  2. Initialize the objects ahead of time and bind them as constant values.

Constant values seems very wrong as injecting anything into the consumer is more difficult (like a logger) whereas factories seem to obfuscate the class and can make the scoping unclear (transient/singleton).

My questions:

  • Is there a most correct way to handle this? What does that look like?
  • What are good reading materials on this topic?
  • Am I just over complicating it?

Thank you for your input!

Aucun commentaire:

Enregistrer un commentaire