Dependency Injection (DI) is used to invert control in portions of a program. Here I focus on the concrete use-case of provisioning a class with a logger implementation that keeps a class-instance loggingInfo
object so that any call to the logger will always include the info from loggingInfo
(e.g. an id for a request that the class instance was constructed solely to handle) in its log messages.
In my implementations, I refer to global.wi
, global.wd
and other similarly cryptically-named methods; these stand for "Winston info-level log" and "Winston debug-level log", etc. (where Winston is the logging library I normally use). At the start of the program, it is assumed that one would register an implementation to these variables – typically a call to a Winston logger, but could equally be substituted for console.log
.
I wanted to investigate two key ways of augmenting existing classes, ultimately to achieve depen