What is Dependency Injection? C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Holy moly your voice is so soothing.

Plus the video is very informative. Keep it up!

👍︎︎ 6 👤︎︎ u/Sorry-Chair 📅︎︎ May 13 2021 🗫︎ replies

Great job

👍︎︎ 2 👤︎︎ u/skramzy 📅︎︎ May 13 2021 🗫︎ replies

Great video! Subbed 👍🏻

👍︎︎ 2 👤︎︎ u/Fishstereo 📅︎︎ May 13 2021 🗫︎ replies

Great video and explanation.

👍︎︎ 2 👤︎︎ u/nhippe 📅︎︎ May 13 2021 🗫︎ replies

Nicely done.

How about one on async/await?

👍︎︎ 2 👤︎︎ u/GogglesPisano 📅︎︎ May 13 2021 🗫︎ replies

I have a pretty solid handle on DI but now I must watch just to hear the voice.

👍︎︎ 2 👤︎︎ u/screwuapple 📅︎︎ May 14 2021 🗫︎ replies

Could listen to your voice all day man. Nice video.

👍︎︎ 3 👤︎︎ u/Boogeyman_liberal 📅︎︎ May 12 2021 🗫︎ replies

Subbed, the descriptions of the processes are brilliant but it also helps to see even the most basic examples of code when possible. Thanks, keep it up.

👍︎︎ 1 👤︎︎ u/ManikMiner 📅︎︎ May 12 2021 🗫︎ replies

oooh this is a good video

👍︎︎ 1 👤︎︎ u/tweetremoved 📅︎︎ May 13 2021 🗫︎ replies
Captions
let's talk about dependency injection [Music] hey guys what is up my name is piano if you guys are new here this channel is all about coding so if you like that stuff be sure to hit like hit subscribe and check out some more videos let's get into it so what are we going to be covering in this video well we're going to be covering dependency injection and also in version of control when people start talking about dependency injection they also start bringing in concepts from inversion of control so i'm going to put them in one bucket just for the simplicity of this video so what is dependency injection the most basic definition for dependency injection is passing an object its properties there's different types of dependency injection we have constructor injection property injection and method injection the most common most widely used is constructor injection so we're going to be focusing on this one for this video so let's take a look at an example so in our non-dependency injection example you can see our service has a constructor and in that's inside that constructor it's actually creating our database so the only difference is our service has a constructor but that constructor now takes in a parameter of i database and that database is then initialized from the parameter and that's it for dependency injection it's not complicated it's not that hard all it really is is passing an object its properties and that's it so now let's talk about a version of control so this is where it gets a little bit more complicated and it's probably going to require a little bit more explanation so bear with me so what is inversion of control my best explanation is the process of creating objects and all of its dependencies so let me give you an example so let's say we have a box and this box is special and it knows how to do some complicated things and we add our objects to this box so we add our service and we add our database from our previous example now we actually ask the box to say hey i want my service please can you give me my service and this box since it knows some special stuff and it knows some complicated things it knows that service actually depends on database so when it gives you the service it will also give you its database as well so it will create the object for you with all of its dependencies and that's the basis of inversion of control it's pretty much just saying hey i want this object give me my object with all my dependencies all already created and do that for each dependency and so on and so on so that's a real basic explanation of inversion of control but now we also have some more advanced features such as lifetimes so now let's look at lifetimes and this is probably best explained with an example so bear with me so we have three different lifetimes we have transient scoped and singleton so let's start with transient lifetime so let's say our service was registered as a transient what that means is every single time i ask my box hey can you please give me back my service it will give you a brand new instance of the service every single time you ask for it next we have scoped so probably best to explain what a scope is i like to think of a scope as like a period of time that something should exist so for example i like to think of a http request so the request comes in and the request goes out that time period of the request coming in and the request coming out that is the scope and what this actually means is let's go back to our box example if i ask my my box hey can you please give me my service while inside of a scope it will give me the same instance of service throughout the entire scope if i ask the box for my service three times within a scope those three times i will have the exact same instance of my service and finally we have singleton so hopefully this is pretty self-explanatory because we have the singleton pattern and essentially what this means is that there will be a single instance of an object throughout the entire application so if we go to our box example again if i register service as a singleton every single time i ask for for my service from the box it will give me the exact same instance every single time so now that you know about dependency injection and inversion of control let's talk about why these things are useful so probably one of the most useful things about dependency injection is that it really helps with unit testing and what i mean by that is it's a pretty common thing when using dependency injection to depend on interfaces rather than actual concrete objects and what that actually allows us to do is that there's a lot of frameworks out there that allows you to actually mock objects and what that means is essentially create a fake object that doesn't really do anything so you can actually unit test your specific class that you want to test so if we go back to our example you can see we have a service that depends on i database what i can do is actually mock out this i database to actually not do anything and then i can purely test my service the second benefit i think of using dependency injection is it helps with your design and what i mean by that is when you're using dependency injection it's pretty common to start using interfaces and that helps with your design as well it makes you think about the single responsibility principle so when i'm designing a class and i find myself having like database connections or something like that i immediately think hey i should put this in its own class and maybe have an interface that that my service depends on so i can easily mock it out and i can and i have my separation of concerns so that's it for this video guys hope you guys enjoyed if you did be sure to leave a like leave a comment and subscribe i'll catch you guys the next one see ya [Music] i
Info
Channel: Jonathan Williams
Views: 8,902
Rating: undefined out of 5
Keywords: c# dependency injection tutorial, c# dependency injection code example, c# dependency injection, c# dependency injection example, c#, dependency injection, dependency injection .net core, inversion of control and dependency injection, inversion of control c#, inversion of control explained, dependency injection for beginners, what is dependency injection, dependency injection explained simply, dependency injection explained c#, dependency injection explained
Id: s6EVfmUgX60
Channel Id: undefined
Length: 5min 40sec (340 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.