Design Patterns: Dependency Inversion Principle Explained Practically in C# (The D in SOLID)
Vložit
- čas přidán 15. 06. 2024
- When you are writing code, are you doing it right? That is a question that worries a lot of people, and it should probably at least be something every developer thinks through. Design patterns are best-practice concepts that we can implement into our code to make it better in some way. Think of them as guardrails that keep our code safe. In this video, we are going to look at the fifth entry in the famous SOLID principle. The D stands for Dependency Inversion Principle. This is not the same thing as Dependency Injection, but it is what powers it.
- Newsletter signup (with exclusive discounts): signup.iamtimcorey.com/ (your email will be kept safe and you will not be spammed).
- Source Code: leadmagnets.app/?Resource=DIn...
0:00 - Intro
1:43 - Demo code walk-through
4:40 - Dependency Inversion Principle: DI Explained
7:42 - Implementing DI: Demo code refactoring
26:15 - Benefits of DI: Changing the code
27:18 - Benefits of DI: Modular code
29:10 - Benefits of DI: Dependency injection
33:03 - Benefits of DI: Unit testing
37:32 - Summary and concluding remarks
These are the best series on SOLID in youtube. Thanks a lot.
I appreciate the kind words.
It's true. These are the videos with the best and simplest explanations.
It's true!
There is no replacement for video content and this type of platform. You get to watch it any time, any where, and as many times as you want to and you can. So glad to be alive in this time and age. Thanks for the content Tim. I leaned a lot.
You are most welcome. Thanks for watching.
Dependency inversion is the principle; dependency injection is one way of making the principle work. - I love you man 5*****
Thanks.
I've been writing code for 15+ years. I still watch Tim to remind myself of best practices.
He is very passionate about using best practices
I have been taught here and there about the SOLID principles for over a year now. This seems to be the first time that I have really grasped these concepts completely and now also know how to use them. Thank you and great material in general.
Awesome! I'm glad it finally clicked. Using these principles can really improve your code.
Alexander Markussen totally agree. Thanks Tim Corey
I actually finally understand abstract classes, interfaces and their purpose. This SOLID series helped me a lot!
Awesome!
me too ))
By far the best explanation I have seen on dependency inversion. Thank you.
You are most welcome. Thanks for watching.
Man, I watched these videos as an experienced developer, I almost didn't learn something new.
BUT I'm so amazed at your skills to explain things in such an interesting and ordered manner. I couldn't stop watching you.
I'm so amazed that such quality content is available for free here.
Thanks, man, you're doing a gorgeous job, hope more people will see these vids.
Awesome! Thanks for the kind words.
Tim, at first when I saw the length of any of your videos, I thought "oh man...."; but now I know for sure that those 40-60 min are enough to get away with the concept and how it looks in real life/in real code... instead of spending that same hour (probably a lot more) going the hard way: digging on the internet trying to clarify any given concept.
Just go straight ahead to Tim's channel and enjoy the explanation of such a wonderful teacher.
Thanks for all the awesome and comprehensive content you make!
I am glad you found them valuable (and stuck with them to figure that out).
My eyes lit up while watching this! 3 years ago a small company on a conglomerate project we had went live. They were using an automation approval tool that didn't allow much programming but had all the tools i.e. API call, SQL, WS, message builder etc. They were so dependent on this that the business logic was built into each approval flows. We had to do a lot of code changes every time the approval level change. I eventually had to implement a matrix table where all the approval flows adhere to. It made the application dynamic and they can control it however they want as long as they follow certain rules(like the interface).
It's funny how this principle can be implemented on many other platforms. I think ServiceMesh also has the same concept. You have a lot of containers and they all follow a certain set of rules and all reports back to the command center.
Thanks Tim for this wonderful lesson! As always you untangle the complication of many programming concepts. God bless you!
You are welcome.
The best C# teacher in the world! And I really mean it. Thank you so much, sir! Brilliant teacher!
I appreciate the kind words.
I learned more from this short video than I did in weeks of browsing and reading on the topic.
Awesome!
Finished the whole SOLID series.
Great series of vids! I really feel like I understand what SOLID is all about and you did a goo job of *showing* why you should use SOLID.
Can't wait to see that video about Dependency Injection now.
Excellent! I'm glad this series helped you get a grasp on the why of SOLID.
You're awesome, years in the industry and i always thought Dependency Injection was same as Dependency Inversion. Thank you.
You are welcome.
Absolutely one of the best SOLID youtube series, well done ! I was struggling refactoring my highly dependant codebase and now i'm refactoring all of my apps one by one, making it more modular/extendable. Big thanks !
Glad it helped!
Thank you Tim. Your detailed yet practical explanation of all the concepts in this series is truly worth its weight in gold. I'll personally recommend this to everyone who is learning SOLID!
Awesome, thank you!
3 shots to the approach for explaining Dependency Inversion. It goes and sits into our heads and stays there for a long time.
Keep up to good work Tim.
You are really making it easy to learn software development.
I appreciate the kind words.
The best SOLID explanation I have found so far! Even better than my Uni- Professor. Thanks a lot and keep up the good work!
I am glad you found it valuable.
Finished with your SOLID videos and a few others about interfaces and generics. The way you SHOW the reasons to use SOLID and the fact that your examples are very similar to real life situations....it makes the content very easy to understand and retain in my mind. Can't thank you enough, I'm gonna watch ALL of your videos!
Awesome! I'm glad you are getting so much out of the content.
Thank you, Tim. This series has been eye-opening! I might have to go over Liskov Substitution principle once more but I look forward to it. You have a fascinating skill in articulating what some would find difficulty in explaining. Thank you so much!
I am glad my content has been helpful.
Thank you Mr.Tim. You're like a father and a mentor to most developers out there, and a grandpa to newbies😂. I really learnt a lot. What brought me here is your tutorial on Dapper and minimal APIs where I asked why you created interfaces for each of the classes, and you replied because of Dependency injection. Since then I've been curious and I'm glad I finally got an answer. Thank you again sir. May God bless you in Jesus Christ name. Amen😌.
You are welcome.
Really enjoyed watching this SOLID series. Learned alot and some things I was fuzzy on have now clicked. Thanks for taking the time to put all of this together!
I am glad it was helpful.
Hey, you're videos are amazing. I am actually a computer scientist and know all this but i love to watch your videos since they're of really high quality. I'm sure you're helping a lot of people with this kind of content and the way you're presenting it.
I appreciate the kind words.
Tim, I just want to say your series and topics are the best. So much clear and easy to follow than any other tutorials I’ve seen so far
Keep up the good work, can’t wait to see your future ones
Thanks!
One of the most useful C# channel out there. Thank you for your will to share the knowledge with us Tim!
You are welcome!
Hi Tim, Thank you very much for this wonderful session. Here's my summary and understanding after practicing the demo project:
Dependency inversion states that the higher-level modules should not depend on the lower-level module, but rather both should depend on abstraction, and those abstraction should not depend on details. Basically, what this means is, higher-level modules should not create objects of the lower-level modules (classes). This responsibility should be assigned to new class (this class (Factory.cs) takes care of the object creation). The higher-level module classes should implement interface to seperate the implementation. The advantage of this is - if a new class (texter.cs) is addedd in the future to the project, the change is only done at the Factory.cs and does not impact the other files (classes).
The abstraction should not depend on the details.. the interfaces and Factory.cs does not depend on detail, the implementation is done by the classes that implement these interfaces, Factory.cs only job is to create the objects.
I hope this understanding is correct?
It is so nicely explained that it is impossible not to understand. Great Video, I wish I would have watched these SOLID videos before.
I am glad they were so helpful.
Finally the good explanation of DIP after several videos and articles. Thanks a lot!
You are welcome.
finally, I understand this principle...thank you Tim, as always!
Excellent!
"At some point, you have to new up an instance " this cleared my confusion
Excellent!
Somethings on this earth are timeless, this my friend is one of those things. Thank you.
You are welcome.
I spent +/- 4 hours (with breaks and by making a lot of notes) to finish your video. You know what? It is definitely worth your time.
Thank you for sharing this knowledge with people!
I am glad you got a lot of value out of this video. Good job going beyond listening to actually studying what you watched.
I just started looking at Design Patterns not more than a few weeks ago, even bought a course on Udemy.
What are the odds that the best guy to explain complex principles comes in to rescue the day, just as I'm about to learn all this stuff.
Thanks for these videos they are great.
Awesome!
These videos really helped me. I always thought I was a good programmer. When I actually got a real job, I see my company implements all this stuff and I was confused on why I kept seeing all these interfaces everywhere. It's mind blowing I didn't know this sooner. This last video is especially amazing. Thanks Tim.
I am glad it was helpful.
I like that you explain it in the bigger picture. That's a way I can easily understand through your great video. I am very glad that you take your time for this great YT channel. Have a good day!
I appreciate the kind words.
Best explanation on DI I've found on CZcams. Thanks!
You are welcome.
After watching tons of videos, this practical tutorial made the DIP concept crystal clear, thanks a lot!
Great to hear!
Thank you for this, that differentiates (or clears the distinction) with dependency inversion vs. injection. You're awesome, Tim!
Glad it was helpful!
Thank you for making these videos. Watching them SOLID in practice has been immensely helpful.
I am glad my content has been helpful.
Wow! I have years of experience and I think your tutorial is very useful. What I like is, you get to the point. I am going to look at more of your tutorials. Thank you very much.
You are welcome.
These are so helpful and clear. Thank you for taking the time to make these. It's greatly appreciated
Awesome! I'm glad they are clear and helpful.
As always simple & elegant. Thank you for the video.
You are welcome.
OMG I watched only this one in the SOLID series and my mind is -blown-
This is exactly how I always thought programming was supposed to be like but my formal education did not go over SOLID, I didn't even know it existed... Thank you so much!!
Awesome!
Thanks to this, finally I get Dependency Injection - such a relief, thank you so much Tim!
Happy to help!
Thank you Tim! I really learned a lot from the SOLID series and looking forward to what's up next.
Excellent. I'm going to cover a couple of one-off topics next but I will be returning to add more to the design patterns series. I'm glad you've enjoyed it so far.
If I watch all of Tim Corey's videos, I'm pretty sure I can get 2 or 3 promotions at once. Seriously, these are the best videos I have ever seen.
We appreciate your watching and contributing. Thanks!
I genuinely loved this video. Thank you!
You are welcome.
I’ve been coding for more than 15 years and I thought I knew all of this stuff , until I started interviewing recently for new jobs and I couldn’t really explain it. Now I can. And also , the code at my current company is a mess. These videos are great! Thanks
Tim, you are a great teacher. In fact, I think you are the best teacher I have ever seen. Congratulations and thank you very much for these explanations! I finally got everything i need to know about dependency inversion! :D
I appreciate the kind words.
This part was probably the most eye opening video of the series. I didn't really get why i should do this implementation at first, but after some time it clicked and became obvious why this is so powerful.
Should i ever persue a career in c# programming, i will make sure to return the favor.
Stay awesome Tim.
I am glad it was so helpful.
Best description of Dependency Inversion...Thanks a lot Tim...
Thanks!
The clearest explanation or how to explain an absratc concept clearly and simply !!
Thanks a lot Mr Corey !
You're very welcome!
I Finally understood the 'DI'. This is wonderful. Thank again, Tim!
Excellent!
This is by far the best tutorial about Dependency Inversion principle, it's the first time i really think i understand the concept. great work Tim
Awesome! I'm glad it finally clicked for you.
Thanks Tim,
looked online for a lot of examples, glad you actually went through the code and explained it clearly!
You are welcome.
These are the best series on SOLID in youtube.(2)
Great job!
Thanks!
The best explanation of SOLID principles I have ever seen/read. Tim, you should also do a series on the various design patterns like Factory, Observer etc.
That is in the works.
The best explanatory tutorial I ever came across thanks
You are welcome.
Now I get why I got confused in the I video; things from SOLI are much clearer for having watched the D!! Great videos.
I am glad it all fell into place.
Thanks a Lot Tim for the free invaluable content ! God bless you.
Glad it was helpful!
Thank you so much for your content, this helped me A LOT with one of my more difficult courses in college. I've watched all 5 of your SOLID videos, and I'm continuing :D.
You're very welcome!
Indeed as many comments state, THIS IS THE BEST SOLID-SERIES ON CZcams. Thank you for taking the time to explain these concepts in an easy-to-understand way & using accurate examples. I've read about SOLID and watched many YT videos about them, but YOUR videos are the one who made see through and clearly understand why they exist and how they make our life so much easier... Thank you for all the effort, you deserve MORE SUBS!!
Wow, thank you!
Very good explanation of the SOLID principle, thanks Tim, you are the best!
Even with your clear and beautiful diction recorded in this video, I needed a subtitle because English is my second language. I can't stop laughing at the auto-generated subtitle "nude up" when you said "new'd up". Yes, I'm still childish 🤣
😆
Thank you, Tim, your examples are very easy to understand the principles
You are welcome.
Wow is this good. I know I have to watch it like three more times to completely get it all, but did I ever learn a lot from this. THANK YOU TIM
You are welcome
For the first time in my life , dependency inversion actually made sense. Thanks a lot!!
Awesome! I am glad it was helpful.
Love it! Succinct and thorough. I read five different sources but your video is what made it crystal clear for me.
Thanks for watching and for the encouragement
Thanks Tim. You are the best. I found this video very helpful and informative. You have a talent to explain concepts simply and easy to understand. At times, I struggle to understand these concepts from other sources because they over complicate the concept. But, you on the other hand cover all spots but still being able to make sure the viewer understands most of the explanation! At least I understood almost most of the part, it is just I have to review it couple of times to make sure it all makes sense.
Great video Tim. I learned a lot from you. I finally understood interfaces in C#.
Excellent!
Just like all other videos I've seen from you so far. Amazing. This is the first time for me everything comes together and makes sense as a whole. Thank you for that.
I have heard and read about the SOLID principles a couple of times now, and when dealing with just (C#) code, it starts to make a lot of sense. Where I'm still struggling at this point is how these principles apply to IO. Like in databases or on screen. (Users these days don't really know how to work with a console.)
As an example, what I would be really interested in seeing is how you would persist the models you created in the last two video's (employee and library) and what this would look like with MS Entity Framework. This would also apply for Java Spring, or other ORM's, but as you say, this channel focuses on C#.
If you stay tuned to my current series (the TimCo Retail Manager), you will see DI being used in a WebAPI and probably the WPF app too.
Bigup to Tim ! Love your channel - your pace is perfect and your emphasis on the key parts is amazing (e.g. We are looking to great rid of "new" is such a great way to describe dependency inversion) - Shared, Liked and Subscribed !
I am glad you are enjoying the content.
0:00 - Intro
1:43 - Demo code walk through
4:40 - Dependency Inversion Principle: DI Explained
7:42 - Implementing DI: Demo code refactoring
26:15 - Benefits of DI: Changing the code
27:18 - Benefits of DI: Modular code
29:10 - Benefits of DI: Dependency injection
33:03 - Benefits of DI: Unit testing
37:32 - Summary and concluding remarks
Thank you again!
Your work educates people. Thanks.
You are welcome.
The best explanation and implementation tutorial about Dependency Inversion Principle and Dependency Injection. I can imagine clearly what is "DI Container" and the other DI structures.
Awesome!
You are a great teacher! Thank you so much.
You are welcome.
Tim, your SOLID principle explenation is far better and superior to any other tutorial on SOLID so far.
I'm glad you think so. Thanks!
This is gold! Finally I can say I understand DI - thank you. And in case you don't overuse mouse only to make it easier for us to follow what you are doing (like when you were replacing commas with semicolons) - by pressing Ctrl+Left/Right you can move cursor through code much faster.
Awesome! And thanks for the tip.
Truely awesome explanation of concepts Mr. Corey ..I feel now confident enough to recommend this SOLID series to folks in my circle to extend the ongoing Chain of Viewers.
Great!
Excellent video! very clear and easy to follow. Thanks Tim!
Thank you!
This is the SOLID video in the world of programming.
Thanks!
This is hands down the best explanation of dependency inversion. Thanks a lot 🙏🙏🙏
You are welcome.
Brilliant, I didn't realise the power of interfaces until now.
I am glad it was helpful.
Best explanation, after struggling to understand dependency inversion and watching tons of youtube videos which just describe what the principle definition is, found this amazing channel ❤
I am glad it was helpful.
After numerous videos and blogs, I understood for the first time how easy a factory method can be and where it can be placed. Most authors can or just want to explain it abstractly and academically - but you got to the point. Thanks a lot! ... An open question for me is whether the factory class from your specific example now violates the SRP principle... That's where it's going to be confusing to me all the time in OOP (coming from 15 years of procedural languages). Though I am firmly convinced and excited about the superiority of OOP - sometimes all that conventions/patterns seems to a large extent being busy clearing a lot of stuff from the living room into the kitchen and the hallway - only to conclude that at the end I am mostly in the living room and need most of the things right there :-)
Yeah, you have to be careful how you do OOP that you don't just do it because you should. I try to be very pragmatic about what I do so that the patterns and systems work for me rather than against me. Now to answer your question specifically about the factory pattern, no, I don't think it violates SRP because it is doing one thing - instantiating classes. Now, it does have more than one reason to change in some ways but not if you look at it as the reason to change is when a class is added or changed. Ideally, you use a dependency injection system to make this even cleaner and easier to work with.
Best software development channel on youtube by far!
Thanks! Be sure to check out the Blog and Podcasts at IAmTimCorey.com
These video are really helpful to understand SOLID principle.Thanks a lot.
You are welcome.
Tim. Much appreciated. This complex principle was made easy to understand.
Excellent!
You are really making Coding Simple. Thanks a lot Tim.
Glad to hear that!
This changed my life. Thank you.
You are welcome.
So clear and easy to understand tutorial! greatly helpful!
Happy to hear that!
Thanks for these amazing SOLID classes, Tim.
I'm reading Clean Architecture by Uncle Bob and your videos helped me a lot in terms of seeing things working on the real world.
You are welcome.
Solid example, really helped me to understand the principle and it's benefits. Thanks!
Glad it was helpful!
Excellent Explanation. Thank you very much Tim. :)
You are welcome.
i learned at my university what is interface, abstract and so on... but have no idea how and when to use them in my development (im a noob obviously), but after watch your SOLID videos, i finally have a strong understanding on how to use them. Thank you so much
Awesome!
SOLID can't be explained better than this :) Thanks a lot.
Awesome. Thanks!
Thank you so much, simple and precious
You are welcome.
You got a new subscriber. This was an excellent explanation. Thank you!
Thank you and welcome aboard!
SOLID simplified in the right way, great refresher course...
Thank you!
The best SOLID explanation. Thanks a ton Tim! :)
Very welcome!