SourceGeneration.ActionDispatcher
1.0.0-beta2.250302.1
dotnet add package SourceGeneration.ActionDispatcher --version 1.0.0-beta2.250302.1
NuGet\Install-Package SourceGeneration.ActionDispatcher -Version 1.0.0-beta2.250302.1
<PackageReference Include="SourceGeneration.ActionDispatcher" Version="1.0.0-beta2.250302.1" />
paket add SourceGeneration.ActionDispatcher --version 1.0.0-beta2.250302.1
#r "nuget: SourceGeneration.ActionDispatcher, 1.0.0-beta2.250302.1"
// Install SourceGeneration.ActionDispatcher as a Cake Addin #addin nuget:?package=SourceGeneration.ActionDispatcher&version=1.0.0-beta2.250302.1&prerelease // Install SourceGeneration.ActionDispatcher as a Cake Tool #tool nuget:?package=SourceGeneration.ActionDispatcher&version=1.0.0-beta2.250302.1&prerelease
ActionDispatcher
Source generator based mediator implementation without the reflection AOTable
.
Supports action dispatch and subscribe.
Installing ActionDispatcher
Install-Package SourceGeneration.ActionDispatcher -Version 1.0.0-beta1.240217.1
dotnet add package SourceGeneration.ActionDispatcher --version 1.0.0-beta1.240217.1
Registering with IServiceCollection
services.AddActionDispatcher()
Action
or Message
or Command
or Event
public class Say
{
public string? Text { get; set; }
}
Handler
You just need add ActionHandlerAttribute
to method
public class ActionHandler
{
[ActionHandler]
public void Handle(Say say, ILogger<ActionHandler> logger, CancellationToken cancellationToken)
{
logger.LogInformation("Handled : " + say.Text);
}
[ActionHandler]
public static void StaticHandle(Say say, ILogger<ActionHandler> logger, CancellationToken cancellationToken)
{
logger.LogInformation("StaticHandle : " + say.Text);
}
[ActionHandler]
public async Task AsyncHandle(Say say, ILogger<ActionHandler> logger, CancellationToken cancellationToken)
{
await Task.Delay(1000, cancellationToken);
logger.LogInformation("AsyncHandled : " + say.Text);
}
}
Method parameters follow the following rules
- The first parameter must be your
action
message
commond
event
CancellationToken
is passed from the caller- Other parameters come from dependency injection
First, the ActionDispatcher will check the IServiceProvider to see if the ActionHandler has been added to the container. If it has, it will be obtained through dependency injection. Otherwise, a new ActionHandler will be created.
public class InjectServiceHandler(ILogger<InjectServiceHandler> logger)
{
[ActionHandler]
public void Handle(Say say)
{
logger.LogInformation("InjectServiceHandled : " + say.Text);
}
}
services.AddSingleton<InjectServiceHandler>();
Dispatcher
You can dispatch action with IActionDispatcher
var dispatcher = services.GetRequiredService<IActionDispatcher>();
dispatcher.Dispatch(new Say { Say = "Hello World" });
Asynchronous invocation is supported.
await dispatcher.DispatchAsync(new Say { Say = "Hello World" });
Log console output
info: ActionHandler[0]
StaticHandle : Hello World
info: ActionHandler[0]
Handled : Hello World
info: InjectServiceHandler[0]
InjectServiceHandled : Hello World
info: ActionHandler[0]
AsyncHandled : Hello World
Subscriber
You can subscribe action with IActionSubscriber
, supprots ActionDispatchStatus
:
- ActionDispatchStatus.WaitingToDispatch
- ActionDispatchStatus.Canceld
- ActionDispatchStatus.Successed
- ActionDispatchStatus.Faulted
var subscriber = services.GetRequiredService<IActionSubscriber>();
subscriber.Subscribe<Say>(action =>
{
Console.WriteLine("Subscriber: Say action dispatched.");
});
subscriber.Subscribe<Say>(ActionDispatchStatus.WaitingToDispatch, action =>
{
Console.WriteLine("Subscriber: Say action dispatching.");
});
subscriber.Subscribe<Say>(ActionDispatchStatus.Faulted, (action, exception) =>
{
Console.WriteLine("Subscriber: Say action dispatch faulted.");
});
//Subscribe all types action
subscriber.Subscribe<object>(action =>
{
Console.WriteLine($"Subscriber: {action.GetType()} action dispatched");
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- SourceGeneration.ActionDispatcher.SourceGenerator (>= 1.0.0-beta2.241113.1)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
- SourceGeneration.ActionDispatcher.SourceGenerator (>= 1.0.0-beta2.241113.1)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- SourceGeneration.ActionDispatcher.SourceGenerator (>= 1.0.0-beta2.241113.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- SourceGeneration.ActionDispatcher.SourceGenerator (>= 1.0.0-beta2.241113.1)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on SourceGeneration.ActionDispatcher:
Package | Downloads |
---|---|
Blux
Blazor flux framework |
|
SourceGeneration.Blazor.Statity
Blazor state management and action disapatch/subscribe extensions |
|
SourceGeneration.Blux
Blazor flux framework |
|
SourceGeneration.Blazor.State
Blazor state management and action disapatch/subscribe extensions |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-beta2.250302.1 | 65 | 3/2/2025 |
1.0.0-beta2.241118.2 | 55 | 11/18/2024 |
1.0.0-beta2.241118.1 | 50 | 11/18/2024 |
1.0.0-beta2.241113.1 | 54 | 11/13/2024 |
1.0.0-beta1.240430.1 | 105 | 4/30/2024 |
1.0.0-beta1.240429.1 | 64 | 4/29/2024 |
1.0.0-beta1.240217.1 | 87 | 2/17/2024 |
1.0.0-beta1.240215.1 | 82 | 2/15/2024 |
1.0.0-beta1.240211.8 | 74 | 2/11/2024 |
1.0.0-beta1.240211.7 | 73 | 2/11/2024 |
1.0.0-beta1.240211.6 | 82 | 2/11/2024 |
1.0.0-beta1.240211.5 | 68 | 2/11/2024 |
1.0.0-beta1.240118.2 | 73 | 1/18/2024 |
1.0.0-beta1.231227.2 | 99 | 12/27/2023 |
1.0.0-beta1.231227.1 | 77 | 12/27/2023 |
1.0.0-beta1.231219.1 | 85 | 12/19/2023 |