Dependency Injection Lifetimes in .NET Core (.NET Interview Questions)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] net core and you are confused on whether you should use transient scoped or Singleton you are on the Right video my name is broken and welcome to dotnet mastery if you are new to the channel make sure to subscribe to the channel That way you are aware of all the new content that will be posted also if you find the content helpful make sure to like the video and leave a comment in this short video we will see the different service lifetimes when it comes to dependency injection in.net core before we take a look at any example let me quickly give you a rough idea on what each lifetime is responsible for transient lifetime is the simplest one among all of them and it is also one of the safest one transient basically means that whenever we want any implementation create a new object and give that new object you never have to reuse an existing object every time a service is requested a new implementation is created after that we have the scoped where it depends on the HTTP request so whenever an HTTP request is sent to the server that time for that scope one lifetime will be created and then the same object or same service is used wherever in that request the service is requested so let's say for one single page load we are calling a service 10 times it will only create that object one time and it will use that same object 10 times in that one request but then if a next request comes in then a new implementation is created and finally we have Singleton where one implementation is created for the lifetime of the application so once the application starts we create an object of that service or an implementation and that is used for all the upcoming requests unless you restart the application so to give you a rough idea again transient new service is created every single time for scope the new service will be created once per request and for Singleton new service will be created once per application lifetime now scoped is one of the most recommended approach for web application because of that a service will be created on each request but even with all of these overview things might be a little complicated so let me explain that little more with a real world example let me switch to visual studio and we will be creating an MVC application I will give it a name of TI service lifetime and let me create that in the project here let me create a new folder for services in there let me create three interface or the three service that we have so the first one will be I scoped let me add a new one here that will be Singleton and finally we will be adding the transient these are just the interface name we have not added any functionality in there let me add a method get grid which will return a random quid so the return type will be string perfect I will add that in all the three interface that we added now we need to add implementation of these three interface implementation will be pretty simple let me add a class here for the Singleton quit service that will implement the I Singleton quit service implement the interface here let me create a private read-only grid with the name of ID and in the Constructor ID we will say grid dot new grid and the function here we just want to return ID dot to string that looks good let me create the same implementation for the other two interface that we have let me add a class which will be sculpt grid service and that will implement the I scoped quid service I can copy everything here paste it and we will change the Constructor name looks good let me add the last one which is transient quit service but let me copy and paste it in here and this will implement the I transient grid service so now we have three interface and their corresponding three implementation that looks good but when we register this services to program.ces we have to write the life cycle to which we want to register so in the add service here we will say Builder dot Services dot add and then we have to write what life cycle we want to use we have scoped transient and Singleton in Singleton we will have the interface which is I Singleton quid service and implementation is inside the Singleton quid service that is how we register a service to the dependency injection container we will also register the transient and scoped service so it is exactly same like a favor registering DP context to the container and then when we have to use that we will do that inside the home controller before we start using that right here let me create six private Fields two will be for this curb service 2 for Singleton and two for transient we have scoped one two Singleton one two and transient one two next what we want is implementation of this interface we registered that in program.cs right here so basically we are saying that when someone asked for an implementation of I Singleton grid service we are telling the dotnet framework that provide them the implementation that is there in the Singleton quid service but there we are telling that use the add Singleton that is the lifetime so when we want to use the implementation inside the Constructor here we can directly ask.net core that we need the implementation of ice scoped grid service and we will call that as scoped quid then we want another implementation of that so scoped grid 1 comma we will have scoped Grid 2 and similarly we want the implementation for Singleton and transient service so these are the implementation that.net framework will provide based on what we registered in program.cs then we have to assign these implementation to the local variable that we have right here for that we will say underscore Singleton 1 is equal to Singleton 1 and same for everything else I can change this to be scoped one rather than scoped quid here perfect looks good so we have this implementation on there we can call the method which is cat quid rather than making anything fancy let me use a string builder for messages here and I will append to that message using string interpolation so first I will say transient 1 and then we will display the value using underscore transient one dot category we have that method and after that let me add a new line so perfect that looks good for the first transient copy and paste that once again we want to display the transient2.catquid and let me add one more new line there copy these paste it two more times next we want the scope one scoped one scoped two underscore scope 1 underscore scope to and finally we need Singleton perfect when we return back we do not want to return back to a view we just want to return OK and display the message dot to string on the screen so now we need to think about what will happen all of them have basically the same category method which returns a random quid but the only thing that is different is on how the life cycle have been registered so let me run this and see that in action and perfect we have something right here you can see when we are working with the transient request here the grids are different but when we have scoped our Singleton they are the same as we know Singleton stays same for the life cycle of the application so even when we refresh the application scope and transient will be different but Singleton will always stay the same you can see when I am refreshing here transient is getting updated along with scoped but Singleton always stays the same and then for scoped here you can see every time for a single request it gets a new value but for one request the value stays the same in both of them but in transient for any time a quid is requested it calls a new grid so that means every time when a transient is called it basically gives a new implementation of that service when it comes to scoped the implementation stays same for one request if in that one request we are calling this 10 times we will get the same service but for the next time it will get you a different service when you are working with scoped so with that you can see how transient scoped and Singleton are different when it comes to dependency Injection Service lifetime I hope you have enjoyed this short video and for more videos like this make sure to subscribe to the channel and I hope to see you guys in some other video
Info
Channel: DotNetMastery
Views: 6,045
Rating: undefined out of 5
Keywords:
Id: 7myI-l3Mrec
Channel Id: undefined
Length: 11min 58sec (718 seconds)
Published: Sat Feb 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.