Angular Inject Function - New Way to Inject Services in Angular 14?

Поділитися
Вставка
  • Опубліковано 11 вер 2024

КОМЕНТАРІ • 81

  • @DecodedFrontend
    @DecodedFrontend  2 роки тому

    💥 Angular courses made by Dmytro - bit.ly/df-courses 💥
    ✂ Coupon UA-cam_DISCOUNT to get a 10%-off discount ✂

  • @WaldirNeto_inspiration
    @WaldirNeto_inspiration 2 роки тому +5

    Your channel is helping me a lot, oddly enough you are helping me to understand more English, the way you speak slowly is wonderful, and your angular content is amazing!!!
    Brazilian here

  • @arthurfedotiew3609
    @arthurfedotiew3609 2 роки тому +6

    Lots of thanks for sharing your vision, Dmytro!
    I think the most beneficial thing of this inject() is ability to use it for inheritance, since as You properly mentioned we do keep using inheritance once in a while, and I personally had nervous twitches passing deps through constructor just to satisfy the contract ;)

  • @MP3D2Y
    @MP3D2Y 2 роки тому +6

    I really appreciate the work you are doing and I improved so much since I find your channel and take your angular course. See you in the next video

  • @ytamb01
    @ytamb01 Рік тому +3

    If you need to use component inheritance for different templates, then the inject() function in the base class looks really useful. Thanks for another great video that explains the concepts as well as real-world application.

  • @drone-plus-plus
    @drone-plus-plus 2 роки тому +2

    Hello, Dmytro!
    It reminded me of when I pass an injector from a child class to a parent class, but after a short period of time I rewrote everything back. Here is a small example:
    `
    class Base {
    protected param: Param;
    protected param1: Param1;
    constructor(injector: Injector) {
    this.param = injector.get(Param);
    this.param1 = injector.get(Param1);
    }
    }
    class Child extends Base {
    constructor(injector: Injector, private childParam: ChildParam) {
    super(injector);
    }
    }
    `
    When we use the injector as a service locator, then we violate the encapsulation, we also violate the segregation of interfaces, and in general, the service locator is an anti-pattern.
    Based on that, don't you think it's pretty much the same thing? Because now it is not clear what dependencies are needed to make this class work.

  • @blokche_dev
    @blokche_dev 2 роки тому +1

    The most valuable youtube channel related to angular topics. Thanks for sharing!

  • @gund_ua
    @gund_ua 2 роки тому +10

    Hey Dmytro! Thanks for your videos as always great stuff!
    I just want to point out that while the inject function looks cool and all using it like that basically turns the Dependency Injection pattern into a Service Locator - which is considered an antipattern for mainly hiding dependencies from the consumer. It has some use-cases but in most cases you would always want to stick with proper constructor injection. And btw you can use the inject function in the constructor as a default value and this will still be a DI pattern which is nice.
    Also I can share one use-case when I use the Service Locator pattern, it was in a base component for table features that was part of public api in the library so basically I did not want users that would create features for table to depend on the implementation details of my base class so I just injected Injector and then inside the constructor was injecting everything else which is basically the same as just using inject function. So in this one case the tradeoff was worth it as now I could change my base class without introducing breaking changes to public api.
    Anyway that's a lot of text already.
    Thanks a lot for the content again!
    Cheers!

  • @paulh6933
    @paulh6933 2 роки тому +8

    It looks like inject can help a lot with component inheritance. It could be done before, but was kludgy. Is component inheritance still discouraged? if yes can you point me to something describing the issues

  • @hansschenker
    @hansschenker 2 роки тому +1

    make injectState generic: injectState(key: keyof T) to have more type safety!

  • @demidovmaxim1008
    @demidovmaxim1008 2 роки тому +2

    Thanks a lot for the new video. As always they are very helpful.

  • @PauloSantos-yu1tn
    @PauloSantos-yu1tn 2 роки тому +2

    I have a lots of super calls to carrying the dependencies... So this will be usefull.

  • @haroldpepete
    @haroldpepete Рік тому

    i see it really helpful, the code looks cleaner and you can use it a special cases, dont't be afraid i never dislike your video, i think you do a awesome job and me best way to support your job is through thumb up and never down

  • @OlehBiblyi
    @OlehBiblyi 2 роки тому +1

    Thanks for showing new features.

  • @collinsk8754
    @collinsk8754 Рік тому

    Very good tutorial. As you mentioned, a key risk with Inject() is the obfuscation of possible hierarchical dependencies.

  • @sanjayidpuganti
    @sanjayidpuganti 2 роки тому +2

    Great Video, As always.

  • @pedrinhofernandes
    @pedrinhofernandes 2 роки тому

    I can really see the benefit of this injection for example on decorators to inject services.

  • @vkiperman
    @vkiperman Рік тому

    Reduced boilerplate is a great thing!
    Great video! Thanks!

  • @HosseinSalmanian
    @HosseinSalmanian 5 місяців тому

    Many thanks for the time you take for creating precious content such this.
    I've watched it to the end.
    As you've said using inject function should have a specific reason like what you mentiond.
    But in general don't you think it's similar to 'Service Locator' which consider as an anti pattetn?

  • @jankisata483
    @jankisata483 2 роки тому

    Hello sir, than you for explaining these advanced angular topics in easy way on your channel.
    Please can you make a video about angular building process, the configuration (angular json) and what do we get after building the app

  • @AntonioSantana-ll8il
    @AntonioSantana-ll8il Рік тому +1

    I think this approach of inheritance could achieve better in angular 15, with directive composition api

  • @maks-yaremenko
    @maks-yaremenko 2 роки тому

    Your videos are great, thank you for your hard work. Good luck!

  • @sinhue
    @sinhue 2 роки тому

    Amazing job bro 😎

  • @josesaldanaperez7314
    @josesaldanaperez7314 2 роки тому

    Great function, what I did to avoid more boilerplate is to use the Injector in the constructor of the base class. But this is even better!

  • @gleisonsubzeroKZ
    @gleisonsubzeroKZ 2 роки тому +5

    Great, but how would it be a unit test for this function?

    • @zzing
      @zzing 2 роки тому

      Why would you need to unit test that?

    • @alex_chugaev
      @alex_chugaev 2 роки тому

      I assume it will be the same, using TestBed to provide mocks

    • @DjSilent
      @DjSilent Рік тому

      yeah you may have issues in pipelines if not mocked

  • @yekaterinacrawford2037
    @yekaterinacrawford2037 2 роки тому

    Surprised that you still don't have at least 100K subscribers!

  • @dahyoalli340
    @dahyoalli340 Рік тому

    thank you

  • @darwinwatterson1732
    @darwinwatterson1732 2 роки тому +2

    Can you make a video talking about Dependency Injection with Standalone components? I am a little confused with that

    • @dav1tt
      @dav1tt 2 роки тому

      I think he already talked about that, check previous videos

    • @darwinwatterson1732
      @darwinwatterson1732 2 роки тому

      @@dav1tt Hmmm, no, he talked about standalone components and lazy loading, but he didn't talked about how services and dependency Injection will work with that new approach.

  • @reeshomer516
    @reeshomer516 2 роки тому +1

    I have concern about testability. Using inject function it is harder to provide dependency mock in your tests.

  • @cyrillbrito
    @cyrillbrito 2 роки тому

    Great videos man, I always learn a lot. keep up

  • @shahardadon9409
    @shahardadon9409 2 роки тому

    Thank you for these great videos!

  • @JavierAcrich
    @JavierAcrich Рік тому +1

    If you don't inject the services in the constructor and instead use the inject() function, how can you mock the services when doing unit testing ?

    • @DjSilent
      @DjSilent Рік тому

      this was my question as well

  • @outofbox2622
    @outofbox2622 2 роки тому

    Thanks for explanation. I remember I avoided super(....) stuff by injecting Injector in AppModule and assign to some global static variable. And then access all services by GlobalInjector.injector.get(ServiceName). :)

  • @igorigor806
    @igorigor806 2 роки тому +2

    Hi, thanks for the news! Can you please describe more about “you should prefer composition over inheritance”?

    • @igorigor806
      @igorigor806 2 роки тому

      Never mind, already found tons of information about this :)

    • @mateuszhaada6837
      @mateuszhaada6837 Рік тому

      @@igorigor806 Please share some sources :D

    • @igorigor806
      @igorigor806 Рік тому

      @@mateuszhaada6837 I'd love to, but there's really nothing specific. I just read a couple of articles, watched a bunch of videos and now in my head it's like compound knowledge. Basically, you should prefer using services with "atomic" logic parts instead of extending one class. The main reason is that you can only extend one class, but you can inject as many services as you want.

  • @xingfucoder2627
    @xingfucoder2627 Рік тому

    Hi Dmytro, great video. Only one question, could be solved the concern about the injectState function with a generic version for example injectState(...) and inside the inject function this generic type? then we could delegate to the injectState function consumer the type specification.

  • @VinaySingh13
    @VinaySingh13 2 роки тому

    Was waiting for ur video.

  • @jeffjohny
    @jeffjohny Рік тому +1

    what about unit test cases?
    we can't override services if we go with injector.

    • @DecodedFrontend
      @DecodedFrontend  Рік тому +1

      You can always provide mocked services in the TestBed inside “providers” properly 🙂

  • @andrewdeiak6871
    @andrewdeiak6871 2 роки тому

    Nice features!

  • @sour4ik
    @sour4ik 2 роки тому

    Really interesting how it works in the 3rd scenario under the hood. How it understand context it is called from in case we create inject function in separate file and use it somewhere in the code.

  • @MP3D2Y
    @MP3D2Y 2 роки тому +1

    At 14:49 how works dependency injection in that scenario? Angular will start searching in the node tree injectors and then in the modules tree injectors to find the provider or maybe has another behavior?

    • @DecodedFrontend
      @DecodedFrontend  2 роки тому +1

      Hi! No, it has no impact on how DI works. It works the same as if you injected the dependency via constructor.

  • @VipinRawat_Offcial
    @VipinRawat_Offcial 2 роки тому

    You are always amazing 👌👌

  • @ammovalomro6017
    @ammovalomro6017 2 роки тому +1

    so, can we use this inject function for conditional dependency injection; and can this be useful for performance?

    • @alex_chugaev
      @alex_chugaev 2 роки тому +1

      There’s no performance impact. Conditional injection is possible but may be pointless and needed in just few cases because it only allowed in constructor. It rather adds a bit of (in)convenience for DI in inheritable components.

  • @mateuszhaada6837
    @mateuszhaada6837 Рік тому

    Hey Dmytro, title of this video is a bit misleading!
    its inject() function not Inject() ❗❗❗
    Inject() is a parameter decorator on a dependency parameter of a class constructor that specifies a custom provider of the dependency.
    inject() injects a token from the currently active injector.
    Both of them (Inject and inject) are included into '@angular/core' but the lowercase one is correct.
    I was struggling two days with this problem and wondering, why Inject(Router) doesn't return router?
    Please take care of the Title!

  • @amaizingcode
    @amaizingcode 2 роки тому

    Hi, hope you're well. I Just want to clarify my knowledge about window object in Angular, this is because I have read that is better to use it like you did in this video, as a Dependency Injection, creating an InjectableToken, but Why?
    I hope you can give me a hand with that. I really appreciate your videos, thanks!

  • @tomekczajka5165
    @tomekczajka5165 3 місяці тому

    Not a single word about testing. I think it is crucial. It is 2024 and I still prefer constructor injection

  • @MP3D2Y
    @MP3D2Y 2 роки тому +1

    At 3:16 you could use isPlatformBrowser from '@angular/common' to archive the same validation right?

    • @DecodedFrontend
      @DecodedFrontend  2 роки тому +1

      Hey! Yes, you could use something like isPlatformBrowser(platform) ? ... : ... ; and you would achieve the same result.

  • @beodan9219
    @beodan9219 Рік тому

    great

  • @musohealthdevelopers5041
    @musohealthdevelopers5041 2 роки тому

    using inject function in the base classe instead of injecting them in the constructor, is this a violation of the D solid principle ?

  • @bmiguelmf
    @bmiguelmf Рік тому

    That last part of the video concerning "Inject Functions" looks very ungly for my taste. I mean either you abstract that function into a class, which by the way defeats its purpose because would realy be a "service" or what you simply create a function outside the component class just to be cool? Maybe Im no seeing the full picture. Any way good channel mate. Keep the good work! Cheers

  • @KiknadzeTemo
    @KiknadzeTemo 2 роки тому

    What is the bad side of inject services like this way?

  • @hennadiikhudolieiev2198
    @hennadiikhudolieiev2198 2 роки тому

    Waiting for video about reactive form typification

  • @stepandemchenko9870
    @stepandemchenko9870 2 роки тому

    Nice!

  • @RobertoDuransh
    @RobertoDuransh 2 роки тому

    man you received disklike for stand-alone component. I think was great video

  • @user-tm2sy2tn6b
    @user-tm2sy2tn6b Рік тому

    You should be saying,-"Boilerplate | Boilerplate code" instead of boil print 👍

  • @FelipeNorato
    @FelipeNorato 2 роки тому

    This featua will make Angular simplier tô use, but I have some concerns about the mess that it could become...

  • @FabianVilersBe
    @FabianVilersBe 2 роки тому

    12:35 refactoring this class would break the open close principle

  • @alison.aguiar
    @alison.aguiar 2 роки тому +1

    ❤🤯

  • @THIAGO_6721
    @THIAGO_6721 Рік тому

    What about tests? With this new way my tests are failing

  • @HagbardCelineOnRust
    @HagbardCelineOnRust 2 роки тому

    "forget about it, angular is not becoming like react"

  • @gonzalocorchon6509
    @gonzalocorchon6509 2 роки тому

    I have mixed feelings about this new functionality. I understand that it can have its use in certain cases, but I don't understand why it is getting so much hype in social media. It seems to me that this is promoting some bad practices/anti-patterns. I read somewhere that this was a hidden gem in the framework, but maybe it was hidden because the Angular team didn't want us to start getting rid of the dependency injection system without a compelling reason. What about testabilty of the components using this way of injecting dependencies?

  • @Moelgaard85
    @Moelgaard85 2 роки тому

    I never really understood the inject and factory pattern 😂

  • @alex_chugaev
    @alex_chugaev 2 роки тому +1

    I feel like Angular is moving back to functional programming because OOP in this case is a verbose way of modeling app structure. It has not much use when it comes to programming logic itself.

  • @temoncher
    @temoncher 2 роки тому

    Welcome to the react hooks world, angular folks!

    • @temoncher
      @temoncher 2 роки тому

      I don't see how they solve different problems. Both are aimed at code reusability, both hide dependencies, both work only in context of React/Angular, both are used to inject services.