Posted on by & filed under Content - Highlights and Reviews, Web Development.

There are multiple ways to do Dependency Injection, but today we will look at three different ways and what the trade offs between them are.

Constructor injection

Constructor injection means injecting dependencies for a module through a constructor function. The main advantage of this is that you can use closures to store the reference to the dependency as a variable. The main disadvantage of this technique is that you can’t export your modules as objects and always have a layer of indirection which is the invocation of a constructor function.

Setter injection

A setter injection uses a different approach by setting the dependency on the exported module. This avoids the pain point of having to expose indirect functions:

Whenever you want to use the module you have to load it and set your dependencies on them as properties. An example of testing such a module is as follows:

Injecting dependencies as properties of the exports instead of passing them through constructors removes the issue of having to rewrite your modules as constructor functions. However, when you want to reference a dependency you cannot reference it directly as mongo but instead have to reference it as a property of your exports, i.e. store.mongo.

Require Injection

Require injection uses the require function to inject dependencies. This gets around the pain point of having to reference your dependencies as properties:

What we are doing here is injecting the database dependency through the require function. An example of doing so would be:

Here we are using a third party module to load a version of the store module, but intercept requires that db return the mongo interface.

Using require injection allows you to use your dependencies as local variables and doesn’t require you to expose your module through the indirection of a constructor function. This means your actual module code is clean and descriptive.

The main disadvantage of this style is that the actual injection of dependencies is verbose and the code is misleading if your not aware that DI is used.

For a more detailed look at these styles feel free to look at the DI example project.

Safari Books Online has the content you need

Take advantage of these node.js resources in Safari Books Online:

In object-oriented programming, a central program normally controls other objects in a module, library, or framework. With dependency injection, this pattern is inverted—a reference to a service is placed directly into the object which eases testing and modularity. Spring or Google Guice use dependency injection so you can focus on your core application and let the framework handle infrastructural concerns. Dependency Injection: Design patterns using Spring and Guice explores the DI idiom in fine detail, with numerous practical examples that show you the payoffs. You’ll apply key techniques in Spring and Guice and learn important pitfalls, corner-cases, and design patterns. Readers need a working knowledge of Java but no prior experience with DI is assumed.
Node: Up and Running (Rough Cuts) introduces you to Node, the new web development framework written in JavaScript. You’ll learn hands-on how Node makes life easier for experienced JavaScript developers: not only can you work on the front end and back end in the same language, you’ll also have more flexibility in choosing how to divide application logic between client and server. Read What Is Node? to get up to speed on Node.js with this concise overview. Node for Front-End Developers shows you how to use this popular JavaScript framework to create simple server applications, communicate with the client, build dynamic pages, work with data, and tackle other tasks. In Sams Teach Yourself node.js in 24 Hours, expert web developer George Ornbo guides readers through every step of creating custom server-side solutions with Node.js.

About this author

Jake Verbaten is a JavaScript evangelist and open source hacker. He’s published a ton of modules on npm and build multiple production applications using node.js. To learn more about Jake, check out his github profile.

Tags: Constructor Injection, Dependency-injection, Node.js, Require Inection, Setter injection,

Comments are closed.