One common problem when developing applications with MVM 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 update.
And that's where a messenger comes in today.
I'm gonna break down a completely cross-platform messenger implementation
from the net community toolkit that'll work in any net application.
So tune in.
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 do net community toolkit for MVVM.
I've talked a lot about, on a lot on this channel and that videos up over here and
down over there and over here and all in the places talking about all that.
Goodness, it just reached version 8.0, which brings in all those
beautiful source generators.
All sorts of IOC stuff and all sorts of good stuff.
But today I really wanna just break down.
One of my favorite new components was, is the messenger API.
This gives you a week and a strong messenger for your applications.
So this is gonna really enable you to keep things decoupled, but send messages
when different components need to update.
So what do I mean?
Well, 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 cuz I
didn't do that in the beginner series.
So we're gonna do it today.
What we wanna do is send a message between this page.
Back to the other page, 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 gonna 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 gonna use that source generator there from the MVM community
toolkit, and it is gonna implement, delete command for me automatically.
All right.
Now, from there, we're gonna need to actually implement this thing
if we go and look, we have this go back, so that's kind of cool.
Um, 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, uh, 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 in that beginner series.
If you wanna 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, uh, for at least doing.
So the first thing that we're gonna want to do is go into our new, get packages.
I'm gonna stop debugging, pull up our NewAge Nu get package manager.
And what you wanna have installed is the community toolkit dot MVVM by Microsoft.
That is the do net community toolkit for MVM.
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 wanna do is we wanna send messages to, and.
Now, if I go into my main view model, this view model is gonna 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 gonna go ahead and navigate back.
So we need a message to send across the wire.
Um, so what we're gonna do is we're gonna go into have a messages folder,
and we're gonna add a new class.
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 gonna say delete item message.
And that's gonna be the message.
So what I'm gonna do is I'm gonna 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
gonna do is we're gonna, um, inherit from value changed.
Message.
And it is gonna be of a specific type and that specific type is gonna be string.
So I'm gonna go ahead and bring this in.
There we go.
So using community toolkit dot MVVM dot messaging dot messages.
All right now, inside of here, this value change message is gonna enable
this sending of a string in this.
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 gonna send across.
So if you had a database item or you had a user class or a person
class, or a dog class or a cat class, that's what you'd wanna put in.
This is your message.
This is what you're gonna send.
Now, visual studio is gonna 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 gonna 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 gonna wanna register.
Now.
I mentioned earlier.
That inside of there, we have a, a weak reference manager,
not the weak event manager.
That's coming from Donette Maui, which I'm using here, but specifically
the weak reference messenger, as you can see is coming from the community
toolkit dot MVVM dot messaging.
We also have strong reference manager now in this case, Week reference, um, ma
messenger is gonna use we weak references.
So it'll be automatically able to be garbage collected and all that stuff.
But if you wanna use the strong reference, you wanna automatically
register and unregister manually, um, you can go ahead and do that as well.
We're gonna use the weak reference manager.
And I can say default here.
Now I'm gonna say default, and it's gonna return to me a weak
reference manager implementation.
And what that means is if you want to pass that into, um, Into a
dependency, uh, injection service.
You can do that as well.
So for example, if I go to, um, go to definition here, we can
see that it's an I messenger.
So if I really wanted to, I could come in and pass in an I messenger here.
Right.
And I can go ahead and pass that down into my dependency injection.
So just cuz I'm using the static here, doesn't mean I'm gonna need
to, um, automatically, um, 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 gonna do here is we're just gonna say register.
I'm gonna give it a type and this type specifically is gonna be that message.
So delete item message.
Just like that.
Bring in that name.
Space, boo.
There we.
And now lemme go ahead and drop this down.
We can pass in the construction and the construction is gonna be
able to take in one of two things.
So, um, lemme go ahead and pull this over here.
If I can.
Here we go.
So the first thing it's gonna take is either an I recipient, which I'll talk
about in a second, or it'll take in a recipient, which would be this main
view model, and then this, um, message handler, which is just gonna be a Lambda.
So I can say this and then I could say, um, Um, 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 gonna be an object, which will 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, uh, go ahead and, and do that as well.
So here we have our, our main void.
Oh, sorry.
This is our M dot value.
So there we go and get on there as well.
So if I really wanna make sure that it's on the main thread, I
could use, uh, main thread dot.
Begin invoke on main thread here.
And in this case, this is gonna be, uh, coming from, uh, .NET Maui.
Um, but you could also ensure that you are, uh, doing this with a dispatch
or whatever UI framework you're using.
This just happens to be a .NET 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 gonna handle it.
So I'm just gonna do it there.
Uh, Sergio can correct me if I don't need to do that or not, but you know, at
least main thread is gonna 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 gonna do here is, I don't know if it needs to be a synchronous,
but at least we're gonna go back async so let's go ahead and at least do that.
Um, we're just gonna go ahead and say week, um, week reference manager
dot and here again, I'm gonna bring in that name space week reference.
Oh, sorry.
Not messing manager me.
And here I can go ahead and bring in my default and I'm gonna 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
bringing that, uh, name space as well.
Delete item.
Message.
I'm gonna keep it legit.
And, and just as I was typing it, cuz you told me you like it when
I make mistakes, there we go.
So we're gonna send the message and then what I'm gonna do is
I'm gonna say a wait, 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, um, message being received by our main view
model, then we're gonna navigate back.
And since we're updating this inside of an observable collection, we're
gonna 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.
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 gonna tap on it now and now I'm gonna 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's receiving it and it's coming from me, of course here.
Now I can go ahead and continue on.
We can see, I'm gonna 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.
That is cool.
Go ahead and add that again.
Now of course I can swipe to delete, but I'm gonna 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 send 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.
All right now, one thing I don't love is I don't love this here, and you may not love
this either is sending this big, you know, big blob over and registering like this.
So this is where it gets kind of cool is, uh, you can actually implement the, uh,
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 I recipient.
Of, uh, delete item message.
And I'm gonna go ahead and do this and say implement interface.
There we go.
And I'm gonna 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, I recipient of all these different messages and this
is much cleaner in my personal opinion.
And I'm gonna 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 gonna 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 gonna do here is I'm gonna say, uh, uh, week mess, uh, reference
messenger dot default dot register.
And again, this is gonna be a delete item message.
This time I'm gonna send in an I recipient.
So this, there we go.
Now in this case, I don't need to set the Lambda.
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, uh, I interface that
we've implemented on our view model.
So here we go again.
I'm gonna say like, and subscribe.
Add there, tap on it.
Delete boom.
Just like that.
I still get the message coming in.
Sure enough.
I get the value change.
The base here of 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 Don net application.
No matter what you're developing with.
If you're developing a .NET Maui app, San forums, app WPF app, an UNO app, and
have alone app or any app for anything that's dot net, it doesn't even matter.
It's just.app code.
I love this messenger.
I love that it, um, is interface based so you can register it with
your, uh, dependency, injection service and everything like that.
I love this.
I'm gonna keep doing videos on this now that it's 8.0, it's out there
into the world and I'm gonna, we're gonna 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 tool kit to just.
Make all my code prettier and, and it's all handled elegantly with the
most modern Donnet features as well.
I'm gonna 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 gonna keep covering all these great features, cuz I love it.
Um, if you wanna 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.
Cause I bought, put out new videos here on my channel, just.
Every single week.
I'll also put this source code right on GitHub, and I'll
put it down there as well.
Now, thank you so much for watching and supporting this channel and to
everyone that has been joining the live streams that have been joining
the membership, and I'm gonna 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 put our new video anyways.
Thanks everyone for watching.
Have a good one.