Next - let’s wire up a RequestHandler. You can have ONLY 1 request handler per request type. This is where you would do the main business logic or data requests.
Let’s create our RequestHandler
Now, let’s register all of our stuff with our .NET MAUI MauiProgram.cs
And finally, let’s use the mediator and call our new models - any model model/viewmodel/etc participating in dependency injection can now inject the mediator
What about my ViewModels?
For .NET MAUI, your viewmodels have the ability to participate in the event publishing chain without being part of dependency injection
With this setup, you don’t need to worry about deallocating any events, unsubscribing from some service, or hooking to anything.
Lastly, if your page/viewmodel is navigated away from (popped), it will no longer participate in the event broadcast
Install
Now…let’s go back to our MauiProgram.cs and alter the AddShinyMediator
Now your viewmodel (or page) can simply implement the IEventHandler interface to participate
Quick Start - Blazor
Install &
Create your handlers just like MAUI above
Setup Mediator with Blazor component event collection
In your main index/app.razor, add the following underneath the Blazor script
Source Generated Registration
When you install Shiny.Mediator, we include a source generator that will create a set of attributes that you can mark
your request handlers, event handlers, and middleware with. This will generate the registration code for you.
Those attributes are:
SingletonHandlerAttribute
ScopedHandlerAttribute
SingletonMiddlewareAttribute
ScopedMiddlewareAttribute
From there, any of those marked classes will be added to a source generated extension that you can use for you IServiceCollection
registration
Example: MauiProgram.cs (ie. Project/Assembly with Shiny.Mediator installed named: Maui.App)
The convention we use is AddDiscoveredMediatorHandlersFrom{AssemblyName} - This prevents naming collisions during the registration process.
All source generated registrations will register your implementations as singleton against ALL interfaces they implement.