{"id":30196241,"url":"https://github.com/knetic0/mesk.mediatr","last_synced_at":"2026-01-20T17:38:19.255Z","repository":{"id":308339119,"uuid":"1032464372","full_name":"knetic0/MESK.MediatR","owner":"knetic0","description":"⚡️A lightweight, fast, and simple implementation of the Mediator pattern for .NET 8, inspired by MediatR. This library provides in-process messaging with support for request/response, command handling, notification publishing, and pipeline behaviors.","archived":false,"fork":false,"pushed_at":"2025-08-05T10:51:58.000Z","size":215,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-05T12:37:36.306Z","etag":null,"topics":["cqrs","csharp","dotnet","mediator-design-pattern","mediator-pattern","mediatr"],"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/knetic0.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,"zenodo":null}},"created_at":"2025-08-05T10:46:58.000Z","updated_at":"2025-08-05T10:53:48.000Z","dependencies_parsed_at":"2025-08-05T12:37:38.806Z","dependency_job_id":"99a023b6-339a-4bc3-8e18-d4d962d8b5f4","html_url":"https://github.com/knetic0/MESK.MediatR","commit_stats":null,"previous_names":["knetic0/mesk.mediatr"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/knetic0/MESK.MediatR","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knetic0%2FMESK.MediatR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knetic0%2FMESK.MediatR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knetic0%2FMESK.MediatR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knetic0%2FMESK.MediatR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/knetic0","download_url":"https://codeload.github.com/knetic0/MESK.MediatR/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knetic0%2FMESK.MediatR/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270183606,"owners_count":24541341,"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","status":"online","status_checked_at":"2025-08-13T02:00:09.904Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cqrs","csharp","dotnet","mediator-design-pattern","mediator-pattern","mediatr"],"created_at":"2025-08-13T05:17:13.714Z","updated_at":"2026-01-20T17:38:19.228Z","avatar_url":"https://github.com/knetic0.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MESK.MediatR\n\nA lightweight, fast, and simple implementation of the Mediator pattern for .NET 8, inspired by MediatR. This library provides in-process messaging with support for request/response, command handling, notification publishing, and pipeline behaviors.\n\n## Features\n\n- 🚀 **Request/Response** - Send requests and receive responses\n- 📢 **Notifications** - Publish notifications to multiple handlers\n- 🔧 **Pipeline Behaviors** - Add cross-cutting concerns like logging, validation, caching\n- 📦 **Dependency Injection** - Built-in support for Microsoft.Extensions.DependencyInjection\n- ⚡ **Lightweight** - Minimal dependencies and overhead\n- 🎯 **.NET 8** - Built for the latest .NET version\n\n## Installation\n\nYou can install the package via NuGet Package Manager or by adding it directly to your project file:\n\n```xml\n\u003cPackageReference Include=\"MESK.MediatR\" Version=\"1.0.8\" /\u003e\n```\n\n## Quick Start\n\n### 1. Register Services\n\n```csharp\nusing MESK.MediatR;\n\nvar builder = WebApplication.CreateBuilder(args);\n\n// Register MediatR services\nbuilder.Services.AddMediatR(options =\u003e\n{\n    options.RegisterServicesFromAssembly(typeof(Program).Assembly);\n    // Register pipeline behaviors if needed\n    // options.RegisterPipelineBehavior(typeof(LoggingBehavior\u003c,\u003e));\n});\n\nvar app = builder.Build();\n```\n\n### 2. Create a Request and Handler\n\n```csharp\nusing MESK.MediatR;\n\n// Define a request\npublic record GetUserQuery(int Id) : IRequest\u003cUser\u003e;\n\n// Define the response model\npublic record User(int Id, string Name, string Email);\n\n// Implement the handler\npublic class GetUserHandler : IRequestHandler\u003cGetUserQuery, User\u003e\n{\n    public async Task\u003cUser\u003e Handle(GetUserQuery request, CancellationToken cancellationToken)\n    {\n        // Your business logic here\n        return new User(request.Id, \"John Doe\", \"john@example.com\");\n    }\n}\n```\n\n### 3. Send Request\n\n```csharp\npublic class UserController : ControllerBase\n{\n    private readonly ISender _sender;\n\n    public UserController(ISender sender)\n    {\n        _sender = sender;\n    }\n\n    [HttpGet(\"{id}\")]\n    public async Task\u003cUser\u003e GetUser(int id)\n    {\n        var query = new GetUserQuery(id);\n        return await _sender.Send(query);\n    }\n}\n```\n\n## Usage Examples\n\n### Commands (Request without Response)\n\n```csharp\n// Define a command\npublic record CreateUserCommand(string Name, string Email) : IRequest;\n\n// Implement the handler\npublic class CreateUserHandler : IRequestHandler\u003cCreateUserCommand\u003e\n{\n    public async Task Handle(CreateUserCommand request, CancellationToken cancellationToken)\n    {\n        // Create user logic\n        Console.WriteLine($\"Creating user: {request.Name}\");\n    }\n}\n\n// Usage\nawait _sender.Send(new CreateUserCommand(\"Jane Doe\", \"jane@example.com\"));\n```\n\n### Notifications\n\n```csharp\n// Define a notification\npublic record UserCreatedNotification(int UserId, string Name) : INotification;\n\n// Multiple handlers can handle the same notification\npublic class EmailNotificationHandler : INotificationHandler\u003cUserCreatedNotification\u003e\n{\n    public async Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken)\n    {\n        Console.WriteLine($\"Sending welcome email to user {notification.Name}\");\n    }\n}\n\npublic class LoggingNotificationHandler : INotificationHandler\u003cUserCreatedNotification\u003e\n{\n    public async Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken)\n    {\n        Console.WriteLine($\"User created: {notification.UserId}\");\n    }\n}\n\n// Usage - all handlers will be executed\nawait _sender.Publish(new UserCreatedNotification(1, \"John Doe\"));\n```\n\n### Pipeline Behaviors\n\nPipeline behaviors allow you to add cross-cutting concerns like logging, validation, caching, etc.\n\n```csharp\n// Create a logging behavior\npublic class LoggingBehavior\u003cTRequest, TResponse\u003e : IPipelineBehavior\u003cTRequest, TResponse\u003e\n    where TRequest : IRequest\u003cTResponse\u003e\n{\n    private readonly ILogger\u003cLoggingBehavior\u003cTRequest, TResponse\u003e\u003e _logger;\n\n    public LoggingBehavior(ILogger\u003cLoggingBehavior\u003cTRequest, TResponse\u003e\u003e logger)\n    {\n        _logger = logger;\n    }\n\n    public async Task\u003cTResponse\u003e Handle(\n        TRequest request, \n        RequestHandlerDelegate\u003cTResponse\u003e next, \n        CancellationToken cancellationToken)\n    {\n        _logger.LogInformation($\"Handling {typeof(TRequest).Name}\");\n        \n        var response = await next();\n        \n        _logger.LogInformation($\"Handled {typeof(TRequest).Name}\");\n        \n        return response;\n    }\n}\n\n// Register the behavior\nbuilder.Services.AddMediatR(options =\u003e\n{\n    options.RegisterServicesFromAssembly(typeof(Program).Assembly);\n    options.RegisterPipelineBehavior(typeof(LoggingBehavior\u003c,\u003e));\n});\n```\n\n### Validation Behavior\n\n```csharp\npublic class ValidationBehavior\u003cTRequest, TResponse\u003e : IPipelineBehavior\u003cTRequest, TResponse\u003e\n    where TRequest : IRequest\u003cTResponse\u003e\n{\n    private readonly IEnumerable\u003cIValidator\u003cTRequest\u003e\u003e _validators;\n\n    public ValidationBehavior(IEnumerable\u003cIValidator\u003cTRequest\u003e\u003e validators)\n    {\n        _validators = validators;\n    }\n\n    public async Task\u003cTResponse\u003e Handle(\n        TRequest request, \n        RequestHandlerDelegate\u003cTResponse\u003e next, \n        CancellationToken cancellationToken)\n    {\n        if (_validators.Any())\n        {\n            var context = new ValidationContext\u003cTRequest\u003e(request);\n            var validationResults = await Task.WhenAll(\n                _validators.Select(v =\u003e v.ValidateAsync(context, cancellationToken)));\n            \n            var failures = validationResults\n                .SelectMany(r =\u003e r.Errors)\n                .Where(f =\u003e f != null)\n                .ToList();\n\n            if (failures.Count != 0)\n                throw new ValidationException(failures);\n        }\n\n        return await next();\n    }\n}\n```\n\n## API Reference\n\n### Core Interfaces\n\n#### `IRequest` and `IRequest\u003cTResponse\u003e`\nBase interfaces for requests.\n\n#### `IRequestHandler\u003cTRequest\u003e` and `IRequestHandler\u003cTRequest, TResponse\u003e`\nInterfaces for handling requests.\n\n#### `INotification`\nBase interface for notifications.\n\n#### `INotificationHandler\u003cTNotification\u003e`\nInterface for handling notifications.\n\n#### `ISender`\nMain interface for sending requests and publishing notifications.\n\n#### `IPipelineBehavior\u003cTRequest\u003e` and `IPipelineBehavior\u003cTRequest, TResponse\u003e`\nInterfaces for implementing pipeline behaviors.\n\n### Configuration\n\n#### `MediatROptions`\nConfiguration class with methods:\n- `RegisterServicesFromAssembly(Assembly assembly)`\n- `RegisterServicesFromAssemblies(params Assembly[] assemblies)`\n- `RegisterPipelineBehavior(Type behaviorType)`\n\n## Best Practices\n\n1. **Use Records for Requests**: Records provide immutability and value equality.\n2. **Keep Handlers Focused**: Each handler should have a single responsibility.\n3. **Use Pipeline Behaviors for Cross-Cutting Concerns**: Logging, validation, caching, etc.\n4. **Async All the Way**: Always use async/await for non-blocking operations.\n5. **Use CancellationTokens**: Always respect cancellation tokens for better performance.\n\n## Performance Considerations\n\n- Handlers are resolved from DI container on each request\n- Pipeline behaviors are executed in reverse order of registration\n- Notifications are published to all handlers in parallel using `Task.WhenAll`\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknetic0%2Fmesk.mediatr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknetic0%2Fmesk.mediatr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknetic0%2Fmesk.mediatr/lists"}