Dependency Injection in C# ❘ A Hands-On Guide to Boosting Code Flexibility and Testability

Поділитися
Вставка
  • Опубліковано 28 вер 2024
  • Welcome back to Skill Foundry! Today, Eric Wise will demystify Dependency Injection, a key concept for improving code flexibility and testability. We start with an overview and then dive into hands-on coding, using C# to transform a basic Rock-Paper-Scissors game through Dependency Injection. This tutorial caters to developers of all skill levels and covers common pitfalls and their solutions.
    Eric will refactor a tightly coupled Rock-Paper-Scissors game in C# into dependency-injected components using interfaces. This practical example will make the concept clear and show you how to apply it in your own projects.
    Whether you're a beginner or an experienced developer in need of a refresher, this tutorial offers valuable insights. You'll learn how Dependency Injection enables the swapping of behaviors and facilitates easier unit testing, preparing you for more complex development projects.
    By the end of this video, you'll have a solid understanding of how to design classes for Dependency Injection. Don't miss this chance to elevate your development skills. Subscribe to our channel and activate notifications to stay updated. Thank you for tuning in, and we look forward to helping you become a more skillful developer.
    📍 Find Us Elsewhere:
    linktr.ee/skil...
    🛸 Join Our Discord!
    / discord
    💥 Learn to Code! Build an Unshakeable Foundation.
    www.skillfound...
    📬 Contact us anytime!
    www.skillfound...
    ---------------
    💬 Did you like this video? Let us know in the comments below!
    ✅ Click here to subscribe to Skill Foundry now!
    / @skillfoundryio
    ---------------
    Welcome to the official UA-cam channel of Skill Foundry - Learn to Code! Build an Unshakeable Foundation ☑️
    With years of experience in shaping talent across corporate offices, universities, and innovative coding boot camps, we've mastered the art of guiding you from scratch to an employable level of coding expertise. Our ability to deeply understand the challenges you face as an aspiring coder sets us apart. Each video we create is crafted to specifically address these pain points, enabling you to overcome common hurdles. At Skill Foundry, we strongly believe in the transformative power of education. Our mission goes beyond just teaching code; we aim to empower you with the skills and knowledge you'll need to excel in the ever-changing landscape of technology. Whether you're contemplating a career change, fresh out of school, or an enthusiastic self-learner, we're committed to providing you with all the tools necessary for a successful technical education journey.
    ➡️ Our learning approach is distinctive. We're not just about lectures and theory. Our videos blend theoretical concepts with hands-on exercises and real-world projects. This holistic method ensures that you're not just memorizing information but also applying what you've learned in practical, job-relevant scenarios. This means you gain both the confidence and the competence to tackle complex coding challenges, setting you up for long-term success in the industry.
    Expect weekly coding tutorials that range from the simplest 'Hello, World!' to complex algorithms, interactive Q&A sessions with industry professionals, career-building strategies, project walkthroughs, and much more. Our aim is to serve as your all-inclusive resource for coding education.
    Join us in our exciting educational adventure as we equip you with the key skills to excel in tech. Together, let's code your future! So, hit the subscribe button and turn on the notification bell so you won't miss any new videos! 🔔
    ---------------
    Common questions we'll answer in this video:
    - What is dependency injection and why is it important?
    - How does dependency injection improve code flexibility and testability?
    - How can I implement dependency injection in a C# project?
    - What are the benefits of using interfaces in coding?
    - How do I refactor a tightly coupled code into dependency-injected components?
    - What are common pitfalls when neglecting dependency injection?
    - How does dependency injection facilitate unit testing?
    - How does designing classes for dependency injection enhance software development?
    - What are the best practices for implementing dependency injection in real-world projects?
    - How can I use dependency injection to prepare for more advanced development projects?
    ---------------
    #SkillFoundry #cplusplusprogramming #education

