Singleton vs Transient - What's the Difference in .NET Dependency Injection?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right let's do it let's answer your questions about the.net dependency injection service for asp.net core.net maui and so much more and answer the question singleton or transient tune in to find out which one to use [Music] hey everyone i'm james montemagno and i'm back today talking about the built-in dependency injection service inside of.net now i get questions all the time because a lot of net maui developers are brand new to the built-in dependency injection service if you're using asp.net core for a long time this may be very common and familiar to you but there are many things if you're just getting started with net that are really important which is all about state management and what it means to register dependencies and register them as a singleton or a transient now i'm going to break down exactly what that means but if you're brand new to the net dependency injection service i have great videos right up over there talking about all why i love it and how it works really elegantly with don and maui now i'm going to use a donna maui application today to demo this off but of course you could be using and building any net application you could also be using other dependency injection services that are out there and they all have very very similar contracts when it comes to that singleton transient approach but let's get into it singleton it's a single ton no it's a single basically instance of whatever you register if you have a page a view model or a service what it means is that when you register it as a singleton that will be kept around basically as a global in a memory so when you register it and you create it for the first time and you get it back the next time you request it it will then go ahead and give you the same instance of it back so for example if you had increased the count of a counter to four in some state and you navigated back and you navigated forward and you registered that view model as a singleton you would get that same exact one back now on the other hand a transient is completely opposite what this means is that when you register that object whatever it is and you request it every time you request it you will get a new one back every single time so it's not going to keep the state around that's really good if you're great if you're navigating to pages and you want to pass a different object and state around you're going to want to go ahead and use that transient but i have a good demo here that kind of walks through all the different approaches and some sort of odd edge cases that you may not suspect so let's take a look now i've created an absolutely astonishing looking application look at it it has a button that says navigate now this navigate is a big old button here in a donna maui application and when you click on it it simply goes to the counter page using shell now that counter page it looks like this it has a dotnet bot on it an increase count and a change background button this counter page will come in and it does a few things it simply does a data binding here to the count and then it has a button that's going to call the increase count command and then here is a button that is going to be specific not to the view model where those data bindings are but to the actual page to change the background color so we can see here in the code behind is that when we go in and create the page when a page is created a new instance of it is created the counter view model will be automatically passed down to us now that will be resolved from the dependency injection service based on if it's a single tin or a transient we'll take a look at that now i have a button here that when i click on it changes the color to orange boom and that's cool because that's going to be to the instance of the page now the counter view model that's injected is very simple i'm using the.net community toolkit for mvvm that enables me to create this observable property of count and a relay command of increase count so when i click on that it increases the count and boom boom it totally works now in this instance here what we can see is that i've registered both the counter page and the counter view model as singletons so what this means is if i navigate back and i navigate forward both the page and the view model are kept in state in memory pretty cool now let's say we wanted to change this up let's say we wanted to change this to add transient for the page in this instance what we would expect to have happen is that every single time i go ahead and navigate a new page is created for me but that counter view model is a single tin so it would be the same exact singleton every single time of the view model so if i navigate i'll increase the count to let's say five change the background color navigate back now if i navigate again the page should be new but the view model shouldn't and there we go background color is white and the count is set to five because the view model was already in that global registry in the dependency injection service so the dependency service automatically held on to it for us so there we go if i increase it again go back navigate again seven the same exact view model comes back now let's do the other one here the opposite add transient and transient this is really good when you are navigating to detail pages because you're probably going to have the user is going to click on different pages and they might represent and show different pieces of data for example or we might have a login page that's only shown once in this instance we're going to navigate increase the count change the background color if i navigate back we ideally should see a white background and count set to zero new page new view model boom now here's a tricky one pop question pop trick question what happens if i do a little bit of the opposite here i add the counter page as a single tin but leave the view model as a transient so this means every time i navigate a new counter page would be created for me let's take a look at that i'm going to navigate increase the count change background go back now here since the counter page is a singleton i'll get the same counter page back but what's going to happen am i going to get a new view model or the existing view model um the existing view model now the reason i got the existing view model even though here it is ad transient you have to remember that that counter page was already in memory it was already created or he had instance to that view model so the constructor did not get called again so remember it is when you are requesting from these services from that dependency service when you're requesting something back that's when it's going to determine do i create a new one or do i return that global one back to them so that's the difference there so when should you use what well i personally like to register most things as transients some people are transient everything well it depends if the main part of my application is um tabs or a fly-out menu i like to make those singletons especially the ones that are going to be used over and over again else whenever they navigate around or they do different things if they fall out of memory then it'll have to go off create network connection download stuff back and forth and i don't like that so i register those as singletons now whenever i navigate and it's a new page with different types of information that i'm going to be presenting on it i registered those as transient there's a bunch of cool extension methods that you can create that'll sort of automatically do this stuff there's different ways of really using that dependency service a lot of cool packages out there but take a look at my other videos about how i set this up i'll put this exact demo inside of my github and i'll put links down there at the bottom hope that you found this video helpful of breaking down that singleton versus transient problem uh that you may have when in doubt probably register a singleton um but you know if you are having hundreds and hundreds of pages it may be best to think about do i really need all 100 pages in memory all the time or just the main ones that my users are going to use like i said hope this has been super helpful if you have any questions leave them below but don't forget if you're here for the very first time we're here for the 100th time don't forget to hit that like button it super helps the youtube algorithm of goodness um to recommend this video to others here on youtube and don't forget to jam on that subscribe button because it notifies you every time i put out a video right here on youtube hope you have an amazing day enjoy this video until the next one thanks for watching [Music] you
Info
Channel: James Montemagno
Views: 17,124
Rating: undefined out of 5
Keywords: dependency injection, .net core, asp.net core, dotnet maui, .net maui, .net maui dependency injection, .net dependency injection, blazor, c# dependency injection tutorial, c# dependency injection example, c# dependency injection, dependency injection c#, dependency injection tutorial, dependency injection for beginners, dependency injection .net core, c#, programming, dependency injection container
Id: oC5zpEbwViE
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Thu Jul 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.