ASP.NET Core Dependency Injection - Singleton, Scoped, and Transient

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello hello everybody my name is Billy and in this video I'm going to be showing you how to pendency injection works in asp.net core and if you don't know what dependency injection is make sure you check out my last video where I go in-depth into the explanation today's topic is just going to be a quick video on how to set up dependency injection the different life cycle hooks and at the end of it I'll just give you a quick tip on how to keep it all organized when the done so far is just create a empty application you can see it's all of the default and the place you look into to start your dependency injection registry is in your startup and I'm just going to minimize so you guys can see all of the details so your startup will have a constructor if you have an empty project you won't have a constructor you will have a configure services and you will have a configure so what you're interested in is configure services this is where you will register your dependency injection into so usually I do it after add MVC in case any of your dependencies if you have any new get ones they might require at MPC there are three life cycles you need to consider yourself with the first one is singleton services dot add singleton the second one is scoped services on ADD scoped and the third one is transient services let me just move my mouth so you can see services dot add transient add singleton will create one instance of that object or the entirety of the applications lifecycle so whenever your application starts it will register that create an instance of that and stay for the rest of that until you shut down the server add scoped will be a object that lasts for your HTTP request life circle so on your let's say get that one object will be created and any subsequent dependencies that require it will have that same object passed around an ADD transient will be in a class scoped life cycle so every time you have a new class it will produce a new instance for you and share that into its dependency tree I'm just going to quickly comment these out just so they're still here and then what we're going to do is we're going to create using kind of the Microsoft example eye operation and then we're going to create a singleton scoped and a transient version of all three of them [Music] all right so I just quickly made all the different operations so I have the singleton the scope the transient and all of them they just create a new ID that we can peek into to kind of make sure that they're recreating for transient and scoped on different requests and the singleton will be the same and then I've also just created a my service interface to that we will just see so we can inject that into our controller and see how these are changing as we go through go back and finalize all of our registries so in start up we're going to register our singleton now and what I've done ahead is I have a bunch of different ways that we can actually register our singleton well the first way is with generics so we have I singleton operation let me resolve that names waste we have a generic which is the abstraction and then the implementation and in the same thing we can do here we have a in abstraction and we have the implementation that we can just new up and then if you don't have a abstraction you can do one of these where you can just set the implementation only or you can do up the implementation only and then you can also do one of these things where you can set it by type of sews once again if you have implementation only or if you have your abstraction and implementation you can do it like that I'm going to keep it very simple and have it just like this and I'm now I'm going to just add the rest of them so I'm going to add I scoped and I had a scoped in operation and same thing for transient transient and I just want to show one more thing services and I'm gonna add transient for my service just so you can see if you do need to provide additional parameters you can do it this way so you can see there's a provider here I service provider that you can tracked out so yet the return [Music] return this I mean to cut that and this and what this expects is the singleton scope and transient operator so you can use the provider here so let's say you want to get the singleton out and I'll get service on the provider and it will be able to resolve that for you on run time so we want this one we can get that and then you can grab the scoped in transient obviously but what this lets you do is if you have any custom fields you can actually pass them in at this point since I don't I'm just going to remove that and we're going to save this and now take a look at the life cycle in our controller alright so I've cleaned up everything a little bit close the tabs and also in our controller I've added all the different dependencies so I have the singleton the scope the transient and then the service which once again the service will have the same thing singleton scope and transient what I'm expecting is that the singleton will have the same ID every single time the scope will have a new one for each time we refresh the page and the transient should be different between my service and home controller every single time so let's run that and see how that looks like okay and I have my page right here so it's just loading up and it's blocked because I have my debug and so we'll see here so this is 7 a B a 2 e and this is 8 7 D so what we're gonna do is we're gonna step through and then we're gonna hit the home controller here and we're going to make sure that these is the same this one should be the same as well someone should be different and that was eight seventy okay so now that's totally different and we can totally see that and what I'm going to do is I'm going to refresh this page and we're going to basically see that this one the scoped one is going to be different so let's take a look at that I'm just going to quickly refresh that's the same now this has changed and this transient we've already proven that this will always change okay so once again watch this one and it should be different than this one here so in your own design make sure that you keep in mind which life cycle you want to use if you're using a configuration or a database connection or an HTTP client it's best to use Singleton's sculpt is usually used most of the time throughout anything that you're gonna have a request life cycle and transient is just anything you want to just throw away real quick finally one of the rules about the life cycles is that you cannot have a scoped inside of a singleton because if you have a scoped scope we'll be creative per request and singleton starts up when the application starts up so when you do this your system will blow up on runtime and you'll see the exception that gets wrong as you keep in mind you can't do this you can have transient transient make sure that it is thread safe because it has to be in a singleton well you can see right here invalid Operation exception and then you'll see that you cannot have scoped inside of a singleton okay so that's very important to keep in mind when you are designing your dependency injection tree okay so I'm going to just undo this now just delete all of this and so we don't need it and let's go back to our startup and just go give you guys a quick tip on how to stay organized because as your dependencies grow your startup is also gonna grow directly so a nice thing to do is to actually encapsulate all of them into like dependency registry extension method see registry extension oil let's just call it dependency injection registry just keep it nice and simple and what you want to do is you want to make a static class and you're gonna create a public static and you want to return a I service collection and this is totally optional if you want to return it I do it just so just so they can kind of daisy chain together because if you notice add singleton it also returns a I service collection and all that does is it lets you write yours tax like this that you don't have to keep calling services dot you can actually add it all like this okay I'm just going to undo all that and I'm going to go back here so the convention is to call ad and then whatever you're registering so I'm going to just call it ad my services and you're gonna call this I service collection to make it an extension method and I'm just going to return services oops services alright and it's very simple all you have to do is copy all of your dependencies I'm sorry I'm just gonna cut them and paste them right here and then replace the startup file and we're going to just take services and we're just gonna call this and that makes it so much more clean because now you have one thing and this file can kind of grow as is and if this is in a different project it can be broken down into more sub registries as well that is it for today's very quick tutorial if you liked this short format make sure you like this video to let me know and comment below and anything I can improve on and if you want to see more in these videos make sure you subscribe to our Channel I'll see you guys next time
Info
Channel: Billy Tech
Views: 18,185
Rating: undefined out of 5
Keywords: dependency injection tutorial, c# dependency injection .net core, c#, Billy Tech, asp.net core, dependency injection container c#, c# dependency injection tutorial, .net core, dependency injection, dependency injection c# example for beginners, c# dependency injection advantages, asp.net core dependency injection controller, dependency injection explained
Id: HXKGKvoQ0Xs
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Tue Apr 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.