How Dependency Injection Works in

Sdílet
Vložit
  • čas přidán 25. 06. 2024
  • In this dependency injection c# tutorial I answer the questions: what is dependency injection? how does dependency injection work? how do dependency injection lifetimes work? We implement our own dependency injection container and a resolver (which really could just be one object) with singleton and transient lifetimes for our dependencies. Primarily we are trying to understand that we want to know about all possible dependencies, so when we try to use a service we can assemble it automatically, and all services come from one source.
    Patreon 🤝 / raw_coding
    Courses 📚 learning.raw-coding.dev
    Shop 🛒 shop.raw-coding.dev
    Discord 💬 / discord
    Twitter 📣 / anton_t0shik
    Twitch 🎥 / raw_coding
    👉 Try Rider
    www.jetbrains.com/store/redeem/
    RD5K9-4TXXW-KMV3G-NYWSF-3ZSTP
    LINQPad: www.linqpad.net/
    Source: github.com/T0shik/raw-coding-...
    #csharp #dotnet

Komentáře • 142

  • @kentswan3230
    @kentswan3230 Před 6 měsíci +3

    Even though this is 3 years old the knowledge being explained is as current as yesterday. I'm going to need to watch this whole thing a couple of times because there's a ton of useful intrinsic understanding being rapidly but coherently presented that makes the other DI frameworks more understandable. A Belated THANKS!

  • @jankohler7560
    @jankohler7560 Před 4 lety +12

    Great! This is what i was looking for. Now it gets so much clearer.

  • @adistutorials5399
    @adistutorials5399 Před 4 lety +3

    Thanks for this man. You create really good contents, channel like this should go big for real!

  • @phaswanamalatjie5790
    @phaswanamalatjie5790 Před rokem +1

    I know this video is old but it was a nice refresher. I enjoyed it, thank you.

  • @aliahmadi3902
    @aliahmadi3902 Před rokem

    Thank you, Anton, you really opened my eyes to this concept. I just used it without understanding it.

  • @hadibazmi6340
    @hadibazmi6340 Před 4 lety +3

    Great work as always, tnx a lot
    i literally watched more than 5 hour worth of time videos about DI so i can grasp the full picture
    your video connected all the dots that i had.
    i'm looking forward for your MVC content that you mentioned at the end of this video.

    • @RawCoding
      @RawCoding  Před 4 lety

      Glad I could help and I think that video will come in about a week or 2

  • @dendeline7816
    @dendeline7816 Před 4 lety +6

    Thank you so much! I'm not good at English, but thanks to easy interpretation, lots of people like me will be interested in your content.

  • @littlemancrs
    @littlemancrs Před 4 lety +15

    Awesome stuff!
    I've started out in the IT world, less than a year ago, with videos from KudVenKat, for real beginners.
    But yours, are on a whole different level. I apreciate them very much.
    Also, I never thought that twich is used for anything other than games. :D
    GG!

    • @RawCoding
      @RawCoding  Před 4 lety +1

      Eyyy thanks man, and yeah I stream working on personal projects, where I can answer questions and talk with viewers, come join i post updates on discord)

  • @balramverma4873
    @balramverma4873 Před 3 lety +2

    This is insanely great stuff :D Thank you Anton .

  • @ivanvincent7534
    @ivanvincent7534 Před 3 lety +4

    Really impressive. Congratulations on your skill set, this is the ultimate under the hood tutorial.

  • @-xeL
    @-xeL Před 3 lety +1

    love the deep dives, nice work!

  • @9415745
    @9415745 Před 3 lety +1

    these C# videos are fantastic, thanks!!

  • @vladimirmelnikov849
    @vladimirmelnikov849 Před 2 lety +1

    Amazing, this is so clear now. Thank you!

  • @jxxqll
    @jxxqll Před 4 lety +2

    Learned a lot! Thank you so much!

  • @septinary
    @septinary Před 4 lety +3

    This video helped me a lot. Thank you.

  • @coreyb1102
    @coreyb1102 Před 8 měsíci +1

    A month into my c# and asp net and you have the best tutorials haha down.

  • @vasugupta1
    @vasugupta1 Před 3 lety +1

    Great work, cheers for the clean explanation

  • @nikolazagorac8634
    @nikolazagorac8634 Před rokem +1

    This is the best explanation I have seen yet

  • @PrasadJachak
    @PrasadJachak Před 3 lety +1

    Thank you for this Video.. Middleware looked complex for me before this video. Now I understood how to works...

  • @RamcoBros
    @RamcoBros Před rokem

    Amazing Video, Nice explanation bro. Thank you for making the concepts clear. I
    This contents helps for the people who are new and starting to learn the programming and concepts associated with it.

  • @achilleskocaeli
    @achilleskocaeli Před 3 lety +1

    Thanks Anton. You are my new hero..

    • @RawCoding
      @RawCoding  Před 3 lety

      Noooo, thank you for watching!

  • @The69pawlak
    @The69pawlak Před 3 lety +1

    Very useful stuff, thanks for that!

  • @azeddine8539
    @azeddine8539 Před 4 lety +9

    Super, Thanks. I Will be very happy if you can explain how MVC DI works.

  • @eclipsetutorialvideo
    @eclipsetutorialvideo Před 4 měsíci

    Impressive you did it without edit it seems :) You did Singleton and Transient, it would be good to know how Scoped is working as lifetime

  • @hirenpatel2006
    @hirenpatel2006 Před 3 lety +1

    Great video and very easy to follow

  • @krystian9924
    @krystian9924 Před rokem +1

    This tutorials are the real deal truly..like your making tutorials and explaining things in a way I would explain them to someone if I knew these topics haha
    Your explonations truly give you insight into topics, not just another abstractions to get by
    I think that this is what happens when you are truly passionate or have read some masterpieces (SICP shhh) 😉

    • @RawCoding
      @RawCoding  Před rokem +1

      thank you for the kind words.
      I attest my knowledge to Clojure

  • @pintileiciprian
    @pintileiciprian Před 3 lety +1

    Yet another great video from Anton. Well done!
    P. S. What keyboard do you use? Sounds nice.

    • @RawCoding
      @RawCoding  Před 3 lety +1

      It’s filco convertible with mx brown switches

  • @yazanshakhshir3049
    @yazanshakhshir3049 Před 2 lety +1

    You are a great person
    Thank you for the great explanation

  • @TheEamonKeane
    @TheEamonKeane Před rokem

    This is very cool! Advanced stuff is difficult to find on CZcams

  • @abuzeromohammed3386
    @abuzeromohammed3386 Před 4 lety +2

    Thank you for these special video s

  • @anatoliytkachenko7081
    @anatoliytkachenko7081 Před 2 lety +1

    Anton, thank you a lot for explanation! Like!

  • @mohammadjouhari4607
    @mohammadjouhari4607 Před 5 měsíci

    great stuffs. keeps going.

  • @georgedraghiceanu1526
    @georgedraghiceanu1526 Před 2 lety +1

    Great video!

  • @nouchance
    @nouchance Před rokem

    Thank you Sir!

  • @ArjunKRaj-vt4vw
    @ArjunKRaj-vt4vw Před 3 lety +1

    Excellent video

  • @youngliao5772
    @youngliao5772 Před 4 lety +2

    Cool guy, really appreciate your tutorial video

  • @user-ff5qh7ol7c
    @user-ff5qh7ol7c Před 4 lety +2

    Thank you a lot!:)

    • @RawCoding
      @RawCoding  Před 4 lety

      Thanks for watching as always :)

  • @ksichtmen
    @ksichtmen Před 3 lety +1

    Awesome video! I would love to see the MVC explanation video. Maybe its already out (gonna check now) ~

  • @sachinsaini8595
    @sachinsaini8595 Před 2 lety +1

    Nice video....very well explained...

  • @im1in260m
    @im1in260m Před 2 lety

    An example of applying this to WPF would be excellent.

  • @olegchuryukin158
    @olegchuryukin158 Před 3 lety +1

    Thank you for excellent video

  • @parsalotfy
    @parsalotfy Před 3 lety +1

    thank you for this video

  • @aloksaxena7905
    @aloksaxena7905 Před 4 lety +1

    It's good video, do not mind to say its very deep thought

  • @kannanp4811
    @kannanp4811 Před 3 lety +1

    Thank you so much

  • @aslam5915
    @aslam5915 Před 3 lety +1

    Great!Thanks a lot, I really needed this video. Could you plz make a video with recursion?

    • @RawCoding
      @RawCoding  Před 3 lety

      Cheers, what question do you have about recursion?

  • @denisosipenko7413
    @denisosipenko7413 Před 3 lety +1

    Thanks a lot!
    You are realy cool! 👍👍👍👍👍

  • @Shinee3e
    @Shinee3e Před 4 lety +1

    Thanks dude very clean and straightforward (implemenentatation lol) i would like you to make MVC video for sure thanks again;

    • @RawCoding
      @RawCoding  Před 4 lety +1

      Thanks and actually did make it check the playlist:)

    • @Shinee3e
      @Shinee3e Před 4 lety +1

      @@RawCoding Oh sorry didn't notice, will check it soon.

  • @needpiebad
    @needpiebad Před 4 lety +2

    Thanks for the video really helpful. Hey, just wondering if it'd make any sense to do this for MVVM in terms of injecting the services into view models. I get that there's MVVM frameworks out there that handle this sort of things just wondering if this is roughly the way they'd handle it?

    • @RawCoding
      @RawCoding  Před 4 lety +2

      Cheers, MVVM is MVC but you have the additional VM (view model) basically mapping between your data and what you want to display. There should be no need to do dependency injection when you do the mapping. View Model is a dumb object, just data representation, same as the Model. Only the controller has functions and has services injected to do stuff to the models.

  • @alexspeleers
    @alexspeleers Před 3 lety +1

    Thnx for good explanation of creation of custom DI container. Can you use classic VS for coding - easier to migrate via code due to familiar interface?

    • @RawCoding
      @RawCoding  Před 3 lety

      Cheers, I think LinqPad is easier to explain things due to it’s visualisation features.

  • @algarud
    @algarud Před 3 lety +1

    Man, you're so cool!

  • @ultramasculine
    @ultramasculine Před 2 lety +1

    I love u dude

  • @fletcherdukakis934
    @fletcherdukakis934 Před 3 lety +1

    First off, awesome video!
    Couple questions. Can you briefly describe (or point me to some resources) describing why we needed to pass the Activator.CreateInstance as a delegate to the CreateImplementation method.
    Also, I understand that this isn’t production ready code, but with the Transient dependencies, wouldn’t the reference to the instance exist for the lifetime of the container. If an application creates multiple instances, they’d never actually get released /garbage collected. Am I understanding that correctly?

    • @RawCoding
      @RawCoding  Před 3 lety

      So I’ve just used an abstract factory pattern there without an interface and the factory is just a function (well in c# delegates are object but anyway) you want to look for abstract factory pattern.
      As I understand it the application has 1 DI container so as soon as the service that consumes a transient service is disposed of the transient service is disposed as well. Otherwise we would run out of memory eventually.

  • @wisnu7734
    @wisnu7734 Před 3 lety +1

    i love your all videos....just to advice for you..please add graphics or animation while you explain something such analogy....

  • @mrreadit1767
    @mrreadit1767 Před rokem +1

    Hi @Raw Coding, thanks for the video.
    1. Could you please elaborate on the scenario when the constructors of the services require some kind of data (authentication, etc.). So how would the static class Activator handle that while resolving? How is that handled in real world applications?
    2. A mention about other types of DI would have been helpful (not just using constructors) - Being a bit exhaustive about the possibilities/ variants.

    • @mrreadit1767
      @mrreadit1767 Před rokem

      1. Just had a look at the MVC video in one of your playlists. I think the reflected data can be parsed after a check (binding?) (using System.Type class)

    • @RawCoding
      @RawCoding  Před rokem

      If the service requires authentication, that service would have the httpcontext injected in to it, or some kind other service that loads authentication from the httpcontext and persists it through the scope.
      On point 2 - you can use reflection to assign things to fields or properties or in to functions.

  • @sitalsitoula6536
    @sitalsitoula6536 Před rokem +1

    I am trying to wrap my head around the GetService method. Please tell me if my summary is right.
    Resolves the service. Using recursion. We want to resolve the service until it has no parameters.
    It essentially uses recursion to create necessary services starting from the ones that
    don't require parameters and use that to create another service in the chain until required service is generated.

  • @aaaaasssss884
    @aaaaasssss884 Před 2 lety +1

    Good stuff!!

  • @lucianbumb6690
    @lucianbumb6690 Před 3 lety +1

    yes for MVC
    what is the performance impact of reflection?

    • @RawCoding
      @RawCoding  Před 3 lety +1

      Ehmmm, I can’t say, depends what parts of the api you are using

  • @cdeba9867
    @cdeba9867 Před 3 lety +1

    when do u stream?

    • @RawCoding
      @RawCoding  Před 3 lety

      Wed & Sun on twitch, you can ask for stream squad role on discord to get notified

  • @emmepombar3328
    @emmepombar3328 Před 2 lety +1

    41:32 Awesome, but I see one flaw, if I am correct. If I try to create an implementation of an class Foo as singleton with a ctor Foo(Bar b), then every time, Bar b is resolved.

  • @brahmcdude685
    @brahmcdude685 Před 3 lety +1

    why no interfaces? what is the use of a type without a base interface?

  • @parsalotfy
    @parsalotfy Před 3 lety +1

    very rich content

  • @thesuperiorman8342
    @thesuperiorman8342 Před 4 lety +6

    Very strange. I've never come across the method Dump()

    • @RawCoding
      @RawCoding  Před 4 lety +7

      It's only available in LINQPad

  • @TheDogfighter13
    @TheDogfighter13 Před 2 měsíci

    anybody knows how to add scoped to this , i tried but it's not working right

  • @grafterallday3763
    @grafterallday3763 Před 2 lety +1

    Are you Scandinavian ?, I could be totally wrong but I'm sensing you are one.

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 Před 2 lety +1

    What if we want to inject interfaces and then resolve them to a concrete implementation

    • @RawCoding
      @RawCoding  Před 2 lety +1

      Just cast the object

    • @abdelrhmanahmed1378
      @abdelrhmanahmed1378 Před 2 lety +1

      Can you elaborate more please

    • @RawCoding
      @RawCoding  Před 2 lety +1

      var myImp = (Implementation) myInterface

    • @abdelrhmanahmed1378
      @abdelrhmanahmed1378 Před 2 lety +1

      Thanks for replying the problem is , when we register we register and interface , so when getservice the list in the container containes the interface , the predicate that evaluate abd return the dependency, the change need to be done there right ?

    • @RawCoding
      @RawCoding  Před 2 lety

      I don’t understand

  • @cffee_
    @cffee_ Před 3 lety +1

    Transient lifetime is an Anti Pattern, breaks single responsibility principle makes code behavior easier to break.

  • @kopilkaiser8991
    @kopilkaiser8991 Před rokem +1

    I lost understanding from the part where you add the parameter in the container. I will repeat and again try to understand. This dependency injection is complicated or I think you have gone a bit complex on trying to build a generic application which will be compatible with any type. I will not give up to give effort to understand.

    • @RawCoding
      @RawCoding  Před rokem

      the container contains types that it knows how to create, if 1 type requires other types to be created, the container needs to know about those required types and how to inject them.

  • @RayanMADAO
    @RayanMADAO Před rokem

    So much abstraction it makes me confused

  • @hydtechietalks3607
    @hydtechietalks3607 Před 3 lety +1

    whats this secret talk... speak louder dear!

    • @RawCoding
      @RawCoding  Před 3 lety

      Haha that was back when I just got the mic, was trying to configure it

  • @eclipsetutorialvideo
    @eclipsetutorialvideo Před 4 měsíci

    Impressive you did it without edit it seems :) You did Singleton and Transient, it would be good to know how Scoped is working as lifetime