MVVM Source Generators: Never Write MVVM Boilerplate Code Again!

Sdílet
Vložit
  • čas přidán 3. 08. 2024
  • Imagine a world where you never had to raise another property changed event or write another IComand. Well, those days are here with the new .NET Community Toolkit, which introduces brand new source generators and helper classes to enable you to write super minimal code. I break down everything you need to know about the toolkit, how to use it, and why it is awesome!
    There have been several updates to the library and name changes for the properties, so check out github.com/CommunityToolkit/d... for changes
    Links:
    Source Code - github.com/jamesmontemagno/MV...
    Toolkit Blog - devblogs.microsoft.com/dotnet...
    Documentation - docs.microsoft.com/dotnet/com...
    Community Toolkit on GitHub - github.com/CommunityToolkit/d...
    Join this channel to get access to perks:
    / @jamesmontemagno
    👕 Buy some swag! - jamesmontemagno.myspreadshop....
    ☕️ Buy me a coffee - www.buymeacoffee.com/jamesmon...
    Follow:
    👨‍💻 GitHub: github.com/jamesmontemagno
    🦜 Twitter: / jamesmontemagno
    📄 Website: www.montemagno.com
    📰 Newsletter: newsletter.montemagno.com/
    Chapters:
    00:00 - Introduction to MVVM
    06:20 - Installing .NET Community Toolkit and INotifyPropertyChanged
    10:20 - ObservableObject and property changes
    12:00 - ObservableProperty source generator
    14:35 - ICommand source generator
    17:00 - Source Generator Wrap-up
    Disclaimer: This channel, videos, and streams are created in my spare time and are a product of me... James Montemagno! They are NOT officially affiliated or endorsed by Microsoft (my employer) in any way. Opinions and views are my own.
    What is on my hat? It is the CLE clothing logo because I am from Cleveland! Checkout their awesome CLE merch: cleclothingco.myshopify.com/
    What is that art on my wall? It is an original piece from the French street artist Gregos of La Butte Montmartre: / bcez1onhiqx
    My Setup:
    💡 Nanoleaf Elements Lights - amzn.to/3umwJVW
    🎙 Blue Spark Microphone - amzn.to/3qgtYkq
    🎙 Blue Pop Filter - amzn.to/3jEWM3r
    🤳 Rode Microphone Arm - amzn.to/2Z68AlE
    🎧 Sony MDR7306 Headphones - amzn.to/372jxta
    📲 Stream Deck - amzn.to/373Uk1n
    📹 Elgato Cam Link - amzn.to/3a9eGbh
    📷 GoPro Hero - amzn.to/374lm90
    🖱 MX Master 2S Mouse - amzn.to/3d7J2gj
    ⌨️ Tecware Phantom Keyboard - amzn.to/3aUP4y9
    #dotnet #mvvm #sourcegenerators
  • Věda a technologie

