Skip to content

Commit 4ce7d1e

Browse files
jamesmcroftSergio0694
authored andcommitted
Added docs for Messenger
1 parent 01e7d92 commit 4ce7d1e

File tree

1 file changed

+253
-2
lines changed

1 file changed

+253
-2
lines changed

docs/mvvm/MigratingFromMvvmLight.md

Lines changed: 253 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ dev_langs:
99

1010
# Migrating from MvvmLight
1111

12-
This article outlines some of the key differences between the [MvvmLight Toolkit](https://github.com/lbugnion/mvvmlight) and the Windows Community Toolkit MVVM framework to ease your migration.
12+
This article outlines some of the key differences between the [MvvmLight Toolkit](https://github.com/lbugnion/mvvmlight) and the MVVM Toolkit to ease your migration.
13+
14+
While this article specifically focuses on the migrations from MvvmLight to the MVVM Toolkit, note that there are additional improvements that have been made within the MVVM Toolkit so I highly recommend taking a look at the documentation for the APIs.
1315

1416
## Installing the WCT MVVM Toolkit
1517

@@ -307,7 +309,7 @@ var isInDesignMode = ViewModelBase.IsInDesignModeStatic;
307309
// No direct replacement, remove
308310
```
309311

310-
## SimpleIoc
312+
## Migrating SimpleIoc
311313

312314
The [IoC](Ioc) implementation in the MVVM Toolkit takes advantage of existing .NET APIs through the `Microsoft.Extensions.DependencyInjection` library.
313315

@@ -406,4 +408,253 @@ Ioc.Default.ConfigureServices(services =>
406408
services.AddSingleton<IToastService, ToastService>();
407409
services.AddSingleton<INavigationService, NavigationService>();
408410
});
411+
```
412+
413+
## Migrating Messenger
414+
415+
The following steps focus on migrating your existing components which take advantage of the `Messenger` of the MvvmLight Toolkit.
416+
417+
The Windows Community Toolkit MVVM framework provides a [`Messenger`](Messenger) type that provides similar functionality.
418+
419+
A note on `Register` methods throughout this migration. MvvmLight uses weak references to establish the link between the `Messenger` instance and the recipient. This is not required by the MVVM Toolkit implementation and if this optional parameter has been set to `true` in any of your `Register` method calls, this will be removed.
420+
421+
Below are a list of migrations that will need to be performed if being used in your current solution.
422+
423+
### Messenger Methods
424+
425+
#### Register<TMessage> ( object, Action<TMessage> )
426+
427+
The functionality of `Register<TMessage>(object, Action<TMessage>)` can be achieved with the MVVM Toolkit's `IMessenger` extension method `Register<TMessage>(object, Action<TMessage>)`.
428+
429+
```csharp
430+
// MvvmLight
431+
Messenger.Default.Register<MyMessage>(this, this.OnMyMessageReceived);
432+
433+
// Toolkit.Mvvm
434+
Messenger.Default.Register<MyMessage>(this, this.OnMyMessageReceived);
435+
```
436+
437+
#### Register<TMessage> ( object, bool, Action<TMessage> )
438+
439+
There is no direct replacement for this registration mechanism which allows you to support receiving messages for derived message types also. This change is intentional as the `Messenger` implementation aims to not use reflection to achieve it's performance benefits.
440+
441+
Alternatively, there are a few options that can be done to achieve this functionality.
442+
443+
- Create a custom `IMessenger` implementation
444+
- Register the additional message types using a shared `Action`
445+
446+
```csharp
447+
// MvvmLight
448+
Messenger.Default.Register<MyMessage>(this, true, this.OnMyMessageReceived);
449+
450+
// Toolkit.Mvvm
451+
Messenger.Default.Register<MyMessage, string>(this, nameof(MyViewModel), this.OnMyMessageReceived);
452+
Messenger.Default.Register<MyOtherMessage, string>(this, nameof(MyViewModel), this.OnMyMessageReceived);
453+
```
454+
455+
#### Register<TMessage> ( object, object, Action<TMessage> )
456+
457+
The functionality of `Register<TMessage>(object, object, Action<TMessage>)` can be achieved with the MVVM Toolkit's `Register<TMessage, TToken>(object, TToken, Action<TMessage>)` method.
458+
459+
```csharp
460+
// MvvmLight
461+
Messenger.Default.Register<MyMessage>(this, nameof(MyViewModel), this.OnMyMessageReceived);
462+
463+
// Toolkit.Mvvm
464+
Messenger.Default.Register<MyMessage, string>(this, nameof(MyViewModel), this.OnMyMessageReceived);
465+
```
466+
467+
#### Register<TMessage> ( object, object, bool, Action<TMessage> )
468+
469+
There is no direct replacement for this registration mechanism which allows you to support receiving messages for derived message types also. This change is intentional as the `Messenger` implementation aims to not use reflection to achieve it's performance benefits.
470+
471+
Alternatively, there are a few options that can be done to achieve this functionality.
472+
473+
- Create a custom `IMessenger` implementation
474+
- Register the additional message types using a shared `Action`
475+
476+
```csharp
477+
// MvvmLight
478+
Messenger.Default.Register<MyMessage>(this, nameof(MyViewModel), true, this.OnMyMessageReceived);
479+
480+
// Toolkit.Mvvm
481+
Messenger.Default.Register<MyMessage, string>(this, nameof(MyViewModel), this.OnMyMessageReceived);
482+
Messenger.Default.Register<MyOtherMessage, string>(this, nameof(MyViewModel), this.OnMyMessageReceived);
483+
```
484+
485+
#### Send<TMessage> ( TMessage )
486+
487+
The functionality of `Send<TMessage>(TMessage)` can be achieved with the MVVM Toolkit's `IMessenger` extension method `Send<TMessage>(TMessage)`.
488+
489+
```csharp
490+
// MvvmLight
491+
Messenger.Default.Send<MyMessage>(new MyMessage());
492+
Messenger.Default.Send(new MyMessage());
493+
494+
// Toolkit.Mvvm
495+
Messenger.Default.Send<MyMessage>(new MyMessage());
496+
Messenger.Default.Send(new MyMessage());
497+
```
498+
499+
#### Send<TMessage> ( TMessage, object )
500+
501+
The functionality of `Send<TMessage>(TMessage, object)` can be achieved with the MVVM Toolkit's `Send<TMessage, TToken>(TMessage, TToken)` method.
502+
503+
```csharp
504+
// MvvmLight
505+
Messenger.Default.Send<MyMessage>(new MyMessage(), nameof(MyViewModel));
506+
Messenger.Default.Send(new MyMessage(), nameof(MyViewModel));
507+
508+
// Toolkit.Mvvm
509+
Messenger.Default.Send(new MyMessage(), nameof(MyViewModel));
510+
```
511+
512+
#### Unregister ( object )
513+
514+
The functionality of `Unregister(object)` can be achieved with the MVVM Toolkit's `UnregisterAll(object)` method.
515+
516+
```csharp
517+
// MvvmLight
518+
Messenger.Default.Unregister(this);
519+
520+
// Toolkit.Mvvm
521+
Messenger.Default.UnregisterAll(this);
522+
```
523+
524+
#### Unregister<TMessage> ( object )
525+
526+
The functionality of `Unregister<TMessage>(object)` can be achieved with the MVVM Toolkit's `IMessenger` extension method `Unregister<TMessage>(object)`.
527+
528+
```csharp
529+
// MvvmLight
530+
Messenger.Default.Unregister<MyMessage>(this);
531+
532+
// Toolkit.Mvvm
533+
Messenger.Default.Unregister<MyMessage>(this);
534+
```
535+
536+
#### Unregister<TMessage> ( object, Action<TMessage> )
537+
538+
There is no direct replacement for the `Unregister<TMessage>(object, Action<TMessage>)` method in the MVVM Toolkit.
539+
540+
Alternatively, we recommend achieving a similar functionality with the MVVM Toolkit's `IMessenger` extension method `Unregister<TMessage>(object)`.
541+
542+
```csharp
543+
// MvvmLight
544+
Messenger.Default.Unregister<MyMessage>(this, this.OnMyMessageReceived);
545+
546+
// Toolkit.Mvvm
547+
Messenger.Default.Unregister<MyMessage>(this);
548+
```
549+
550+
#### Unregister<TMessage> ( object, object )
551+
552+
The functionality of `Unregister<TMessage>(object, object)` can be achieved with the MVVM Toolkit's `Unregister<TMessage, TToken>(object, TToken)` method.
553+
554+
```csharp
555+
// MvvmLight
556+
Messenger.Default.Unregister<MyMessage>(this, nameof(MyViewModel));
557+
558+
// Toolkit.Mvvm
559+
Messenger.Default.Unregister<MyMessage, string>(this, nameof(MyViewModel));
560+
```
561+
562+
#### Unregister<TMessage> ( object, object, Action<TMessage> )
563+
564+
There is no direct replacement for the `Unregister<TMessage>(object, object, Action<TMessage>)` method in the MVVM Toolkit.
565+
566+
Alternatively, we recommend achieving a similar functionality with the MVVM Toolkit's `Unregister<TMessage, TToken>(object, TToken)` method.
567+
568+
```csharp
569+
// MvvmLight
570+
Messenger.Default.Unregister<MyMessage>(this, nameof(MyViewModel), this.OnMyMessageReceived);
571+
572+
// Toolkit.Mvvm
573+
Messenger.Default.Unregister<MyMessage, string>(this, nameof(MyViewModel));
574+
```
575+
576+
#### Cleanup ()
577+
578+
There is no direct replacement for the `Cleanup` method in the MVVM Toolkit. In the context of MvvmLight, `Cleanup` is used to remove registrations which are no longer alive as the implementation takes advantage of weak references.
579+
580+
Any calls to the `Cleanup` method can be removed.
581+
582+
```csharp
583+
// MvvmLight
584+
Messenger.Default.Cleanup();
585+
586+
// Toolkit.Mvvm
587+
// No direct replacement, remove
588+
```
589+
590+
#### RequestCleanup ()
591+
592+
There is no direct replacement for the `RequestCleanup` method in the MVVM Toolkit. In the context of MvvmLight, `RequestCleanup` is used to initiate a request to remove registrations which are no longer alive as the implementation takes advantage of weak references.
593+
594+
Any calls to the `RequestCleanup` method can be removed.
595+
596+
```csharp
597+
// MvvmLight
598+
Messenger.Default.RequestCleanup();
599+
600+
// Toolkit.Mvvm
601+
// No direct replacement, remove
602+
```
603+
604+
#### ResetAll ()
605+
606+
There is no direct replacement for the `ResetAll` method in the MVVM Toolkit. In the context of MvvmLight, this method is used to reset the default `IMessenger` instance by calling the static `Reset` method.
607+
608+
Any calls to the `ResetAll()` method can be removed.
609+
610+
```csharp
611+
// MvvmLight
612+
Messenger.Default.ResetAll();
613+
614+
// Toolkit.Mvvm
615+
// No direct replacement, remove
616+
```
617+
618+
### Messenger Static Methods
619+
620+
#### OverrideDefault ( IMessenger )
621+
622+
There is no direct replacement for the `OverrideDefault(IMessenger)` method in the MVVM Toolkit.
623+
624+
Any calls to the `Messenger.OverrideDefault(IMessenger)` method can be removed.
625+
626+
```csharp
627+
// MvvmLight
628+
Messenger.OverrideDefault(new Messenger());
629+
630+
// Toolkit.Mvvm
631+
// No direct replacement, remove
632+
```
633+
634+
#### Reset ()
635+
636+
There is no direct replacement for the `Reset` method in the MVVM Toolkit. In the context of MvvmLight, this static method is used to reset the default `IMessenger` instance.
637+
638+
Any calls to the `Messenger.Reset()` method can be removed.
639+
640+
```csharp
641+
// MvvmLight
642+
Messenger.Reset();
643+
644+
// Toolkit.Mvvm
645+
// No direct replacement, remove
646+
```
647+
648+
### Messenger Static Properties
649+
650+
#### Default
651+
652+
`Default` has a direct replacement, `Default`, requiring no change to your existing implementation.
653+
654+
```csharp
655+
// MvvmLight
656+
IMessenger messenger = Messenger.Default;
657+
658+
// Toolkit.Mvvm
659+
IMessenger messenger = Messenger.Default;
409660
```

0 commit comments

Comments
 (0)