КОМЕНТАРІ • 140

  • @SkillFoundryIO
    @SkillFoundryIO  4 місяці тому +3

    If you like this video and want to learn C# in depth from me, visit www.skillfoundry.io! The learning pathway takes you from zero to professional-level coding skills, but be prepared for rigor and depth. It takes over 700 hours to complete, with 80+ exercises and 12+ substantial capstone projects.

  • @guavavodka
    @guavavodka Рік тому +25

    If anyone is curious why it's called dependency inversion, it is because instead of the class depending on lower level details (where the choices come from), it now depends on a HIGHER level abstraction (the interface), thus the dependencies were inverted

    • @SkillFoundryIO
      @SkillFoundryIO  Рік тому +4

      Absolutely! I left that term off the video because most novices only hear about the injection part. 😇
      Thanks for adding more information!

  • @SproutSkout
    @SproutSkout 3 місяці тому +4

    I am sold when you said "We teach people how to code the right way."

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

      Especially in the current market it’s important to have strong foundations!

  • @RogerDunder
    @RogerDunder 5 місяців тому +14

    That was the easiest most clear explanation i found so far, thank you so much!

  • @gracicot42
    @gracicot42 10 місяців тому +4

    Hi! I'm the creator of the kangaru dependency injection library for C++. What I love about that tutorial is that you teach beginners to do it without a framework. A dependency injection library is only there to automate boilerplate that happens in the wiring code, and not essential for dependency injection.
    Another point (for more advanced programmers) is that technically, dependency injection don't require interfaces to work. union (sum types) or enums can also do the trick when you want your component to let users choose its behaviour, but have a closed set of possible behaviour. This still enable testing without introducing virtual function calls when unneeded.
    In my case, which might be a C++ only thing, adding virtual functions (any overridable function) strictly only for the purpose of testing is smelly code. I try to keep behaviour choices as closed as possible and thus do my DI with concrete types when possible with the choice of behaviour being values inside those concrete types.

    • @SkillFoundryIO
      @SkillFoundryIO  10 місяців тому

      Thank you for your kind words!
      I agree on the virtual methods as well, it’s a smelly pattern.

    • @gracicot42
      @gracicot42 10 місяців тому

      @@SkillFoundryIO Yeah, but definitely something that will only appear with larger projects, or projects with tight performance requirements. Out of the scope of the video, but still worth mentioning here in the comments :)

  • @NiKo-lh2nq
    @NiKo-lh2nq 2 місяці тому +2

    As soon as I opened the video and saw your face sir, I felt overwhelmed by a sense of trust. I immediately subscribed.

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

    This is exactly the approach we took in our project. OOP in C# is not complete if not done with DI. Dependency Injection was intimidating at first but once you fully understood its principles, you'll get addicted to using it in your OOP implementation.

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

      Agreed! There is always a trade-off for complexity, so for really simple things like batch jobs I sometimes skip it, but for the most part you should be implementing it, especially between layers of your project.

  • @stressfreesid9498
    @stressfreesid9498 4 місяці тому +1

    I will say you are the great master teacher. You have extra ordinary skills and techniques for teaching lots of blessing from all who watched this videos.

  • @zajac4817
    @zajac4817 15 днів тому

    This video got recommended to me just now. Amazing concept explained flawlessly. Thank you!!!!!!!😊

  • @christianfredrickgerman9170
    @christianfredrickgerman9170 6 місяців тому +3

    Thanks. You explain DI so clear and simple that even non programmer can understand.

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

      absolutely! I just explained it to my grandma (83 years old, can barely write) and she totally got it.

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

    Wow, really cool. I've been doing it a lot in my code, but didn't have a complete understanding of how powerful it is

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

      I’ve noticed a lot of tutorials show how but don’t spend much if any time on the why.
      It’s one of the reasons in our courses we refactor projects as we learn new techniques so our learners can better see the difference between novice and professional approaches!

  • @drgothmania
    @drgothmania Місяць тому

    I have to say that every time I read something about dependency injection, I kinda understand a bit of it yet still feel that it's an intimidating concept. But your explanation is so cool and simple. Thank you for making this. I love the example and really want to try it out right now.

  • @michi1106
    @michi1106 4 місяці тому

    Love the short but rly clearly overview and example, of what dependency injection is. At the begin i thought it was some evil stuff, but learned fast, its simple and rly helpful.

  • @joekimbler
    @joekimbler 7 місяців тому +1

    Fantastic explanation and example for DI!! You teach both the concept of DI as well as how it is implemented in c#. Simple and to the point. Great job.

  • @navjotkaur973
    @navjotkaur973 3 місяці тому +1

    In depth Explanation from a master! Awesome!

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

    Sir, this was OUTSTANDING! Thank you so much! I really appreciated you talking slowly thru it. Really helped me not to too confused along the way. You make a great teacher!

  • @justHeisen
    @justHeisen 8 місяців тому +1

    You have the perfect pace for teaching.

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

    I have been searching for weeks and watching tens of videos on the subject. I finally found it on your channel. Thank you for sharing 😂. It's clear to me now. I'm subscribing to your channel 😊.

  • @ВалерийСтарцев-з1у
    @ВалерийСтарцев-з1у 2 місяці тому

    Thank you for the video! This is one of the best explanations for Dependency Injection. And one of the best demonstrations of the right approach to programming.

  • @investinglearningmachine3951
    @investinglearningmachine3951 7 місяців тому +1

    Hey, this is the best explanation and examples. Thank you so much!

  • @marceloleoncaceres6826
    @marceloleoncaceres6826 4 місяці тому +1

    GREAT example, thanks for sharing it!

  • @taar
    @taar 2 місяці тому

    Thank you, this is so clear and easy to understand.

  • @martinroa
    @martinroa 5 місяців тому +1

    This video is gold.

  • @EA33964
    @EA33964 6 місяців тому

    Best explanation so far. So glad I found your video. Thanks

  • @Director414
    @Director414 6 місяців тому

    Great video! You really good at explaining stuff, well done! Impressive. Keep up the good work!!

  • @Krissini
    @Krissini 7 місяців тому

    fantastic video, beautifuly explained and an easy to follow and recode example. Thank you!

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

    The best explanation for this topic!

  • @julieyeaeun
    @julieyeaeun 9 місяців тому +6

    Eric you are an AMAZING teacher!
    Sincerely, a former teacher turned programmer :)

  • @StefanoTurcarelli
    @StefanoTurcarelli 6 місяців тому

    Thank you so much for this amazing example!

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

    Nice work, with a well chosen example

  • @bamf75
    @bamf75 7 місяців тому

    really nicely done...any chance you could do a DI container video?

  • @thanongtft6953
    @thanongtft6953 Місяць тому

    clean code, and maintenance

  • @ce1tzu
    @ce1tzu 3 місяці тому +1

    Gold content!

  • @CHURUTEInternational
    @CHURUTEInternational 8 місяців тому +1

    2 questions. are you in your bath tub? and does this work the same if I want to use dependency injection in xamarin forms or MAUI?

    • @SkillFoundryIO
      @SkillFoundryIO  8 місяців тому

      1. I’ve been upgrading my gear. Didn’t want to over invest if the channel didn’t take off.
      2. Yes it works the same.

  • @bradleyr4451
    @bradleyr4451 Місяць тому

    Hi this is great, am at 0:53 and see its a BLACK SCREEN with faint letters in tiny font? I started computers in the 70s, the monitors had extreme contrast by todays standard. This is eye strain, any way you can do one episode with the "normal" white background and dark blue text thats zoomed so it looks 18pt or so? why, WHY??? do people feel the need to use the black background? why? I have alot to say about DI and making changes to it. Thank you

    • @SkillFoundryIO
      @SkillFoundryIO  Місяць тому +1

      If I use the white background people complain too. Sigh…
      But done, next video will be default color scheme!

    • @bradleyr4451
      @bradleyr4451 Місяць тому

      @@SkillFoundryIO Thanks! Im in a very well lit room and have used the white screen since the late 80s when it was possible.

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

    This is an excellent video!

  • @KarZaz-k2g
    @KarZaz-k2g 5 місяців тому

    Amazing video 👌👌

  • @plaxor
    @plaxor 2 місяці тому +2

    watching in 1.5x speed.. better..

  • @ahmedh2482
    @ahmedh2482 4 місяці тому

    you are awesome

  • @joelmatthews3255
    @joelmatthews3255 4 місяці тому

    Great video. My only recommendation is to maybe forgo the background music. I personally found it a little distracting while trying to concentrate on your voice at times.

  • @666nevermore
    @666nevermore 3 місяці тому

    Why is this an interface? Why not a class Player that has a choice and then the 2 classes human and computer derive from player? I mean why an interface? It sounds more logic to me to be a class. I’m genuinely asking

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

      There’s a few reasons. What you suggest is polymorphic so you could substitute. However inheritance is an is-a relationship while choosing a type is a can-do.
      It is more modular and testable to be an interface implementation.

    • @666nevermore
      @666nevermore 3 місяці тому

      @@SkillFoundryIO yes and thank you. I was just wondering if you could use the same injection pattern but using inheritance instead of interfaces

  • @neilfaeskene4978
    @neilfaeskene4978 2 місяці тому +1

    So this looked to be a pretty good video, but then the background music started up. No added benefit, super distracting. So my recommendation would be to leave it out.

    • @SkillFoundryIO
      @SkillFoundryIO  2 місяці тому

      Thank you. I plan on doing future demos without music.

    • @zajac4817
      @zajac4817 15 днів тому

      Lmao. Zero problems with the music. A flawless video, but someone is always going to complain.

  • @josefvarilek6712
    @josefvarilek6712 7 днів тому

    Great tutorial, its definitely the first one I saw, which does not use any "magic" of a DI container, which is very confusing for beginners.

  • @HiwashiYT
    @HiwashiYT 10 місяців тому +2

    What an amazing explanation for a very confusing topic to beginners like myself, you even added the testing example that so many people mention but never show! Great work, will definitely follow your channel for more content.

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

    This video was a great refresher especially after being away from it for so many years! Is there any available code so I can save?

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

      Sorry, I haven't set up a GitHub repository for the UA-cam channel. Honestly didn't think about it. I'll put that on my list of things to do this month.

  • @panic_seller
    @panic_seller Рік тому +2

    saw this in a Job Description and Now seeing it on UA-cam all in less than 2 hours, The gods wanted me to learn this

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

      Hope it helped! If you want to go deeper into it in C#, check out our main site.

  • @SumitKumar-fn3gj
    @SumitKumar-fn3gj 29 днів тому

    one of the best video of this topic. Thank you so much!

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

    NIce tutorial! Thanks a lot! Promising channel.

  • @lingy-r8e
    @lingy-r8e Місяць тому

    That's helpful, thank you

  • @lemonade2345-j4e
    @lemonade2345-j4e 4 місяці тому

    lol. a switch statement anyone?

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

    I’m learning programming as a hobbyist and to make my work life easier. I am at the stage where I have learnt the basic C# syntax and solving individual problems. Now trying to start learning OOP, this is a really helpful approach to start understanding it’s uses!

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

      Stay motivated! OOP is the hardest part, once you get it the rest is not nearly as difficult to learn.

  • @LuisSieira
    @LuisSieira 4 місяці тому

    Very good video. Just... I'm not fan of the Iplayer nomenclature... the client of the API you defined does not care if it is interacting with an interface or an implementation. You manager talks to a Player, not to an IPlayer. Same way you interact with a List, not an Ilist, that you can new ArrayList, but the list is not an IList, and the ArrayList is not a ListImpl (I am not fan of the PlayerImpl either).
    You have a public interface Player, and a public class HumanGamer implements Player, and a BipBoopRandomGambler implements Player...

    • @SkillFoundryIO
      @SkillFoundryIO  4 місяці тому

      That’s mostly a c# thing. Most devs use the I prefix.

  • @PrettyBlueThings
    @PrettyBlueThings 6 місяців тому +1

    Excellent explanation!

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

    Can you do a video on the concept of "Clean Code". Also utilizing abstract classes and interfaces to achieve clean code.
    I've been interviewing a lot lately and Object Oriented Design style interviews have been creeping up a lot (Amazon famous for it). I personally would love it if you did.

    • @SkillFoundryIO
      @SkillFoundryIO  3 місяці тому +1

      I have an SRP video as well, but I haven't had time to get to the rest yet. It'll happen someday.

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

    Nicely explained

  • @أدرينالين-قصصرعب
    @أدرينالين-قصصرعب 5 місяців тому

    Thank you sir you’ve saved me ❤️ you way of explaining the concept is really amazing and the example itself is simple. Please explain the dependency injection container too 🙏 thanks again

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

    Thanks for this video. Seems strange to have the actual console, the view, inside of an interface class? Does that restrict how the human player is implemented? What if you were moving this to a WPF app?

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

      Yes I was just demonstrating DI. I think going full separation of concerns would be
      distracting to the learner.

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

    Nice explanation keep it up❤

  • @asyaaynurgers1261
    @asyaaynurgers1261 Місяць тому

    Really helpful video for begginners thank u

  • @TheSopk
    @TheSopk 4 місяці тому +1

    So clear, first time I understand how usefulin dependency injection.

  • @ernestocampese
    @ernestocampese 4 місяці тому

    Very well explained. Great job, thank you 👍

  • @F30-Jet
    @F30-Jet 4 місяці тому

    made me smile

  • @karimdh8549
    @karimdh8549 22 дні тому

    Thank you for the clear explanaition

  • @randallholmes4282
    @randallholmes4282 10 місяців тому +1

    Beautiful! Thank you!

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

    Another great video! Keep it up! 😄👍

  • @userman444
    @userman444 7 місяців тому

    Thank you! Found something new :)

  • @ataberktural
    @ataberktural 2 місяці тому

    Can you position your camera smaller in the next video? It's a bit distracting.

    • @SkillFoundryIO
      @SkillFoundryIO  2 місяці тому

      Absolutely

    • @ataberktural
      @ataberktural 2 місяці тому

      @@SkillFoundryIO thank you for your reply but its was just irony :)

  • @unknown.artist.track3
    @unknown.artist.track3 2 місяці тому

    awesome awesome awesome. just brilliant explanation and example

  • @bayanghabin
    @bayanghabin 29 днів тому

    YOU ARE AWESOME! I was struggling for days trying to understand what is dependency injection, Now I get it!! Thank you with all my heart!

  • @aluminatsulana
    @aluminatsulana 4 місяці тому

    This was some incredible explanation!
    I was realy struggleing with the concept at first but now after the video how I even struggled at the first place!
    Great video!

  • @Aleamanic
    @Aleamanic 2 місяці тому

    Great job explaining! First time, after watching many related videos, I could follow the explanation and got it. Presented at the right pace with good examples. Thank you!

  • @JanKowalski-ld4ec
    @JanKowalski-ld4ec 8 місяців тому

    I'd been lookin for good interface explanation until I found your channel. And you did best of what you could. Explained interfaces and DI at once, on working example. Fantastic job! Great content, great teaching approach, wide perspective filled with necessary details without overhelming the learner and topic. All the best for you and your channel!

  • @philariascfm
    @philariascfm 2 місяці тому

    Bro, you are such a good teacher. Thank you so much. You made this concept so understandable.

  • @KarZaz-k2g
    @KarZaz-k2g 5 місяців тому

    Great video

  • @jessadadissopa2318
    @jessadadissopa2318 2 місяці тому

    i like the way you teach, explaining things with not too fast speed and i like your energy. you're a great teacher. and I subscribed

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

    This is an awesome video! The way you explain this concept so clearly and calmly really made me understand it. Thanks!

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

      You are welcome! Now reinforce it with practice!

  • @waynegreen7970
    @waynegreen7970 8 місяців тому

    Good content!

  • @MarkVoz-n7j
    @MarkVoz-n7j 7 місяців тому

    this was amazon explanation, I love it, thank you

  • @SAAbir-si8wk
    @SAAbir-si8wk 4 місяці тому

    I can never thank you enough! God, It's a gem! The example, the explanation, the accent, the pace.... it's perfect! damn! 🤌🤌
    One other thing. The background sound-track is so soothing! What's the track name?

    • @SkillFoundryIO
      @SkillFoundryIO  4 місяці тому +1

      Not sure on the music, my editor uses epidemic sound subscription. They have a lot of music like that.