Dependency Injection | Blazor Tutorial 2

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ย. 2024
  • Become a Patreon and get source code access: / nickchapsas
    Check out my courses: dometrain.com
    Hello everybody I'm Nick and in this Blazor tutorial I will talk about Blazor's Dependency injection or DI. There are a few differences from the regulart .NET Core DI so it's worth watching and understanding even though you might be familiar with .NET Core.
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasG...
    Follow me on Twitter: bit.ly/ChapsasT...
    Connect on LinkedIn: bit.ly/ChapsasL...
    #blazor #tutorial

ความคิดเห็น • 24

  • @bujin1977
    @bujin1977 5 ปีที่แล้ว +14

    Thanks for the demo on Transient, Scoped and Singleton. That was something I was struggling to get my head around, but this has cleared it up quite nicely!

  • @carloswanderley8868
    @carloswanderley8868 4 ปีที่แล้ว +2

    Thank you for being able to explain so clearly and objectively the concept of dependency injection. 👍

  • @naveenat7259
    @naveenat7259 4 ปีที่แล้ว +2

    Really useful content. Now i clearly understand the difference between Transient, Scoped and Singelton.

  • @scotttaylor1051
    @scotttaylor1051 5 ปีที่แล้ว +2

    Great video. You showed how a page refresh affects the state for the Singleton case, but not for the Scoped case, so I'm curious how that works. Does a page refresh incur a new connection, resulting in a fresh scope context (i.e. so a new Guid is generated), or does it reuse the same connection so the scope is maintained (showing the same Guid)? And whatever the answer is, is the behavior browser-dependent or does it work the same way for all browsers?

    • @nickchapsas
      @nickchapsas  5 ปีที่แล้ว +1

      Refresh on scoped would create a new id because it is a new connection. Moving from tab to tab maintains the scope but anything that can disconnect you from signalr will create a new scope. It is not browser dependent, or at least it shouldn’t be since all browsers should behave the same way in that scenario.

  • @irlonterblanche1639
    @irlonterblanche1639 3 ปีที่แล้ว

    Excellent explanation. Thanks!

  • @Mfbzai
    @Mfbzai 4 ปีที่แล้ว

    Love your explanation

  • @rhegafhazry3545
    @rhegafhazry3545 4 ปีที่แล้ว

    Thaks and great video , keeping up on blazor im so interest

  • @Jj-bo1dm
    @Jj-bo1dm 4 ปีที่แล้ว

    Thanks, Sir.

  • @widowdev
    @widowdev 4 ปีที่แล้ว

    At 3:57 when you say you have to import it, how is that done? is that a keyboard shortcut? I can't get it to link up with that type.

    • @julianweber9658
      @julianweber9658 4 ปีที่แล้ว

      Delete the folder name after the namespace in your class, and then It'll work! haven't found any other solution than that so far...

  • @Netherlands031
    @Netherlands031 3 ปีที่แล้ว

    7:05 wait, if you inherit you can't put a constructor in the base class? Can't you just do base() in the constructor of the derived class?

  • @hozaifamohammed4894
    @hozaifamohammed4894 3 ปีที่แล้ว

    How can I reach this level your in .... your awesome 💪💪

  • @gpapadopoulos
    @gpapadopoulos ปีที่แล้ว

    Can U add Captions please? All of your other videos do. Thanx

  • @amirwork8003
    @amirwork8003 5 ปีที่แล้ว

    Thank you for this useful tutorial. So I can store the login data into singleton service, but what if I want something like session to use the timeout of it and redirect the user to the login if the site delays for a while ?

    • @nickchapsas
      @nickchapsas  5 ปีที่แล้ว +4

      That’s something that a scoped service can be good at since it is session specific.

  • @kevintupper7252
    @kevintupper7252 4 ปีที่แล้ว

    When I use the RandGuid for Transient, it actually changes twice when I refresh the page. Any ideas why?

  • @cars5088
    @cars5088 4 ปีที่แล้ว

    Hello, thank you very much for your tutorial. One question: and which is the difference between using @inject and @inherits? I tested - "@inherits Services.RandomService" is working as well.

    • @robertmrobo8954
      @robertmrobo8954 4 ปีที่แล้ว +1

      "inherit" is a c# keyword which allows us to have derived classes which inherits from the base classes (Inheritance), the @inherit you use on Blazor Components is equivalent to saying
      public class PersonBaseClass
      {
      public string Name {get; set;}
      public int Id {get; set;}
      }
      public class Programmer : PersonBaseClass // You are inheriting from the the person base class
      {
      public string FavouriteProgrammingLanguage {get; set;}
      }
      int Main()
      {
      private Programmer currentWorkingProgrammer = new Programmer();
      //from here, you can access both the "Programmer" and "PersonBaseClass" class properties from a Programmer's instance
      Console.WriteLine($"Hello {currentWorkingProgrammer.Name}, is {currentWorkingProgrammer. FavouriteProgrammingLanguage }"
      + "still your favourite programming language?");
      }
      Before the current approach (of using partial classes), the only way to have a code-behind file for our blazor components was to use the inherit keyword to inherit to classes which we have created them by inheriting from ComponentBase.
      So, the reason you app is working with both inherit and inject, is because it is being treated as a code-behind behind file (sort of), therefore the class properties are available for your component to access.
      You will run into issues (it will be impossible) if you decide to inject an interface into your component, rather than a concrete class (this is very common with DI)
      I can still say a lot, let me know if you get what im trying to pass to you or ask where you missed me... :) Happy Coding!

    • @cars5088
      @cars5088 4 ปีที่แล้ว +1

      @@robertmrobo8954 Thanks a lot for your explanation.

    • @robertmrobo8954
      @robertmrobo8954 4 ปีที่แล้ว

      @@cars5088... You are welcomed 🤝

  • @bboystunts101
    @bboystunts101 4 ปีที่แล้ว

    Can someone explain the "get" and "set" parameters?

  • @alkishadjinicolaou5831
    @alkishadjinicolaou5831 3 ปีที่แล้ว

    Έλα πατρίδα

  • @athlentral-1491
    @athlentral-1491 2 ปีที่แล้ว

    Why blazor webassembly getting more publicity than blazor web server. Is Microsoft guiding us more towards using web assembly more than blazor server. I do understand their uses and benefits but everywhere I check most of real world projects are teaching us blazor web assembly. Is there certain truth that blazor server is not getting as much interest or traction as assembly? Or what is influencing the media to promote one more than the other? Thx.