{"id":24073037,"url":"https://github.com/vlada22/minimal-mediator","last_synced_at":"2025-09-16T17:18:38.678Z","repository":{"id":167251996,"uuid":"642814732","full_name":"vlada22/minimal-mediator","owner":"vlada22","description":"Minimal Mediator","archived":false,"fork":false,"pushed_at":"2024-11-26T09:37:12.000Z","size":114,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-26T15:16:51.457Z","etag":null,"topics":["dotnet","mediator","minimal-api","minimal-mediator","native-aot","source-generated","streams-api"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vlada22.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-19T12:06:10.000Z","updated_at":"2024-12-22T18:40:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"44f37193-cead-405b-9193-39ccc337501f","html_url":"https://github.com/vlada22/minimal-mediator","commit_stats":null,"previous_names":["vlada22/minimal-mediator"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlada22%2Fminimal-mediator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlada22%2Fminimal-mediator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlada22%2Fminimal-mediator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlada22%2Fminimal-mediator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vlada22","download_url":"https://codeload.github.com/vlada22/minimal-mediator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233227618,"owners_count":18644428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet","mediator","minimal-api","minimal-mediator","native-aot","source-generated","streams-api"],"created_at":"2025-01-09T17:25:38.913Z","updated_at":"2025-09-16T17:18:33.644Z","avatar_url":"https://github.com/vlada22.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Minimal Mediator\n![Github actions](https://github.com/vlada22/minimal-mediator/actions/workflows/build-release.yml/badge.svg)\n[![Nuget feed](https://img.shields.io/nuget/v/MinimalMediator?label=MinimalMediator)](https://www.nuget.org/packages/MinimalMediator)\n\nMinimal Mediator is a lightweight library for implementing the mediator pattern in .NET Minimal APIs.\n\nLightweight and fast, it is designed to be used in high-performance applications with no overhead.\n\nMinimal Mediator runs in-process without any serialization or reflection.\n\nNative AOT compilation is supported.\n\nSupport for publishing/sending messages.\n\nSupport for streaming requests and responses based on **System.Threading.Channels** and **IAsyncEnumerable**.\n\nThe implementation provides a simple middleware pipeline workflow for handling requests and responses that is easy to extend and customize. \n\n## Installation\nInstall the [MinimalMediator](https://www.nuget.org/packages/MinimalMediator) NuGet package.\n```bash\ndotnet add package MinimalMediator\n```\n\n## Initialization\nMinimal Mediator supports `Microsoft.Extensions.DependencyInjection` and `Microsoft.Extensions.Hosting` for dependency injection and configuration.\n\nThere are three possible ways to initialize the mediator services:\n- using **Source Generators** - automatically by scanning the assembly for handlers and generating the mediator code\n```csharp\nbuilder.Services.AddMinimalMediator(config =\u003e config.UseSourceGenerator());\n```\n- using **Reflection** - automatically by scanning the assembly for handlers and registering them in the service collection\n```csharp\nbuilder.Services.AddMinimalMediator(config =\u003e config.UseReflection(typeof(Program)));\n```\n\n- Manually by registering the mediator and handlers in the service collection\n```csharp\nbuilder.Services.AddMinimalMediator(config =\u003e\n{\n    config.AddConsumer(typeof(IConsumer\u003cTestMessage\u003e), typeof(Consumer2));\n    config.AddMiddleware(typeof(IAfterPublishMiddleware\u003cTestMessage\u003e), typeof(AfterMiddleware1));\n    config.AddReceiver(typeof(IReceiver\u003cTestMessage, TestResponse\u003e), typeof(ReceiverTest));\n});\n```\n\n## Usage\nMinimal Mediator supports two types of invocations:\n- **Publish/Subscribe** - broadcast a message to all consumers\n- **Request/Response** - send a message and receive a response. This also supports streaming requests and responses.\n\n### Publish/Subscribe\nMinimal Mediator supports publishing messages to all consumers that implement the `IConsumer\u003cTMessage\u003e` interface.\n```csharp\npublic interface IConsumer\u003cTMessage\u003e\n{\n    Task ConsumeAsync(TMessage message, CancellationToken cancellationToken);\n}\n```\nThe mediator will invoke all consumers in parallel.\n\n### Middleware\nBecause the mediator implements middleware pipelines, it is possible to add middleware that will be invoked before and after the message is published.\n\n`This behavior is only valid for the Publish/Subscribe invocation. The Request/Response invocation does not support middleware.`\n\nThere are three types of middleware:\n- **IBeforePublishMiddleware** - invoked before the message is published\n- **IAfterPublishMiddleware** - invoked after the message is published\n- **IExceptionHandlerMiddleware** - invoked if an exception is thrown during the message publishing\n\nMultiple middleware can be registered of a specific type/message and they will be invoked in the order they are registered.\n\nTo control the order of middleware, use the `Order` property of the `MinimalMediatorAttribute` class. The order is important only for middleware of the same type.\n\nThe presence of this attribute is not required. If it is not present, the middleware will be invoked in the order they are registered. \n\n```csharp\n[MinimalMediator(Order = 2)]\npublic class AfterMiddleware2 : IAfterPublishMiddleware\u003cTestMessage\u003e\n{\n    ...\n}\n```\n\n### Request/Response\nMinimal Mediator supports sending and receiving messages and streams. The functionalities are provided by the following interfaces:\n- **IReceiver\u003cTMessage, TResponse\u003e** - used for sending a message and receiving a response\n- **IReceiverStreamAsync\u003cTMessage, TResponse\u003e** - used for sending a **IAsyncEnumerable\u003cTMessage\u003e** stream of messages and receiving a TResponse message\n- **IReceiverStreamChannel\u003cTMessage, TResponse\u003e** - used for sending a **ChannelReader\u003cTMessage\u003e** stream of messages and receiving a TResponse message\n- **IReceiverConsumeStreamAsync\u003cTMessage, TResponse\u003e** - used for sending a TMessage message and receiving a **IAsyncEnumerable\u003cTResponse\u003e** stream of messages\n- **IReceiverConsumeStreamChannel\u003cTMessage, TResponse\u003e** - used for sending a TMessage message and receiving a **ChannelReader\u003cTResponse\u003e** stream of messages\n\n`See the sample project and tests for more details.`\n\n### Service Lifetime\nThe default lifetime of the **IMediator** service is **Singleton**. There is also a **Scoped** lifetime available that can be enabled from the configuration.\n\n```csharp\nbuilder.Services.AddMinimalMediator(config =\u003e config.UseSourceGenerator(), ServiceLifetime.Scoped);\n```\n\nWhen **IMediator** registered as **Scoped** lifetime, the mediator creates/manages its own scope that has the same lifetime as the **IMediator** instance.\n\n## License\n[MIT](https://choosealicense.com/licenses/mit/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvlada22%2Fminimal-mediator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvlada22%2Fminimal-mediator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvlada22%2Fminimal-mediator/lists"}