Synaptrix.Generator
3.0.1
dotnet add package Synaptrix.Generator --version 3.0.1
NuGet\Install-Package Synaptrix.Generator -Version 3.0.1
<PackageReference Include="Synaptrix.Generator" Version="3.0.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Synaptrix.Generator" Version="3.0.1" />
<PackageReference Include="Synaptrix.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Synaptrix.Generator --version 3.0.1
#r "nuget: Synaptrix.Generator, 3.0.1"
#:package Synaptrix.Generator@3.0.1
#addin nuget:?package=Synaptrix.Generator&version=3.0.1
#tool nuget:?package=Synaptrix.Generator&version=3.0.1
Synaptrix.Generator: Compile-Time Handler Registration for Synaptrix
Synaptrix (formerly Concordia) — renamed starting from v3.0.0.
Synaptrix.Generator is the C# Source Generator component of the Synaptrix library. It automates the registration of your handlers at compile-time and generates a concrete GeneratedMediator class that dispatches requests and notifications with zero runtime overhead — no DI lookups, no reflection, no allocations on the hot path.
Table of Contents
- Why Synaptrix?
- Key Features
- Performance
- Installation
- Usage
- Contribution
- License
- NuGet Packages
- Contact
- Support
Why Synaptrix?
- An Open-Source Alternative: Synaptrix was created as an open-source alternative in response to other popular mediator libraries (like MediatR) transitioning to a paid licensing model. We believe core architectural patterns should remain freely accessible to the developer community.
- Lightweight and Minimal: Provides only the essential Mediator pattern functionalities, without unnecessary overhead.
- Optimized Performance: The Source Generator goes beyond simple handler registration — it produces a
GeneratedMediatorwith constructor-injected handler singletons and directis-type-switch dispatch, achieving ~1–2 ns per call with zero allocations on the hot path. - Easy DI Integration: Integrates seamlessly with
Microsoft.Extensions.DependencyInjection. - Same MediatR Interfaces: Uses interfaces with identical signatures to MediatR, making migration extremely straightforward.
- CQRS and Pub/Sub Patterns: Facilitates the implementation of Command Query Responsibility Segregation (CQRS) and Publisher/Subscriber principles, enhancing separation of concern and code maintainability.
Key Features
- Two generated files:
SynaptrixGeneratedHandlersRegistrations.g.cs— anAddSynaptrixHandlers()DI extension method that registers all discovered handlers as Singletons and wires upGeneratedMediatorasIMediator/ISender.SynaptrixGeneratedMediator.g.cs— a sealedGeneratedMediatorclass with constructor-injected handlers, direct type-switch dispatch, and anIsCompletedSuccessfullyfast-path for notifications.
- Zero Registration Friction: The
[assembly: DiscoverSynaptrixHandlers]attribute is injected automatically via a.targetsfile — no manual setup required. - Cross-Assembly Discovery: Handlers defined in referenced assemblies that also use
Synaptrix.Generatorare discovered and included automatically. - Configurable Method Name: Rename the generated extension method via
<SynaptrixGeneratedMethodName>in your.csproj.
Performance
Benchmarks measured with BenchmarkDotNet on .NET 10, Intel Core i7-13800H. Ratio = relative to MediatR (1.00).
Send Command (fire-and-forget, no pipeline)
| Method | Mean | Ratio | Allocated |
|---|---|---|---|
| MediatR | 50.5 ns | 1.00 | 128 B |
| Synaptrix | 22.2 ns | 0.44 | 0 B |
| SynaptrixGen | 1.7 ns | 0.03 | 0 B |
| Martin | 5.8 ns | 0.11 | 0 B |
Publish Notification (2 handlers)
| Method | Mean | Ratio | Allocated |
|---|---|---|---|
| MediatR | 87.2 ns | 1.00 | 440 B |
| Synaptrix | 74.0 ns | 0.85 | 224 B |
| SynaptrixGen | 1.6 ns | 0.02 | 0 B |
| Martin | 7.9 ns | 0.09 | 0 B |
Installation
Install the Synaptrix meta-package (recommended) or the individual packages:
dotnet add package Synaptrix
Or individually:
dotnet add package Synaptrix.Core
dotnet add package Synaptrix.Generator
Usage
Define your Handlers, Processors, and Behaviors (as described in
Synaptrix.Core's documentation).Configure your
.csproj: ReferenceSynaptrix.GeneratorasPrivateAssets="all"so it is consumed only as a Roslyn analyzer and not exposed as a transitive dependency. Optionally set a custom method name:<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net10.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <SynaptrixGeneratedMethodName>AddMyAppHandlers</SynaptrixGeneratedMethodName> </PropertyGroup> <ItemGroup> <CompilerVisibleProperty Include="SynaptrixGeneratedMethodName" /> </ItemGroup> <ItemGroup> <PackageReference Include="Synaptrix.Core" Version="3.0.0" /> <PackageReference Include="Synaptrix.Generator" Version="3.0.0" PrivateAssets="all" /> </ItemGroup> </Project>Register services in
Program.cs:A single call to
AddSynaptrixHandlers()(or your custom name) registers everything — handler Singletons and theGeneratedMediatorwired asIMediator/ISender:using Synaptrix; var builder = WebApplication.CreateBuilder(args); // Registers all handlers + GeneratedMediator as IMediator/ISender in one call. builder.Services.AddSynaptrixHandlers(); builder.Services.AddControllers(); var app = builder.Build(); app.MapControllers(); app.Run();Inject
IMediatororISenderin your controllers / services as usual:public class ProductsController : ControllerBase { private readonly ISender _sender; public ProductsController(ISender sender) => _sender = sender; [HttpGet("{id}")] public async Task<IActionResult> Get(int id) => Ok(await _sender.Send(new GetProductByIdQuery { ProductId = id })); }
The generator automatically finds your handler implementations and generates two files. Look for them under Dependencies → Analyzers → Synaptrix.Generator in Solution Explorer.
Contribution
Feel free to contribute to the project! Report bugs, suggest new features, or submit pull requests. Please follow the Contributing Guidelines.
License
This project is released under the MIT License. See the LICENSE file for more information.
NuGet Packages
- Synaptrix — meta-package (recommended)
- Synaptrix.Core
- Synaptrix.Generator
Contact
For any questions, issues, or feedback, please open an issue on the GitHub repository.
Support
If you find this library useful, consider supporting its development: Buy Me a Coffee.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Synaptrix.Generator:
| Package | Downloads |
|---|---|
|
Synaptrix
A lightweight and powerful .NET Mediator library with compile-time source generation. Install this single package to get Synaptrix.Core and Synaptrix.Generator. |
GitHub repositories
This package is not used by any popular GitHub repositories.