All right, I'm getting it into the third option of .NET
dependency, injection scoped.
What the heck is scoped?
When should you use it and why should you use it?
So tune in.
Hey everyone.
I'm James.
And I'm back with another video again, talking about .net dependency injection.
I can't get out of this cycle.
I love it.
Cuz I've been building a lot of applications with
blazer and with.net Maui.
And I love the built in dependency injection service.
I just did a video about dependency injection.
And then I did another one about singletons versus transients,
which is how you register your dependencies when you wanna use them.
And then some of you ask James.
What about that other thing in there that you skipped over completely scoped.
Okay.
Well, scoped specifically is all about registering services
with a scoped lifespan.
And this is usually the lifetime of a single request.
Now, this is important when you're doing web application development
and I skipped over it cuz I've been in the client space for so long.
There's only really one single request.
It's your app.
So what I wanna do is actually show you when to use scoped based on
your application needs and how it compares to Singleton and transient.
So let's go ahead and get into it.
So I've created a brand new blazer server application right
here, and it says, hello world.
And there's a counter, you click on it and it increases the clicks.
Look at that.
It just does it over and over and over and over again now.
If we look at the code here for this, we can see that I've actually created the
same exact file, new project blazer, but I've added this blazer counter service.
So normally it just increases the count on this page and then it goes ahead and
it, um, will kind of dispose of that data when you navigate back and forth.
So it's stateless, um, But the counter service could actually provide some state.
So here I'm doing a binding to this counter service dot count.
And then whenever I click on the button, I simply say counter service increase.
Now that counter service is very simple.
It has a account.
There's an increased method that increases the count.
So nothing special there.
All right.
Very, very simple.
Now in the program, I've registered it alongside my weather service
and we have counter service.
So if I bring back up that application, what we're gonna see is if I go
to another page, come back here.
I still have my count.
Okay.
Cuz I'm, I'm going to that page.
The Singleton is serving it up.
All right.
If I open another page, we can see that 25 is still there.
And if I click on it, sure enough, it increases.
Now the other page, doesn't actually refresh, cuz it's not getting notified.
I'd have to do some magic there, but if I go back and forth, sure enough,
that single tin is returning the same exact counter service from the server.
All right.
So that's coming from blazer server there.
So let's go ahead and change this though, to add transi.
Now transient, if you remember, will serve up a new instance of that
service every single time I request it.
So if I go in into my counter and I increase increased
click, go away, come back.
As you can see, it's reset.
So that's pretty cool.
It's kind of the same as it being inside of that page itself.
So that transient, right?
Every single time that page comes.
Here it is.
It's gone.
If I open up another page.
Sure enough.
I'm gonna get another counter service that go ahead.
And they're not connected in any way.
Right?
If I refresh this, it's back to zero.
So that transient again is requesting it back automatically.
All right, here we go.
Scoped, this one is different, right?
So that one was like the shared global state, this one transient
being nude up fresh every time.
All right, let's go ahead and change this now to add scoped.
There we go.
Now, if we go ahead and debug this, we're gonna get a little
bit different functionality, which I think is pretty cool.
So here we go.
I'm gonna come in, go to the counter.
I'm gonna go ahead and click up to 10.
If I go to home, back to counter, it's still 10.
Because it's this single instance, this request that I've put in,
I'm like a user to this, this instance of the blazer application.
Now, if I go and open another tab, look at this it's zero.
If I go back and forth, if I refresh it's empty, right?
If I go to 10, go here back and forth.
It's still five back and forth at seven back and forth.
It's 10.
So it's keeping it per the tab.
And of course, if I refresh, it goes away.
There we go.
Super duper simple.
Right?
You got Singleton shared everywhere between all the tabs between
transitioning, even refreshing.
You got transient, which is sending you a new one every single time.
Whenever you request it, even between tabs, when you refresh or in another
tab, and then you have scoped, which is gonna be for that instance of the
page when the user navigates to it.
And then of course, blazer is serving that service up every single time.
Whoa, it's James from the future.
Okay.
Well, not really from the future, but I have a different t-shirt on
and additionally, I have no lights.
Does this look better?
Maybe it looks better.
I think I might look better without any lights on.
Let me know when the comments below anyways, I'm about to tell you
basically, in this video about how.
Nothing matters for client development with Don M Maui, but I'm totally wrong.
Cause I actually put out this video and then Dan from the prism team
was like, Hey, no, actually it does matter in certain instances.
And then I checked with the Don M Maui team and I was totally wrong.
So now I'm gonna correct her right now.
Put out this video again because .NET Maui will use scope dependencies
when you're working with windows.
So multiple windows, not the windows operating system.
Multiple windows on desktop or on tablets or things like that.
Now I often don't do this and haven't really explored it too much.
So that's why I made the mistake.
But, um, just be known that if you are doing that, that will be different scopes.
So you can have multiple scopes kind of makes sense or different windows.
Duh, James.
Anyways, that's a cool update.
Dan also said that if you're using prism, they actually have a way of
doing scoped for specific pages too, which is also very fascinating, but
the core just on Maui stuff there for multiple windows, it's there.
So sometimes, you know, we make mistakes and we get to re-edit.
I love it.
And look at this out.
We get to check it out and it's a whole new day gonna go edit this, put it out.
Hope you like this video.
Now back to other James, who's about to tell you maybe other information about
cool stuff and let's wrap up this video.
Again, thanks for watching.
Let's head back to Jane.
Now, again, if you're doing mobile and desktop application, this isn't really
gonna matter because scope, there is one scope, it's the app itself, right?
So you really wanna focus between Singleton or transient for your app,
but if you are coming from the web world, hopefully this was informative.
If it was jam that like button down over there, it goes into
the YouTube algorithm of.
And if you have any questions about this stuff, I'll leave
it in the comments below.
If you like the video, that also goes into the YouTube algorithm of goodness as well.
And of course, don't forget to subscribe for more videos here.
I love talking about dependency injection.
I love talking about .NET Maui.
I love talking about blazer.
If you wanna see more specific topics, I'll let me low, no below, or hit me
up at Twitter at James Monte Magno.
Thank you all for watching.
Thanks for the questions.
Thanks for giving me content.
I love this stuff.
And until next time, thanks for watching.