Wiring Modules: Singleton Dependencies

Learn how to wire dependencies using the Singleton pattern.

We'll cover the following

Wiring modules

Every application is the result of the aggregation of several components and, as the application grows, the way we connect these components becomes a win or lose factor for the maintainability and success of the project.

When a component, A, needs component B to fulfill a given functionality, we say that “A is dependent on B” or, conversely, that “B is a dependency of A.” To appreciate this concept, let’s look at an example.

Let’s say we want to write an API for a blogging system that uses a database to store its data. We can have a generic module implementing a database connection (db.js) and a blog module that exposes the main functionality to create and retrieve blog posts from the database (blog.js).

The following figure illustrates the relationship between the database module and the blog module:

Get hands-on with 1200+ tech skills courses.