Giter VIP home page Giter VIP logo

dependency-injection-typescript's Introduction

About this repo

This is a repo with the purpose of teaching myself the ideas and hands on examples of Dependency Injection with typescript .

I have no copyrights to this code feel free to thank Kevin.

About Depdency injection

Dependency injection is a powerfull parttern to organize our code structure.

This is a repo for my future self to always remember the usage and maybe help some lost souls in the subject during the learning process.

This repo follows the code, examples and explanations from the article: dependency Injection in typescript

All my explanations here are my understandings of what the article attempts to teach.

A typical class structure without DI

class Foo {}

class Bar {
  foo: Foo;

  constructor() {
    this.foo = new Foo();
  }
}

class Foobar {
  foo: Foo;
  bar: Bar;
  constructor() {
    this.foo = new Foo();
    this.bar = new Bar();
  }
}

The code above is just an example that repeats a lot in code without dependency injection. but what is repeating?
What is repeating here is the instantiation and the parameters of classes that depends on a specific constructor.

Each time the use needs a class that depends from another class it will need to write the code to instantiate it, again and again and again....

This doesn't sound very DRY code...

Solution with Dependency injection

Using Dependency injection this problems disappears giving us a much more testable, reliable code.

First example

class Foo {}

class Bar {
  constructor(foo: Foo) {}
}

class Foobar {
  constructor(foo: Foo, bar: Bar) {}
}

This code already takes away the dependency problem but instantiation code doesn't look much better...

const foobar = new Foobar(new Foo(), new Bar(new Foo()));

This is where the injector comes in:

const foobar = Injector.resolve<Foobar>(Foobar); // returns an instance of Foobar, with all injected dependencies

Already looks cooler and more promising. But:

  • Where does this Injector class comes from?
  • How does this Injector returns an instance of a giving class?
  • Will it work for multiple classes?

Dependency Injection with Typescript

dependency-injection-typescript's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.