Komentáře • 290

  • @JamesMontemagno
    @JamesMontemagno  Před 2 lety +16

    There have been several updates to the library and name changes for the properties, so check out github.com/CommunityToolkit/dotnet/releases/tag/v8.0.0 for changes

    • @mikp1280
      @mikp1280 Před 2 lety

      This comment saved my day :) Thanks James!

    • @meilleur102
      @meilleur102 Před 2 lety

      Hi! Idk if you can help me on one thing. When I create a [ObservableProperty] of anything (string, ObservableCollection etc) the binding work perfectly.
      When I create a [ObservableProperty ] of a Model, i can't get the ui to update...I really don't get it.
      When I debug my Model.Properties get updated, but the ui dont. I try UpdateSourceTrigger=PropertyChanged, OneWay, TwoWay etc on the view. Nothing works.
      Thanks

    • @paulegan3783
      @paulegan3783 Před 2 lety

      Cheers!

  • @LeeM0to
    @LeeM0to Před 2 lety +1

    Wow... Game changer. Kudos to all involved. Can't wait to start using it. Thanks for the video James 👍

  • @stevehidem816
    @stevehidem816 Před 2 lety +3

    Shout out to James! You continue to help me up my game. This MVVM source generator is awesome and I look forward to more updates on recent developments.

  • @JesseLiberty
    @JesseLiberty Před 2 lety +2

    Great, great, great. And as usual, brilliantly described. Nice work James & team.

  • @georgeleithead
    @georgeleithead Před 2 lety +15

    Makes the code so much cleaner, and easier to understand for others.

  • @comediehero
    @comediehero Před 2 lety +37

    Wow, finally something that fixes my biggest gripe with xaml MVVM development. Thx James!

  • @ByronBennett
    @ByronBennett Před 2 lety +6

    This is beyond awesome! Super easy to implement. Makes your code soooo lean and easy to read. I love how you can use the code generation where you want to and still go the long route if you need to do stuff in your Get or Set. Excellent code! And a great video showing how to use it!

  • @enzodiaz3921
    @enzodiaz3921 Před 2 lety +1

    You said it: it's ridiculously awesome. I've been working with WINUI 3 and i can see some differences that could be tricky for a newbie like me, but in the road we learn. Thanks James.

  • @e2a2j
    @e2a2j Před 2 lety +1

    MIND BLOWN! I love this with a passion. This helps reduce stupid errors both in initial creation and going back and modifying. This is awesome. I've used code generators in the past and I love them, but this is next level. Thanks again James!

  • @Agredo10
    @Agredo10 Před 2 lety +1

    Wow simply wow! The MCT.MVVM was already very helpful but this is incredible!

  • @Jgathairu
    @Jgathairu Před rokem +1

    Thanks James! Amazing and easily broken down. Definitely applying this

  • @jontemplin6990
    @jontemplin6990 Před 2 lety +19

    This is so awesome! One of the hurdles of understanding and effectively using MVVM was getting your head around all the boilerplate plumbing code and understanding why it's needed. I always thought to myself "why can I just specify properties and commands and let the compiler do all the ceremony?" Yay! It finally happened!

  • @collinsk8754
    @collinsk8754 Před 2 lety +1

    Really great addition to the community toolkit! Thanks a lot for the tutorial.

  • @twitterer
    @twitterer Před 2 lety

    Love to see the excitement in your own expressions, tells you how great it is !

  • @mrrob1454
    @mrrob1454 Před 2 lety +5

    Amazing. This just made WPF applications 100 times easier. Thanks for the great video.

  • @malkywullur8683
    @malkywullur8683 Před 2 lety +1

    Yes, this would be my 1st personal choice among others mmvm library out there !! Thanks a bunch

  • @MaximilienNoal
    @MaximilienNoal Před 2 lety +1

    I was stuck with mvvm since 2010. Now, I finally love it.

  • @ghanimalmarzouqi3277
    @ghanimalmarzouqi3277 Před 2 lety +1

    Many thanks to you James. This is really helpful and blowing my mind too 🤯🤯🤩.

  • @TonyHenrique
    @TonyHenrique Před 2 lety +1

    This is awesome! Very clever implementation.

  • @kosteash
    @kosteash Před 2 lety

    wow James, you are amazing. You showed my so many cool nugets, that make my life easier. Thank you very much !

  • @jimsimson7266
    @jimsimson7266 Před 2 lety

    My mind IS blown James! Thank you!

  • @munver19
    @munver19 Před rokem +1

    Great tutorial, Thanks James.

  • @danielblakeley2995
    @danielblakeley2995 Před rokem

    The best MVVM content creator ever thanks james.

  • @hrishikeshgarud7177
    @hrishikeshgarud7177 Před 2 lety

    Very useful. I was doing everything code behind. Now i will try this.

  • @anandshindey
    @anandshindey Před 2 lety

    This is awesome. Thanks James.

  • @RaabStephan
    @RaabStephan Před 2 lety

    This is honestly pretty amazing!

  • @LordErnie
    @LordErnie Před 2 lety +2

    Yet again James is here to save the day for devs and students, thanks man.

  • @zappledd6598
    @zappledd6598 Před rokem +1

    Top notch! Nice work.

  • @paulegan3783
    @paulegan3783 Před 2 lety

    Thanks so much James, very nice

  • @anderson.damasio
    @anderson.damasio Před 2 lety

    very good this! James

  • @astrowalker2629
    @astrowalker2629 Před rokem

    Awesome, thank you very much. Now I am on the hunt for some good tutorials how to create such generators by myself 🙂.

  • @zandyaverhoff9554
    @zandyaverhoff9554 Před 2 lety

    wow, how many things are change. Thank jame

  • @matk5087
    @matk5087 Před 2 lety +1

    Such a great tool :) thx for sharing!!!!

  • @ericblankenburg
    @ericblankenburg Před 2 lety +1

    This is really great. Thanks for posting.

  • @Otonium
    @Otonium Před 2 lety +1

    Wow, at last all that tons of lines went away... Keep those in just (a little few) attributes (not towers) thogh
    Maybe with a comma we'll avoid attributes towers. Great video!😎 James.

  • @Albahadly
    @Albahadly Před 2 lety

    Thanks James, you really treasure

  • @PticostaricaGS
    @PticostaricaGS Před 2 lety

    Excellent video, and great topic, there is still a lack of video-content on Source Generators

  • @suchtendolp644
    @suchtendolp644 Před 2 lety

    You changed my life, bro.

  • @mashrulhaque1168
    @mashrulhaque1168 Před 2 lety

    Thank you so much James

  • @kashifenet1983
    @kashifenet1983 Před 2 lety

    Cool that is too much life will be easy after using this Thank you JM

  • @origamiiekiller
    @origamiiekiller Před 2 lety +3

    Hi James,
    Great content and thank you for bringing this to my attention, seems to work perfectly in your demo.
    Small question: You had an example with the asynchronous command. Will the binding still work if I postfix the name of the method to 'SubmitAsync'? Or does the generated binding only work, because the name is 'Submit' for the method and 'SubmitCommand' in the Xaml?

  • @MarekLS60
    @MarekLS60 Před rokem +1

    Great tutorial !!!

  • @L-E-son
    @L-E-son Před 2 lety

    VERY cool! Thanks for sharing!

  • @marcelfischer5139
    @marcelfischer5139 Před 2 lety +1

    James, thank you for this video. I wrote so much code over the last years, that I can now delete. - Just a joke. I definitely check this out!

  • @emmanueladebiyi2109
    @emmanueladebiyi2109 Před 2 lety

    Truly an awesome library

  • @kwesiboateng2646
    @kwesiboateng2646 Před 2 lety

    My mind was blown james, when you decorated the private fields with ObservableProperty

  • @mubarekabdi
    @mubarekabdi Před 2 lety

    Super useful ! Thanks 👍

  • @MarcJennings
    @MarcJennings Před 2 lety +1

    Game changer! Can't wait to get stuck in!

  • @billashton6511
    @billashton6511 Před 2 lety

    Looks like the future to me James..!

  • @azizjel
    @azizjel Před 2 lety

    Listening to James makes me think of those old grow hair potion salesmen:
    " Well James, you wrote the ISync command, now What if you want to sink the Submit button and make it sing the song?"
    James:"Well you just need to carry the CommunityDev boilerplate which comes with the Generator..".
    Thanks for the the very interesting presentation James though! I like it 🙃😉

  • @chandra_arifin
    @chandra_arifin Před 2 lety

    Awesome... Like it so much. 😍😍😍👍👍👍

  • @joehanna_
    @joehanna_ Před 2 lety +1

    Sweet! I use base classes to do some of that but it is not a lovely as using the attributes with the generators. The new code feels right and clean. Chunky boilerplate code has always felt dirty

  • @Gazcan
    @Gazcan Před rokem

    This is so good, that it makes me wonder why this wasn't there with the release of WPF.

  • @ferdinandcastilho
    @ferdinandcastilho Před 2 lety

    Great!! Thanks for sharing

  • @ali_jb94
    @ali_jb94 Před 6 měsíci

    Fantastic video,!

  • @abba5102
    @abba5102 Před 2 lety

    Waw that's awesome thank you sir.

  • @isax55
    @isax55 Před 2 lety +2

    Hooo, I need to update my projects right now!!

  • @cristisandu678
    @cristisandu678 Před 2 lety

    Wow, it is very useful. Thx a lot for the tips 👏 Can you make a video for API generator ?

  • @shrikantthorve5701
    @shrikantthorve5701 Před 2 lety +2

    Thank you James for really nice video..
    One suggestion, could you please create video on DeepLinking where application can launch from url like Amazon...

  • @Proto61
    @Proto61 Před 2 lety

    Thank you for the awesome video, could you make a video how to use Messengers with the toolkit? And a video for navigation would be also very nice. Thank you!

  • @scara1701
    @scara1701 Před 2 lety

    This is awesome!

  • @ababacarcisse7969
    @ababacarcisse7969 Před 2 lety

    I like your plugin Media❤️

  • @felipeapj
    @felipeapj Před 2 lety

    I wish a can give more than just a thumbs up, thats awesome!!!

  • @rxtx3948
    @rxtx3948 Před 2 lety

    Stunning beautiful

  • @Sajgoniarz
    @Sajgoniarz Před rokem

    Amazing stuff, thanks You very much! I'm writing a simple app for my team and i just can't wrap my head around how inefficient to write is WPF.

  • @zappledd6598
    @zappledd6598 Před rokem

    Hi James, I love your tutorials. I've been diving a bit deeper into the MMVM Toolkit as I steadily replace my old MVVMLight code. One area not covered in your video that I'd like some more insight into is the ICommand "RaiseCanExecuteChanged". I'm seeing things like [NotifyCanExecuteChangedFor] in the new Toolkit that looks promising. Do you have a video that covers this or can you point me somewhere that might? Thanks so much.

  • @tarsisf8173
    @tarsisf8173 Před 2 lety

    Fantastic!!!

  • @sammynimmy
    @sammynimmy Před rokem

    Excellent!

  • @JoseSilva-gt6zj
    @JoseSilva-gt6zj Před 2 lety +2

    Awesome explanation, James! Thank you! Let me ask about obfuscation. Do you know if it works after obfuscating the code? I mean for a WPF app. Thanks!

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety +1

      Yes it does from my understanding, it is the same as if you wrote the code ;)

  • @anonub
    @anonub Před rokem +1

    Awesome 😊

  • @nixnemo
    @nixnemo Před 2 lety

    Looks/sounds amazing. Many thanks for the efforts. If [ObservableProperty] attribute would work without compilation error, telling MyViewModel already exists, I would really appreciate. Many thanks in advance. Again, really appreciate what you done.

  • @marufahmad4385
    @marufahmad4385 Před 2 lety +1

    Thanks for the great content. Can you please also make a video on MVVM Messaging?

  • @belmiris1371
    @belmiris1371 Před 2 lety +4

    It's a great idea. Since I'm a WinForms guy I keep sliding back to it because it's so simple and I can write stuff fast and clean. I tried MVVM again on my latest Xamarin application (since WinForms was not an option) but still ended up using mostly code behind because I found all the MVVM stuff just too hard to keep track of and getting it to work cleanly with the UI was just too daunting. If these people can make it less complicated that would be great. I DID find the code it generated very scary looking though. If I get some bizarre error about eyepoke() not properly initialized or the my app just hanging or disappearing I would probably give up and go simple again.

  • @buddysnackit1758
    @buddysnackit1758 Před 2 lety

    I actually made a post about "Why doesn't a system that generates the MVVM code for you!" in one of your older videos! Yes!

  • @user-gq8of6kd4r
    @user-gq8of6kd4r Před 2 lety +1

    OMG this cool!

  • @jhanifkara8552
    @jhanifkara8552 Před 2 lety

    INCREDIBLE 🙈

  • @albertorodriguez7096
    @albertorodriguez7096 Před 2 lety

    Amazing!

  • @MohitKesarwaniPhoton
    @MohitKesarwaniPhoton Před 2 lety +1

    Amazing that is what I wanted

  • @mojoojoseph8145
    @mojoojoseph8145 Před 2 lety

    This is so cool 😎

  • @fifty6737
    @fifty6737 Před 2 lety +2

    MVVM is now practical with this

  • @billymartin6497
    @billymartin6497 Před 2 lety +2

    Great video, as usual, James. Couple quick questions: How do we pass command parameters? Does it function inside of VS 2019? Sometimes we have to drop back to use the MultiLingual extension, since it's not in VS 2022.

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety

      I believe they should, but I only have 2022 on my machines now

    • @Sergio0694
      @Sergio0694 Před 2 lety

      You can pass command parameter through whatever system the UI framework you're using has, that's separate from the command implementations the MVVM Toolkit has (for instance, on WPF/UWP/WinUI you'd do CommandParameter="{Binding ...}", or {x:Bind ...}, etc.). As for VS2019, no the generators will not work there. For the 8.0.0 release of the MVVM Toolkit I've rewritten all of them to be incremental generators, and that feature needs Roslyn 4.x to work, which isn't available on VS2019. You'll need VS2022 for that (or another modern IDE of your choice, eg. Rider). Hope this helps!

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety

      Checkout github.com/CommunityToolkit/dotnet/issues/158 for a working sample, You will need to put things in a .NET Standard library for it to fully work.

  • @CricketThomas
    @CricketThomas Před 2 lety +1

    This is sweet.

  • @SanzRealtyGroup
    @SanzRealtyGroup Před 2 lety

    Awesome!

  • @NehaSingh-fe7nu
    @NehaSingh-fe7nu Před 2 lety

    Hi James, I am building a poc blazor hybrid application using .NET Maui in VS Preview 2022, and I am using razor pages for the UI. I am using INotifyPropertyChanged, but somehow it doesn't seem to work, anything you might know about this? Any help appreciated, thanks!

  • @broadshare
    @broadshare Před 2 lety

    Good stuff, Maui has really delayed. We need to use all these stuff on production

  • @ramigamal6637
    @ramigamal6637 Před rokem +1

    An amazing tool I am using it for about a year -_-

  • @WilliamCarinci
    @WilliamCarinci Před rokem

    In using the MVVM pattern, how do you go about handling Navigation.PushModalAsync when it often expects a parameter of type Page? In particular, this seems to be a concern of mine when handling a page NOT defined as a shell page. For instance, a settings modal. It seems peculiar that you get caught in an issue where the .xaml.cs of a page gets set with the BindingContext = viewModel.

  • @mohamadsoltani1982
    @mohamadsoltani1982 Před 2 lety

    it was amazing

  • @WarBorg
    @WarBorg Před 2 lety

    hey James, awesome video as always, one question sits on my mind, how does this work with the xamarin community toolkit and the future MAUI community toolkit, as I see some overlapping bits, thx!

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety +1

      So they are all on the same GitHub organization now and are aligned. The genera .NET one will focus on full compatibility with all .NET apps and then the individual one will be specific to the frameworks and they are working on minimizing any overlap

    • @WarBorg
      @WarBorg Před 2 lety

      @@JamesMontemagno awesome thx for the answer, I really like the code generated stuff but I would like to use the xamarin / Maui community toolkit as well and not have to worry about using the wrong namespace :D

  • @sauravkumar2464
    @sauravkumar2464 Před 2 lety

    great stuff for MVVM , one questions i am having like if we have more than 1 button how can we distinguish with this? Whether name of method will be same as ICommand mentioned in Xaml?

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety

      It is the name of the method and then they add Command. Checkout the source code sample. You can have as many ICommand as you need

  • @anmolbehl8572
    @anmolbehl8572 Před 2 lety

    Hi James,
    I want to add some logic within my properties.
    Example:
    For a particular entry for Age from the UI we can set a property if the person is eligible to vote or not and display it on the UI.
    Can you suggest a way how this would be implemented using CommunityToolkit.Mvvm?
    Best Regards

  • @kamertonaudiophileplayer847

    I am with you. I created a source generator for MVVM 7 years ago, and since then I stopped writing a lot of a code. Everything is done by the source generator. Continue to propagate the idea.

    • @ostelaymetaule
      @ostelaymetaule Před 2 lety +1

      How many juniour developers could understand and extend on your code without wondering about black magic and stuff? D: I am very very cautious every time I see some work being done in background by some sort of extensions or dynamic code generation or self invented conventions nobody else in the world knows about. One thing is to rely on ms own attributes and conventions with tons of documentation and tests and usage and another is thinking that I am able to create, support and fix this on my own and that when I quit the job the next guy will know what the hell is happening in the code

    • @kamertonaudiophileplayer847
      @kamertonaudiophileplayer847 Před 2 lety +1

      @@ostelaymetaule It is a very good concern and actually I had a fear that simply developers will run away. But to my surprise, a feedback was completely opposite. They gladly started using it. Why? If system is designed well, it is so natural and intuitive, developers on any level, especially juniors, just use it.

    • @ostelaymetaule
      @ostelaymetaule Před 2 lety +1

      @@kamertonaudiophileplayer847 I am glad to hear you managed to make it work, I have seen couple examples earlier in my career as well, but also encountered some over engineered magic bazoongas nobody knows what it does and the original dev is long gone ;)

    • @kamertonaudiophileplayer847
      @kamertonaudiophileplayer847 Před 2 lety

      @@ostelaymetaule It is normal, we lost several good developers exactly for the reason. Some system for some reason wasn't designed by us well. The result was really disappointing, we started loosing people because they simply can't use the system. Solution? Do not be afraid of to redesign the system instead of trying to fix it little by little.

  • @miguelmunozluman4732
    @miguelmunozluman4732 Před 2 lety

    Sorcery! jajaja what amazing change!

  • @MrMmajeed
    @MrMmajeed Před 2 lety

    Holy Molly!!!

  • @xmichael911
    @xmichael911 Před 11 měsíci

    Thanks

  • @jewersp
    @jewersp Před 2 lety

    Hi James. This is pretty awesome. The pattern with the if(SetProperty(...)) { ... } is not new to me, I've been using that already for a while now since MvvmLight allowed me to do the same thing, except for the code generation part of it. Does this mean that we can reuse our ViewModels and share them between Xamarin/MAUI and WPF applications? Does the new Community Toolkit also include ICommand derivatives and implementations for IAsyncCommand like the Xamarin Community Toolkit does? BTW, I absolutely love that we can update the canExecute flag of Commands using attributes now, it takes away a lot of messy code pain.

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety +1

      It does, it included RelayCommand and AsyncRelayCommand or something like that, it is what it generates

    • @jewersp
      @jewersp Před 2 lety

      @@JamesMontemagno that's awesome. Thanks! 😊

  • @lerocher2182
    @lerocher2182 Před rokem

    Thx James,
    How can I use CanExecute for the Command function?

  • @CoreDreamStudios
    @CoreDreamStudios Před 2 lety

    James, I am confused on this method, how do I add a Model to this? or does it all go in ViewModels?

  • @doublebass120
    @doublebass120 Před 2 lety +1

    Excellent library, great video!
    Does this also work for Xamarin Forms and WPF?

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety +3

      Yes it does!

    • @royeagle
      @royeagle Před 2 lety

      I tried it but I get “This generator is not generating files…”

    • @royeagle
      @royeagle Před 2 lety

      @@JamesMontemagno I can get the ObservableProperty to work after changing to C# 9 in Xamarin Forms. Can't get the ICommand to work, the method is never called. Ver 8.0 preview2.

    • @royeagle
      @royeagle Před 2 lety +1

      I figured it out. I named my method ClickMeCommand and it did not work. I changed it to ClickMe and now it works.

  • @ddavid8888888
    @ddavid8888888 Před 2 lety

    Hi James is this considered a mvvm framework?
    And if not Is there an official or a “default “ mvvm framework?
    I have searched for on but the ones I find are broken not maintained or poorly documented
    All of the above makes it so difficult for newcomers as me to even get started in Maui and it would be very beneficial if you could maybe give some references
    Thanks, and keep up with the great content it is really helpful.

    • @JamesMontemagno
      @JamesMontemagno  Před 2 lety

      I wouldn't consider it a framework. It is helpers for mvvm and other things for .NET developers. The documentation is being updated, but checkout devblogs.microsoft.com/ifdef-windows/announcing-net-community-toolkit-v8-0-0-preview-1/