Even More MVVM Source Generator Awesomeness for .NET Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
stop writing mvvm code and let the community toolkit generate it all for you I've talked about it before in previous videos but mvvm Source generators are absolutely amazing and I've learned a lot since my original video and there's a new version that introduces some brand new features that I'm going to break down for you today so tune in [Music] thank you [Music] hey everyone I'm James and I'm back again talking about mvvm storage generators part of the.net community toolkit I've talked about them in previous videos at dot net comp Sergio broke down everything new in version 8.1 but I've learned a lot since my original videos I've been using them and tons of my applications and there are just a plethora of new features that I want to break down for you that maybe I've actually been around for a while but I didn't include in my video because I did know they exist and then Sergio told me they exist and I want to let you know about all the awesome features because the more I use this toolkit the more I absolutely love it so let's go and break it all down all right first things first over here this is a done in Maui application but this community toolkit works with pretty much anything I have a dotted Maui app I have a xamarin forms example I have a WPF example I have a wpf.net framework sample now you do need to put your code in a net standard library and and go into your Cs proj and you know update your langver over here to at least be C sharp I think eight or nine or something like that the tools will tell you but everything is here for you automatically now let me go ahead and run this application really quick this is my mbvm source generator sample that I use as an example in my previous video it's a very simple application over here that has a first name field James and the last name field montemagno here I can click the submit button and it goes and it parses some data it waits a little bit and then it will pop up a dialog box using the guard methods in the community toolkit now if we look at the source code here in the main view model we're going to notice a few things first I'm using these cool attributes that are coming from the mvvm source generators we can see here there's the community toolkit stuff down here I have an observable property a first name and another one of last name and note that I have another public property here that is concatenating first name and last name and we've added this notify property changed for both both of these properties and then down here I have a simple method and it has this relay command on top now previously this was I command and also notify for but in the final version of 8.0 they change the names here and I've updated my example so you can now see them now the cool part here is that this doesn't really look like normal property changed events and also like how is I notify property change getting implemented here well that's all because of source generators those attributes are Source generating behind the scenes if I go into my analyzers I can look at the mvvm source generators down over into the I notify property change we're going to notice that this is a partial class and all of this code has been generated for me automatically a little more so easily enough is the property changed events now this is Source generated code so it looks a little complex but what we see is it generates first name it gives us our get and our sets and it optimizes the code and raises those property changed events for first name and full name and we can see it here last name and full name as well so that's really really cool now a few things that I will also note that I didn't know existed before is sometimes you might want to do things when things change and there are partial methods to do that if you look at the source generated code they're right there so this is actually really neat so let's say for example you needed to not just go in and execute notify property changed event like we can do with the source generator maybe you needed to execute some other code for example whenever that property changed you can do partial and here if I zoom in you'll see that for every property you get an on first name on last name changed and changing event so here if on first name changed I could go in and I could go ahead and I don't know like you know display an alert so let's grab an alert alert notification down here and then just say you know changed I don't know for some reason and we'll say uh values I'll put the value right so now just like that I've taken this and I've said whenever first name changed of course execute the value changed event but then also raise this alert dialog so let's go and make this smaller and I'll say D boom just like that look how fast it was basically it now has notified that up and it's going to be a bad experience there but that's really neat in case you need to do something and some people have noted that additionally that sometimes there are multiple attributes and those Stack Up wouldn't it be nice to have those attributes on one line sure just like put your attributes on one line that works as well that will generate the same source code whatever you like fully up to you um this works really great so you can just add those attributes there and handle everything for you I like them on separate lines that's my opinion all right the other one down here is something that has been built in since 8.0 which is the relay command this was I command previously and now it's relay command this will handle synchronous or asynchronous tasks and we can see that this is basically going through awaiting and parsing some data so sort of mimicking a network call now if we look over at the source generators what this has done for us is it's implemented this submit and it's created a submit command and this is of type I async relay command and we can see that it's implemented in async relay command now inside of here what's really neat is it gives you additional properties and ways of canceling and getting Insight if that command is running so normally what I would do inside of here is I'd have something like a you know a bull is busy and then I would make this an observable property and then what I would do down here is I would say like you know it is busy equals true right now make that a you know is busy of course and then I'd say you know false basically something like that right so a little is busy true or false and then I go back over to my UI and I'd adjust that back and forth uh based if I want to do a little spin spinner or something like that well you don't need to do that because there's a bunch of stuff built in to the relay command so let's get rid of this stuff because we don't need it and I'm going to go into my main page here I have an activity indicator but it's not running it doesn't know when to run doesn't know when it is busy so what we can do is I can say is running I'm going to do a binding here two specifically the submit command Dot and what I want to show you here we zoom in is that there is a can be canceled execution task is cancellation requested and is running so I could bind is running and is visible to the binding submit command dot is running just like that let's go ahead and run this one more time and we should see for about that five second when that submit command is executed that it goes ahead and runs so let's go ahead and do this here I'm going to type in my James montemagno click submit and just like that our activity activity indicator is running it's mind-boggling it's like so unbelievably cool and here we get our guards and we can see that that's gone off and running and boom just like that it's done so that's really neat because no matter what if there's an exception there's something else when this thing is done you don't need to add extra code into it so that is really really cool one less variable one less thing because the command knows if it's in the execution block automatically that's pretty unbelievably cool all right so those are some of the new features that I've noticed as I've been going through this I've also noted here that while I do come in and I add this attribute of notify property changed there is a built-in class that you can inherit from called observable object an observable object if I go over here we can see that this is coming from the community toolkit and it implements I notify properties changed and notify property changing so up to you if you're able to inherit from a base object then go for it else just go ahead and add that attribute up top and your code will be Source generated for you so either way there really really nice all right let's talk about new features now inside of 8.1 we're in preview one so I got preview in here so it may change over time I don't want to say anything um in case they change or attribute names but they have done tons of performance improvements and under other optimizations to all of the generated code so just for free by upgrading you'll get optimizations but they've also added the ability to set different Json properties and Json attributes now since the code is generated for us there previously was no way if we were serializing that or deserializing that to use different Json properties so that's kind of a bummer right if you're using the system text Json stuff you want to add those attributes on top of it to say this is required or you know don't use first name use name or use FN for example but now you can do that so specifically if you have version 8.1 preview one you can now add another attribute on top of this so I can go in and I can use property this is a really neat thing they did and Sergio breaks down everything in more detail around how why they pick these things in the release notes and in the dominant com video but I can say Json and I can use any of these Json properties from system text Json serialization so I could say required for example or I could say property and I could say Json property name and I could say let's say FN for some reason right maybe that's what it's being serialized to and I could do that same thing maybe last name is not required but that is Ln now when I go back over to my source generated code let's go ahead and look at the observable properties here there we go it will automatically append on those specific attributes for you automatically so because we applied it to that specific property we now get the Json required attribute and the Json property name attribute as well which is super duper cool and we can see down here we also get Ln on our property name attribute there are so many amazing features in the community toolkit that's just one of them the.net Community toolkit has a bunch of things including Diagnostics including uh performance things there's also a.net Maui Community toolkit there's a Windows Community toolkit there's so many amazing things I will link to the community toolkit GitHub and of course I'll link to my sample project right here so you can pull it down see how it works in down in Maui in WPF and framework applications and xamarin forms applications and so much more if you have any questions leave them down there if you like this video at all you're using all these new source generators give this video a thumbs up boom hit that subscribe button share it with your friends for all of these goodies but I just want to thank you so much for all of the amazing feedback on the previous video on the workshop and every time we show this thing off I absolutely love it and I know that you're gonna love using these new features if you have other things that you found that maybe I didn't cover yet put them in the comments below love to hear from you thanks for watching foreign [Music]
Info
Channel: James Montemagno
Views: 24,529
Rating: undefined out of 5
Keywords: .net maui, .net, c#, mvvm, source generators, mvvm tutorial, .net community toolkit, community toolkit, dotnet mvvm, dotent maui mvvm, .net maui tutorial, .net maui example, mvvm tutorial for beginners, dotnet maui, .net maui community, learn .net maui, xamarin forms, maui tutorial, .net maui mvvm, tim corey, c# tutorial, dotnet maui mvvm, microsoft maui, xaml data binding, mvvm wpf, c# mvvm, wpf, mvvm programming, mvvm toolkit, wpf mvvm, mvvm c#, c# mvvm framework
Id: 9vvm_-YveTs
Channel Id: undefined
Length: 12min 54sec (774 seconds)
Published: Thu Dec 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.