Command Scheduling
Commands are often designed to tell a system to do something. You don’t need a response other than “did it crash”. Commands offer additional capabilities such as deferred execution/scheduling and it is super simple to add!
-
Create a command that implements
IScheduledCommand
(part of Shiny.Mediator.Contracts)public class MyScheduledCommand : IScheduledCommand{public DateTime DueAt { get; set; }} -
Create a standard command handler. Notice there is nothing special about this handler.
public class MyScheduledCommandHandler : ICommandHandler<MyScheduledCommand>{public async Task Handle(MyScheduledCommand command, MediatorContext context, CancellationToken ct){// do something with the command}} -
Follow all the steps to register the handler shown in Commands
-
Register the command scheduling middleware in your AddShinyMediator registration
services.AddShinyMediator(x => x.AddInMemoryCommandScheduling()); -
Now, sends your command
IMediator mediator;await mediator.Send(new MyScheduledCommand{DueAt = DateTime.UtcNow.AddMinutes(5)});
Custom Scheduling
In-Memory is fine for getting up and running. There are likely instances where you want this to survive across application restarts. We currently only offer an in-memory provider, but this is easily extendable to you now.
- Create a command scheduler by impleming
Shiny.Mediator.ICommandScheduler
(part of Shiny.Mediator)public class MyCommandScheduler : ICommandScheduler{public Task<bool> Schedule(IMediatorContext context, DateTimeOffset dueAt, CancellationToken cancellationToken){// IMediatorContext contains all of the "stuff" you needmediator.Message; // object of your mediator contractmediator.MessageHandler; // the handler to call - HOWEVER: consider using context.Send// It is not recommended you use the cancellation token before STORING whatever data here as this is related to the WRITING of the call, NOT the run of the scheduled command}} - Now register during your AddShinyMediator registration
services.AddShinyMediator(x => x.AddCommandScheduler<MyCommandScheduler>());
- Now, when you send a command that implements
IScheduledCommand
, it will go through your schedulerIMediator mediator;await mediator.Send(new MyScheduledCommand{DueAt = DateTime.UtcNow.AddMinutes(5)});