dependency inversion in large-scale typescript applications with inversifyjs

34
Dependency Inversion in large-scale TypeScript applications with InversifyJS By Remo H. Jansen

Upload: remo-jansen

Post on 16-Apr-2017

866 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Dependency Inversion in large-scale TypeScript applications with InversifyJSBy Remo H. Jansen

Page 2: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Coupling between modules occurs when one module directly references another module. In other words, one module “knows” about another module.

The problem: Coupling

Page 4: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Abstractions

Something considered as a general quality or characteristic, apart from concrete realities, specific objects, or actual instances.

Page 5: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Concretions

A concretion is something that relates to or involves specific people, things, or actions rather than general ideas or qualities.

Page 6: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Dependency Injection

Page 7: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Dependency Inversion

Page 8: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Composition root

“A (preferably) unique location in an application where modules are composed together.”

Page 9: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Inversion of Control (IoC) and IoC containers

● Inversion of control is used to increase modularity of the program and make it extensible and flexible.

● The question is: "what aspect of control are we inverting?" The IoC container takes the control over the object composition.

Page 10: Dependency Inversion in large-scale TypeScript applications with InversifyJS

InversifyJSA powerful lightweight inversion

of control container for JavaScript

apps powered by TypeScript

Page 11: Dependency Inversion in large-scale TypeScript applications with InversifyJS

inversify.config.ts

The modules are unaware of each other but the IoC container is aware of all the modules in the application.

Coupling takes place in one unique place.

Page 12: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Declaring kernel modules

Kernel modules are used when your inversify.config.ts file becomes too large.

Page 13: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Declaring kernel middlewareThe middleware is an extension point for InversifyJS. It allows developers to create plugins for InverifyJS.

The internal InversifyJS process:

1. Annotation phase2. Planning phase3. Middleware (optional)4. Resolution phase5. Activation handlers (optional)

Page 14: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Using concretions: Classes

Page 15: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Using abstractions: String literals

● We use string literals to represent interfaces at runtime● String literals can lead to naming collisions be careful!

Page 16: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Using abstractions: Symbols

● Symbols helps us to avoid naming collisions● Symbols might be generated by the TypeScript compiler in the future.

This would eliminate the need for manual annotations when using interfaces!

Page 17: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Controlling the scope of the dependencies

The IoC container takes the control over the lifecycle of the dependencies.

Page 18: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Let’s take a look to some of the InversifyJS features!

Page 19: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Injecting a value

Page 20: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Injecting a class constructor

Page 21: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Injecting a Factory

Page 22: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Auto-factory

Page 23: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Injecting a Provider (async Factory)

Page 24: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Activation handlers

Objects become unaware of cross-cutting concerns and the IoC container takes the control over them!

Page 25: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Multi-injection

Page 26: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Contextual bindings: Tagged bindings

@named(“name”) is an alias of @tagged(“named”, “name”)

Page 27: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Contextual bindings: Named bindings

Page 28: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Create your own decorators

Useful when you are going to apply a @tagged decorator to many entities.

Page 29: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Contextual bindings: @paramName

Only needed if you are planning to compress your application.

Page 30: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Out of the box contextual constraints

InversifyJS includes out of the box implementations of the most common contextual constraints.

Page 31: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Custom contextual constraints

InversifyJS includes some helpers to help you to declare contextual constraints.

Page 32: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Thanks!

$ npm install [email protected] --save

Beta available now!

Page 33: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Questions?

Page 34: Dependency Inversion in large-scale TypeScript applications with InversifyJS

Thanks!Want to learn more?● http://inversify.io/● @InversifyJS ● https://github.com/inversify/InversifyJS