/dependency-injection-typescript-example

A small example of dependency injection using Typescript

Primary LanguageTypeScriptMIT LicenseMIT

Dependency Injection in Typescript

Dependency inversion is a complicated topic. This repo explores the most common, simple issue as it relates to software development.

Build

yarn
yarn build

No dependency injection

In src/noDI.

yarn start:noDI

This is our starting point. Just a couple of classes with dependencies created in the constructors.

Mutator dependency injection

In src/mutatorDI.

yarn start:mutatorDI

DI enabled only by calling setters.

Constructor dependency injection

In src/constructorDI.

yarn start:constructorDI

DI enabled by passing parameters to constructors.

Simple dependency injection driven by an inversion of control container

In src/simpleDI.

yarn start:simpleDI

This moves the dependency setup to another module to encapsulate the inversion of control logic. This technique is often referred to as a "container", though that term most often applies to larger DI frameworks.

Dependency injection using the Inversify framework

In src/InversifyDI.

yarn start:InversifyDI

Demonstrates a naïve use of Inversify for dependency injection. This appears to be one of the more popular frameworks, but hasn't seen much recent activity.