Today, I'm gonna talk about how you can streamline validation of your
arguments that are pass into any method by using the guard method
inside of the.net community toolkit.
It makes it crazy simple to get rid of all of those.
If conditional checks and manually throwing of exceptions by using a
simple helper method called guard.
So tune in.
Hey everyone.
I'm James.
And today I'm back talking about the .net community toolkit.
In a previous video, I talked about the M V VM library, uh, which is part of
the .net community toolkit that helps you do source generation of code when
it comes to M V VM and also provides a bunch of different helper methods and
properties to really just make V VM.
Awesome.
I'll put a link to the video up there.
It's probably one of my favorite videos of all time that I've ever made.
It's really, really cool.
And all the feedback on it was really fantastic.
Now today.
I'm gonna talk about some other parts of the.net, community
toolkit and all the things that are in this community toolkit too.
Cause there's a lot of community toolkits and I actually had Sergio
on, on.net recently talking about the lineage in history and where it's
going and how you can get involved.
So definitely check out the.net, YouTube over@youtube.com slash.net.
Let's talk about guard.
All right, let's talk about actually.
Handling errors and different things that are passed into your methods.
Normally, what we need to do is validate to make sure that
we get the correct input often.
This means that we're checking against null, but it may also mean we
want to check against like specific lengths or seeing if integers are
above or below a certain size or like an index, uh, of an array that
we're trying to query on is valid.
Like, is it in the range?
Uh, of course, you know, just checking against.
Empty strings is also another one, too.
If you've ever written any C sharp code or any code in general, you have a bunch
of, if statements you say, if this thing is this thing, then throw an exception.
If this thing is this thing, then throw this thing.
Guarding sort of flips.
The script onto its head and says, let's guard our method and
let's define the valid state.
And if it passes each of these validation methods, continue
on else, throw an exception.
So for example, where we would manually check to see if something
is null and throw an argument, Nu exception, we would say if is not NA.
Then continue.
Right?
So it's kind of the inverse thinking of it, which is really, really neat.
And the guard helper inside of the dining community library helps you do this.
So let's check it out.
So I'm over here inside of my MVVM source generator code that I ran a billion times,
uh, and pretty much what I've done is I've created this method called submit info.
This is directly from the sample with a few modifications, um,
from the do net community toolkit.
Um, but I kind of, it's a great example, right?
Cuz it.
Submits information.
It takes in an integer array.
It has an index that you probably wanna index on and
then a string property as well.
And this is what probably a lot of people's codes look like.
We have array.
If it's null throw a no exception, if it's greater than an equal to
10, throw an argument exception.
If it's not inside the index range, throw an argument out of range exception.
If this string is null or empty, or maybe even as white space.
Throw an exception, right?
Oh my goodness.
And you gotta write all this code and it kind of chains up over and over again.
Now there are some really nice helpers, specifically the bang bang operator
coming in C sharp 11 that enable you to do something like this, which
actually allows you to just put these little two, uh, bangs right behind it.
And that will say, if this thing is null, throw an argument knowledge section.
Now that's great, but it's not gonna handle every scenario for you.
Um, because it's not checking against these other things.
That's where guard comes in.
So what we wanna do is go over to Newgate and we wanna type in community toolkit.
Now, there are a lot of community toolkits and specifically I'm
doing the include pre-release here.
Um, but there's a lot of them in here.
And the community toolkit is a GitHub, a repository over here, which, uh,
and, and, and actually organization.
So if I bring this up over here, it's a bunch of different community toolkits,
like.net, the windows, community toolkit, the dot and Maui one has
all sorts of stuff inside of here.
So you definitely wanna pay attention to this thing.
But what I wanna show over here is that I'm gonna not only install the
M V VM one, which we did previously, I'm gonna come down to this one
community toolkit dot diagnostics, and specifically I'm installing the latest
8.0 version that's in preview right now.
So I'm gonna check this and install it.
Now this happens to be a dot Maui application, but this can be.
Any application, it doesn't matter what version of.net you're targeting.
It could be framework.
It could be Don as standard could Beamin it could be UWP.
It could be dot Maui, anything.
It doesn't matter.
Now that we have it installed, let's come up and at another community.
Directive here.
So we'll say using community toolkit diagnostics, and that's the name
space that gives me everything I need now inside of here, if we go down
here, there's, there's two things.
If I do community toolkit dot diagnostics, there's a guard and a throw helper
and a few other things in here too.
Specifically, we get guard and this is what's gonna enable us to guard.
Our method against invalid input.
So if I say guard dot, I'm gonna zoom in here and just show you
some of the things that it can do.
So for example, we wanna check, can it do something?
Is it greater?
Is it not NA right?
What is the valid state that we want?
So for example, here we have some stream, uh, methods such as can
read, can seek, can write, and if it can do that, it will continue.
If it can't do that.
It will throw an exception.
We have other things like has size equal to is greater than greater than or
equal to less than, um, is a signable of type is that start position is between
is canceled, is close, is completed.
That's awesome.
On a task is completed successfully on a task.
That's really, really cool.
Um, is false is faulted is greater.
What is the valid here?
Look at all these helper methods, and this will automatically throw
the corrupt exception for you under the hood automatically.
And that code has been F.
Finally tuned and optimized, which is really, really cool.
All right.
So let's look at all this.
So there's all these things is not of type his white space is not white space.
It's so cool.
Okay.
So there's all sorts of stuff.
So what do we wanna do here?
Well, we wanna say, if this thing is Nu, then throw an exception.
So what is the valid state of the array?
Well, we can say guard.is not NA all right now, full line in tele sense here from
visual studio, 22 is telling me to fill in array and name of array automatically.
And what this is saying is if this thing is not Nall, it's valid continue on, but
if it is NS, throw an exception, you.
Right.
Takes in a parameter and the specific name that you'd wanna use in the exception.
Now, this thing is really, really smart.
So you can actually just get rid of that.
You don't even need to pass it in.
It'll automatically figure that out for you, which is cool.
So now we take these four lines of code and delete them.
Boom.
All right.
How about array dot length?
Greater.
Or equal to, well, what's the valid state, the invalid state of its 10 or greater.
So the valid state should be, is, or has size less than right,
because it needs to be less than 10.
So here I can say array and 10, just like that.
So is the size of this array less than 10.
And if so, then it's valid and we can go ahead and delete that.
Awesome.
Next one.
Now this is actually checking if index is less than zero, but also if index is
greater than or equal to the array length.
So in this instance, like what is actually could do is we could do a guard and we
could say guard.is greater than, for example, we could say it's greater than
zero or greater than, or equal to zero.
What we're actually doing is checking the range over here.
It's between two values.
So what we can say here is we can say guard.is in range four.
Like, is it in range of the index?
Right?
That's the index and over here can give it the array.
All right.
So this is saying, is the index in range?
Of the array.
Now you can do this on a bunch of different things.
So here we can actually, um, see that there's, um, there's
a in range for, uh, string.
There's a collection, there's a read only collection.
There's a list of tea, memory of tea.
So all sorts of things that you can pass in here, which is really nice.
This will automatically take in that array and sure enough, bam gone.
All right now, this last one, right.
We're checking to see if the text is string is null or empty.
And here I'm gonna say guard is not null or in this case white space.
Right?
Right.
That's one that you can use, or if you do wanna do empty also valid two.
And here I'll just put in text.
So it's up to you.
If you want is not all or white space or not Nu or empty.
So whatever your method calls, here we go.
Okay.
Now let's go ahead and call this thing.
So now what I've done is I've created a little submit buy-in and
the very first time it comes in, it's gonna pass in invalid state.
So we're gonna keep passing invalid state get closer to validation to the very end.
So what I'm gonna do here is the first time is I'm gonna pass in
Noll for the array that should fail this check, cuz it, it is null.
So it's not gonna work.
The next one, I'm gonna pass on a really long integer array, which is gonna be
over the amount of 10 that it needs.
So that'll be invalid.
Then over here, what I'm gonna do is I'm gonna pass in five for the index, which is
not gonna be in the range for the array.
Then I'll pass in one and give it a null for the string.
And then finally, we should be good to go.
All right.
So let's just go ahead and debug this thing.
So we can actually verify this.
I'm actually gonna add a break point point on that count equals five.
So you can really, really see it cuz we're expecting exceptions up until that point.
So here's our application.
All right, there we go.
I'm just gonna hit the submit ticket.
Now the very first time it comes in, it passes it and all, so it
says, array must not be Nall.
Boom.
You're good to go.
The parameter is array.
Figured it out for us automatically.
You can of course give it, you know, better message as well.
And you can handle specific exceptions in your code too, but that is a
nice little error message right there, gen for us automatically.
All right.
Next one here.
We're gonna go ahead and pass in size 11.
Right?
So it needs to be less than 10 for the Enterra size handled automatically.
Next one.
What do you know?
It's not in the range.
It's very, very specific here.
Especially if you're creating a library, you wanna give these
exceptions to your developers.
It literally tell them everything they need to know, including the actual value
here of everything that's inside of it.
Next one.
Over here again.
The null cannot be null.
The string cannot be null or empty, and it was null on that parameter.
And then finally, let's check it out.
I'm gonna submit the info.
Let's actually walk into it as well and walk through.
So there we go.
Past, past, past.
And it passed.
And our application successfully is now up and running and working.
And there you have it.
That is how you can optimize and fine tune your code and delete all those.
If checks all over the place and use guard, part of the.net community
toolkit diagnostics package, and there's tons more in there to help you write
even more ingrain tune, optimized code, hope you did enjoy this video.
I highly recommend you check out the community toolkit.
Organization over on GitHub, take a look at all the great community
toolkit, libraries and packages and bundles that are being partnered between
Microsoft employees and the community.
It's really cool to see those things come together.
Big shout outs to Sergio for coming onto the on.net show, talking
about all this stuff and actually listening to me and, and, and.
Taking some of my suggestions at heart and talking through stuff, it's fun
to engage with the team on GitHub too.
I'm really excited to start using these packages and my applications.
And I hope that you find it, you know, useful as well.
If you do, let me know down in the comments below, if this is something
that you're interested in, or if you've, uh, already been using something similar
to, I'd love to there's other libraries out there that are even helping you
optimize that validation as well.
I'm gonna come back with even more.
Samples from the .net maui community toolkit, including actual validation
of fields and properties and some more of the performance things that they
have as well to go deep, dive on all these hope that you enjoy this video.
If you did give it a thumbs up down there, if you made it this far, it
really helps out the channel and this video and helps other people find it.
And you can actually help 'em by sharing it.
Just it share, drop that in your teams and your slack.
Just share it out on Twitter.
I would really appreciate that.
And if you're new here, I put out videos every single week
and you don't wanna miss them.
So jam on that subscribe button over there and ring that notification bell.
So you get up to date whenever I put out new episodes, I'm James.
I really appreciate you tuning in and thanks for watching.