A Better Pub/Sub Messenger for .NET Applications with WeakReferenceMessenger

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one common problem when developing applications with mvvm or other sort of architectures that really are decoupling components is sending messages between those components the whole idea of using this architecture is to keep them decoupled so they're not tightly coupled and they don't reference each other but sometimes you need to pass the data between the two or notify them when something updates and that's where a messenger comes in today i'm going to break down a completely cross-platform messenger implementation from the dotnet community toolkit that'll work in any dot net application so tune in [Music] hey everyone i'm james and i'm back with another video this time talking about the messenger service inside of the.net community toolkit for mvvm now the dotnet community toolkit for mvvm i've talked a lot about a lot on this channel my videos up over here and down over there and over here and all the places talking about all that goodness it just reached version 8.0 which brings in all those beautiful source generators and all sorts of ioc stuff and all sorts of good stuff but today i really want to just break down one of my favorite new components was is the messenger api this gives you a weak and a strong messenger for your applications so this is going to really enable you to keep things decoupled but send messages when different components need to update so what do i mean let's check it out now over here i have a very simple task application this is actually coming from the.net maui beginner series that i recorded that you can find over on the.net youtube i'll link it up over here there's an application where you can add tasks you can click on one you can go back just like this and you can also swipe to delete now when you swipe to delete the implementation knows about what's in the ui but what happens when you want to delete from right here so if you want to click and delete that well i need to implement that because i didn't do that in the beginner series so we're going to do it today what we want to do is send a message between this page back to the other page and tell it to remove itself automatically so when i go back it's automatically done this could be really great when you need to send different notifications not only for deleting but for adding or updating at least tell it what information to do okay so let's hop into the code so the first thing that we're going to see over here is i have my detail page and i have a delete button now that is inside of my detail view model and so far i have a simple relay command this is going to use that source generator there from the mvvm community toolkit and it is going to implement delete command for me automatically all right now from there we're going to need to actually implement this thing if we go and look we have this go back so that's kind of cool but we also have our main view model our main view model here is that first page so we have an observable collection of just strings so nothing fancy here just a string we have the observable property of text so whenever i went and entered something here like hello world and hit add here that gets updated and it gets added into the list so we can see it right here and of course you can actually follow along this application as i'm building it on that beginner series if you want to build your own from scratch literally file new so here's that delete it's very simple now is this the best way to delete and add items because there's no identifier it's just a string no but it's a good it's a good sample for at least doing this so the first thing that we're going to want to do is go into our nuget packages so i'm going to stop debugging pull up our nougat do you get package manager and what you want to have installed is the communitytoolkit.mvvm by microsoft that is the.net community toolkit for mvvm it has all sorts of good stuff in there you already saw the source generators that i'm using version 8.0 that's what i'm using right now so what we want to do is we want to send messages to and from now if i go into my main view model this view model is going to subscribe to basically messages kind of pub sub the detail view model is going to send a message to delete that item and then it's going to go ahead and navigate back so we need a message to send across the wire so what we're going to do is we're going to go into i have a messages folder and we're going to add a new class and let's call this delete item message and the whole idea here is that every message is sort of like an action that you may have so here we're going to say delete item message and that's going to be the message so what i'm going to do is i'm going to fix this up because we don't need any of this junk in here and we'll just say public class now inside of here what we're going to do is we're going to inherit from value changed message and it is going to be of a specific type and that specific type is going to be string so i'm going to go ahead and bring this in and there we go so using communitytoolkit.mvvm.messaging.messages all right now inside of here this value change message is going to enable this sending of a string in this case now if we go ahead and right click and say go to definition what we'll see in here is that we have basically just a value it's very very simple class so it's just something we implement so the messenger knows to be able to send it or not all right now this type string is important this is the message that i'm going to send across so if you had a database item or you a user class or a person class or a dog class or a cat class that's what you'd want to put in this is your message this is what you're going to send now visual studio is going to create the the constructor that's required and it needs to take in a constructor of the same type and that's literally our message create a message create one that takes in a string it's going to pass it to the backing value store so it knows to pass that back and forth and that's it that's our message easy peasy pretty rad okay so now all we can do is go back into our main view model and we're going to want to register now i mentioned earlier that inside of there we have a a week reference manager not the weak event manager that's coming from donet maui which i'm using here but specifically the weak reference messenger as you can see it's coming from the community toolkit.mvvm.messaging we also have strong reference manager now in this case week reference messenger is going to use weak references so it'll be automatically able to be garbage collected and all that stuff but if you want to use the strong reference you want to automatically register and unregister manually you can go ahead and do that as well we're going to use the weak reference manager and i can say default here now i'm going to say default and it's going to return to me a weak reference manager implementation and what that means is if you want to pass that into into a dependency injection service you can do that as well so for example if i go to go to definition here we can see that it's an imessenger so if i really wanted to i could come in and pass in an imessenger here right and i can go ahead and pass that down into my dependency injection so just because i'm using the static here doesn't mean i'm going to need to automatically not get that di ioc goodness which i talked about in a previous video so inside of here i can check to see if something is registered i can register it i can send a message i can unregister i can unregister all what we're going to do here is we're just going to say register we're going to give it a type and this type specifically is going to be that message so delete item message just like that bring in that namespace boop there we go and now i'm going to drop this down we can pass in the constructor and the constructor is going to be able to take in one of two things so let me go ahead and pull this over here if i can there we go so the first thing it's going to take is either an i recipient which i'll talk about in a second or it will take in a recipient which would be this main view model and then this message handler which is just going to be a lambda so i can say this and then i could say the object itself and then the message itself here so we'll go ahead and do this and now we can see if i do m it's going to be the delete item message r is just going to be an object which would be the sender the the that's going on here cool so m dot and we're totally good to go so what we could do is we could come in and then call delete and pass it m which would be my string there we go now in this case we probably also want to make sure we're on the main thread and we could go ahead and do that as well so here we have our our main void sorry this is our mdat value so there we go and get on there as well so if i really want to make sure that it's on the main thread i could use main thread dot begin invoke on main thread here and in this case this is going to be coming from don and maui but you could also ensure that you're doing this with a dispatch or whatever ui framework you're using this just happens to be a done maui application so here you know this is important i don't know what thread this is coming from if i'm sending that message from a background thread how that is going to handle it so i'm just going to do it there sergio can correct me if i don't need to do that or not but you know at least main thread is going to check to see if i'm on the main thread ahead of time so now what we need to do now that we're registered here is we need to go ahead and send it so again i come to my detail view model and what i'm going to do here is i don't know if it needs to be asynchronous but at least we're going to go back async so let's go ahead and at least do that we're just going to go ahead and say week um week reference manager dot and here again i'm going to bring in that namespace weak reference oh sorry not mess manager messenger and here i can go ahead and bring in my default and i'm going to say this time send and now what i need to do is just give it a new delete item command and pass it in in this specific case the text that is coming in because that's what i have bring in that namespace as well delete item message i'm going to keep it legit and and just as i was typing it because you told me you like it when i make mistakes there we go so we're going to send the message and now what i'm going to do is i'm going to say await go back so that's how we have the async there and that's it really and now what i should be able to do is navigate into an item click on delete it's going to send a message across and what we should get over in our main view model if we go over here is that specific message being received by our main view model then we're going to navigate back and since we're updating this inside of an observable collection we're going to automatically see that deleted and we probably won't even see it if it's that fast so let's give it a second here to compile up and deploy all right so our application is up let's add a task in here i'll say subscribe and a like if you haven't done that already let's go and add it i'm going to tap on it now and now i'm going to hit delete and here we go check this out we've sent over a message and our value is subscribe and like now if i go over here to our message we can see that the main view model sorry the r is the recipient so that is who is receiving it inside of this lambda so we're totally good to go so the main view model is receiving it and it's coming from me of course here now i can go ahead and continue on you can see i'm going to call delete pass in my value and then sure enough it's gone that's really cool i can go ahead and do that again let's do this wow wow that is cool go ahead and add that again now of course i can swipe to delete but i'm going to go in check out details hit delete sure enough it comes here really fast boom it's gone automatically so just like that i'm sending and receiving message across my view models you can set it to anywhere in your app so if your app is changing or something is coming up from sleep it's a good way of sending messages from an app layer into a view model layer all right now one thing i don't love is i don't love this here and you may not love this either it's sending this big you know big blob over and registering like this so this is where it gets kind of cool is you can actually implement the i um uh sorry this is an i recipient and this will then take in your message so this is kind of cool i'm gonna say irecipient of delete item message i'm gonna go ahead and do this and say implement interface there we go and i'm going to go ahead and delete this and delete this just like that scroll down to the bottom where it was implemented and now i can have this eye recipient of all these different messages and this is much cleaner in my personal opinion and i'm going to get a delete item message i don't get any of that extra stuff that i need and i can just say message dot value and now i'm going to go ahead and delete that so i can have all of these recipients coming in and i receive these messages over and over and over again so as many unique messages as you want now i still need to register it so what i'm going to do here is i'm going to say week mess reference messenger default and again this is going to be a delete item message but this time i'm going to send in an irecipient so this there we go now in this case i don't need to set the lambda or do anything like that it's just there so now let's go ahead and do this one more time and actually see how we can pass this across with this new i recipient interface that we've implemented on our view model so here we go again i'm going to say like and subscribe hit add there tap on it delete boom just like that i still get the message coming in sure enough i get the value changed the base here value is like and subscribe and boom just like that it is gone all right there you have it the easiest way i love this implementation of a messenger for any dotnet application no matter what you're developing with if you're developing a donna maui app xamarin forms app wpf app and uno app and levelonia app or any app for anything that's.net it doesn't even matter it's just a dot-net code i love this messenger i love that it is interface based so you can register it with your dependency injection service and everything like that i love this i'm going to keep doing videos on this now that it's 8.0 it's out there into the world and i'm going to we're going to keep looking at how to further reduce some of that code clutter that you may have had or some other libraries out there from the past i've created many of these and i just love there's a community toolkit to just make all my code prettier and it's all handled elegantly with the most modern dot net features as well i'm going to put links to the documentation and the mvvm toolkit 8.0 release blog that sergio put out and some videos as well down in the show notes below hope that you enjoyed this video i'm going to keep covering all these great features because i love it um if you want to see anything specific leave a comment below and as always don't forget to like if you liked any part of this video and subscribe because i bought put out new videos here on my channel just about every single week i'll also put this source code right on github and i'll put it down there as well anyways thank you so much for watching and supporting this channel and to everyone that has been joining the live streams that have been enjoying the membership and i'm going to be doing some additional live streams upcoming so again make sure you subscribe so you get notified when you ring that notification bell every time i go live or about our new video anyways thanks everyone for watching [Music] you
Info
Channel: James Montemagno
Views: 38,834
Rating: undefined out of 5
Keywords: .net maui, .net community toolkit, mvvm toolkit, mvvm messenger, pub/sub, .net maui messenger, .net maui tutorial, dotnet maui, what is .net maui, .net maui workshop, .net 6, .net maui community, messenger api, send and receive messages in apps, .net maui mvvm, .net maui community toolkit, what is pub/sub, how to use pub/sub, .net messenger, .net core, programming, learn c#, c# tutorial, dotnet developer, dot net
Id: vD17OetzGXc
Channel Id: undefined
Length: 16min 58sec (1018 seconds)
Published: Thu Aug 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.