VOOZH about

URL: https://shinylib.net/mediator/getting-started/

⇱ Quick Start | Shiny.NET


Skip to content
Client v5: BLE, BLE Hosting, HTTP, Jobs - Linux, MacOS, & Blazor Support! Full AOT, RX on BLE only & MANY other features! Power up!

Quick Start

  1. Install 👁 NuGet package Shiny.Mediator
    & 👁 NuGet package Shiny.Mediator.Maui

  2. First, let’s create our request & event handlers

    usingShiny.Mediator;
    publicrecordTestCommand(stringAnyArgs, intYouWant) : ICommand;
    publicrecordTestEvent(MyObjectAnyArgs) : IEvent;
    // and for request/response requests - we'll come back to this
    publicrecordTestResponseRequest : IRequest<TestResponse> {}
    publicrecordTestResponse {}
  3. Next - let’s wire up our handlers. 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 Request & Command Handlers

    usingShiny.Mediator;
    // NOTE: Request handlers are registered as singletons
    publicclassTestCommandHandler : ICommandHandler<TestCommand>
    {
    // you can do all dependency injection here
    publicasyncTaskHandle(TestCommandcommand, IMediatorContextcontext, CancellationTokenct)
    {
    // do something async here
    }
    }
    publicclassTestResponseRequestHandler : IRequestHandler<TestResponseRequest, TestResponse>
    {
    publicasyncTask<TestResponse> Handle(TestResponseRequestrequest, IMediatorContextcontext, CancellationTokenct)
    {
    varresponse=awaitGetResponseThing(ct);
    return response;
    }
    }
    publicclassTestEventHandler : IEventHandler<TestEvent>
    {
    // Dependency injection works here
    publicasyncTaskHandle(TestEvent@event, IMediatorContextcontext, CancellationTokenct)
    {
    // Do something async here
    }
    }
  4. Now, let’s register all of our stuff with our .NET MAUI MauiProgram.cs

    publicstaticclassMauiProgram
    {
    publicstaticMauiAppCreateMauiApp()
    {
    varbuilder= MauiApp
    .CreateBuilder()
    .UseMauiApp<App>();
    builder.Services.AddShinyMediator(x=> x.UseMaui());
    // AddSingletonAsImplementedInterfaces/AddScopedAsImplementedInterfaces are extension methods we've added to IServiceCollection
    // that allow you to register all interfaces that a class implements
    builder.Services.AddSingletonAsImplementedInterfaces<TestEventHandler>();
    builder.Services.AddSingletonAsImplementedInterfaces<TestCommandHandler>();
    builder.Services.AddSingletonAsImplementedInterfaces<TestResponseRequestHandler>();
    // OR
    // if you're using our attribute for source generation - simply add [RegisterHandler], [RegisterMiddleware] to your types
    // NOTE: that for each assembly you have source generation enabled, you will need to call AddDiscoveredMediatorHandlersFrom{AssemblyName}
    builder.Services.AddDiscoveredMediatorHandlersFromMaui_App();
    }
    }
  5. 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

    publicclassMyViewModel(Shiny.Mediator.IMediatormediator)
    {
    publicasyncTaskExecute()
    {
    await mediator.Send(newTestCommand()); // this will execute TestCommandHandler
    varresponse=await mediator.Request(newTestResponseRequest()); // this will execute TestResponseRequestHandler and return a value
    // this will publish to any service registered that implement IEventHandler<TestEvent>
    // there are additional args here to allow you to execute values in sequentially or wait for all events to complete
    await mediator.Publish(newTestEvent());
    }
    }

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

  1. Install 👁 NuGet package Shiny.Mediator.Maui

  2. Now…let’s go back to our MauiProgram.cs and alter the AddShinyMediator

    builder.Services.AddShinyMediator(cfg=> cfg.UseMaui());
  3. Now your viewmodel (or page) can simply implement the IEventHandler interface to participate

    publicclassMyViewModel : BaseViewModel,
    Shiny.Mediator.IEventHandler<TestEvent1>,
    Shiny.Mediator.IEventHandler<TestEvent2>
    {
    publicasyncTaskHandle(TestEvent@event, IMediatorContextcontext, CancellationTokenct)
    {
    }
    publicasyncTaskHandle(TestEvent2@event, IMediatorContextcontext, CancellationTokenct)
    {
    }
    }
  1. Install 👁 NuGet package Shiny.Mediator
    & 👁 NuGet package Shiny.Mediator.Blazor
  2. Create your handlers just like MAUI above
  3. Setup Mediator with Blazor component event collection
    builder.Services.AddShinyMediator(x=> x.UseBlazor());
  4. In your main index/app.razor, add the following underneath the Blazor script
    <scriptsrc="_framework/blazor.webassembly.js"></script>
    <scriptsrc="_content/Shiny.Mediator.Blazor/Mediator.js"></script>

When you install Shiny.Mediator, we include a source generator that will create a set of attributes that you can mark your handlers with. This will generate the registration code for you.

Those attributes are:

  • MediatorSingletonAttribute
  • MediatorScopedAttribute

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)

builder.Services.AddMediatorRegistry();
// OR
builder.AddShinyMediator(x=> x.AddMediatorRegistry());

Please read Source Generation (AOT) for more information

Step 1 — Add the marketplace:

claude plugin marketplace add shinyorg/skills

Step 2 — Install the plugin:

claude plugin install shiny-mediator@shiny

Step 1 — Add the marketplace:

copilot plugin marketplace add https://github.com/shinyorg/skills

Step 2 — Install the plugin:

copilot plugin install shiny-mediator@shiny
View shiny-mediator Plugin