TypeScript Dependency Injection using tsyringe

Поделиться
HTML-код
  • Опубликовано: 2 мар 2021
  • Learn how to use dependency injection in a TypeScript application using tsyringe as a dependency injection container.
    We will be creating a REST API using NodeJS & Express. We will be doing this using classes in TypeScript. Each of these classes will use dependency injection so we can pass their dependencies into their constructors. We will then use tsyringe as a dependency injection container so their dependencies can be automatically injected.
    Check out the code on Github: github.com/kriscfoster/typesc...
    Don't forget to Subscribe here: / @krisfoster1
  • НаукаНаука

Комментарии • 38

  • @reggenius
    @reggenius 28 дней назад

    Honestly, this video took the core of dependency injection using tsyringe and decoupled it in approximately 7 minutes.
    Nice one Kris Foster, it's hard to find conciseness merged with in-depth these days.

  • @Apparentt
    @Apparentt 2 года назад +13

    I think the only thing that’s missing here is where you would be using interfaces rather than concrete implementations, which is where you’d likely start using a ServiceLocator; having said that I’m quite unfamiliar with this library and would be interested to see how they’ve went about usage in more complex applications

  • @gabrielangel09
    @gabrielangel09 3 года назад +10

    Fantastic short course, exactly what I was looking for! Thank you so much!

    • @KrisFoster1
      @KrisFoster1  3 года назад

      Glad you enjoyed and thank you for the kind words!

  • @alexcoroza4518
    @alexcoroza4518 Год назад +2

    I have this personal project made with NodeJS + Typescript. It is now getting bigger and is now having problems managing dependencies. I haven't thought of this problem before because I am originally an Angular dev and haven't had any problems with DI as it is automatically managed by Angular. I did a little research and found TypeDI and Inversify as the best recommendations from the community. I then read a comment in Reddit stating that there's a relatively new one which is Tsyringe and he said its his favorite(also maintained by Microsoft) so I watched it here in RUclips.
    After watching this vid, I was like "Danm is looks like Angular, its so easy". I think I don't need to look on those 2 other options(TypeDI and Inversify) as this one is already enough for my requirements and I am comfortable with the syntax it is using. Nice explanation also, thanks.

    • @KrisFoster1
      @KrisFoster1  Год назад +1

      Thank you for the feedback and good luck with your project!

  • @LivvieLynn
    @LivvieLynn 3 года назад +4

    Great video! Was looking forever for this. Everyone else's videos are way too long and bloated. This is straight to the point.

    • @KrisFoster1
      @KrisFoster1  3 года назад

      Thank you for the nice feedback!

  • @greatifeanyichukwuadams1297
    @greatifeanyichukwuadams1297 8 месяцев назад

    This was very helpful, short and straight to the point.

  • @itamarribeiro8865
    @itamarribeiro8865 Год назад

    It worked beautifully, thanks

  • @someChicoRy
    @someChicoRy 9 месяцев назад

    довольно интересно. Осталось научиться применять эти знания на практике)

  • @devcanoeofficial2021
    @devcanoeofficial2021 2 года назад

    thanks alot, this was exactly what i needed

  • @luffymonkey6967
    @luffymonkey6967 Год назад

    nice video, clean example!

  • @pranjalwalia3810
    @pranjalwalia3810 3 года назад +12

    This is really good ...I noticed that you use class based implementations in express....could you do a video on how you structure an express app in class based implementations and testing it ... obviously with typescript...cheers 🔥

    • @KrisFoster1
      @KrisFoster1  3 года назад +3

      Thanks for the feedback & suggestion @pranjal walia, I actually plan to make a couple of TypeScript videos next & that is one of them!

  • @adventurer2395
    @adventurer2395 2 года назад

    Great video, and thank you for choosing a package maintained by Microsoft! How about just having an index file for the controller where, with a function, you just manually export an instance of the controller with all its dependencies? That is, just creating your own container.

  • @JanacMeena
    @JanacMeena 3 года назад +1

    Great video. Can you do another one in TSyringe for mocks + Jest + SuperTest?

  • @thehard-coder9398
    @thehard-coder9398 Год назад

    @Kris - Thank you for creating such a great content. I;m facing issue when Im testing my dependency services with Jest unit test framework. Would you mind to share if there is any sample code or tutorial?

  • @harel2021
    @harel2021 3 года назад +1

    Nice!

    • @KrisFoster1
      @KrisFoster1  3 года назад

      Glad you enjoyed @Harel Tussi

  • @xseman
    @xseman 2 года назад +1

    Is tsyringe web compatible? I can imagine great usecase for React

  • @pPablo240
    @pPablo240 2 года назад

    Man, you are a god. That's the first time that i understand di

  • @devopmh
    @devopmh 2 года назад

    great video bro, Did u have some example unit testing an application with tsyringe?

    • @devopmh
      @devopmh 2 года назад

      I found an example in your github, but you weren't using tsyringe for dependency tree in your tests, Did u have any reason to do that? Thanks in advance for your attention

  • @_slier
    @_slier 2 года назад

    kool

  • @joshuamcdonald2533
    @joshuamcdonald2533 7 месяцев назад

    you say we are automatically injecting the dependencies, but not what that really means; like in the constructor, its specifying a type, is that just automatically picking that type as the dependency to inject too?

  • @Luke_UPPX
    @Luke_UPPX 2 месяца назад

    Can u explain how to use this DI in React Component?

  • @counterpunK
    @counterpunK 2 года назад

    This was well done, and what it covers is good, but I feel like you missed explaining one of the main usages of DI and these containers, which is using mocks and setting those in the DI framework. It also doesn't explain how to handle the situation where a dependency that we want to autoInject requires parameters in the constructor. Both are pretty common imo.

    • @mrsai4740
      @mrsai4740 2 года назад

      ? He set a service as autoinjected that had the repository as a dependency. Seems like he did show a situation where you need a depenency autoinjected that has a dependency itself in its constructor.

  • @ritesisright
    @ritesisright 3 года назад

    What if BookRepository have a dynamic property? E.g. library: 'library1'|'library2'. Can I control it from BookController?

    • @TheDesvendador
      @TheDesvendador 3 года назад

      do you mean these dependencies being injected in the BookRepository constructor?

  • @davethorn9423
    @davethorn9423 2 года назад

    don't get it you have a direct dependency in BookService for example, it's not like your're saying the injected dependency has to conform to an interface, its a class. How do I swap some other service in . What am I missing , this is obviously useful ?

  • @malthesrensen4953
    @malthesrensen4953 Год назад

    Everyone seems to tsyringe without any fear of the experimental state of the decorators. Can anyone elobare on this. Is this a non-issue or something i should be concerned about when building an application that needs to be maintained

  • @mikeverlinde7116
    @mikeverlinde7116 2 года назад

    Very helpfull video. However, your mixing up @injectable and @autoInjectable.
    Injectable should be used with "const instance = container.resolve(MyClass)"
    AutoInjectable should be used with "const instance = new MyClass()" and optional parameters

  •  Год назад

    Please place a pop filter in front of your mic, a lot of Ps and Ts are causing the audio to clip.

  • @Nonsense116
    @Nonsense116 2 года назад +1

    This is great information but I think your audio needs some tweaks. Everytime you take a breath and swallow I hear your spit. I feel like I'm watching ASMR XD

    • @CZRaS
      @CZRaS Год назад

      agreed, it makes really hard to listen to the video... put your mic farther from your mouth

  • @gandralf
    @gandralf 2 года назад

    God, how I hate javascript and all its children...