https://github.com/sajanv88/simple-mediatr
A lightweight and easy-to-use implementation of the MediatR pattern for .NET Core applications. This library provides a clean and decoupled way to send request, and publish notifications within your application.
https://github.com/sajanv88/simple-mediatr
asp-net-core async-communication decoupling notifications
Last synced: about 1 year ago
JSON representation
A lightweight and easy-to-use implementation of the MediatR pattern for .NET Core applications. This library provides a clean and decoupled way to send request, and publish notifications within your application.
- Host: GitHub
- URL: https://github.com/sajanv88/simple-mediatr
- Owner: sajanv88
- License: mit
- Created: 2025-04-09T09:21:34.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-09T10:28:49.000Z (about 1 year ago)
- Last Synced: 2025-06-24T02:03:19.350Z (about 1 year ago)
- Topics: asp-net-core, async-communication, decoupling, notifications
- Language: C#
- Homepage:
- Size: 19.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleMediatR
A lightweight and easy-to-use implementation of the MediatR pattern for
.NET Core applications. This library provides a clean and decoupled way to send request,
and publish notifications within your application.
If you are looking for a simple and effective way to manage communication between components in your .NET Core application,
SimpleMediatR is the perfect solution.
## ✨ Features
- Supports request/response and notification patterns
- Decouples business logic using the mediator design pattern
- Lightweight and minimal dependencies
- Easy to integrate into existing .NET Core projects
## 📦 Installation
```bash
dotnet add package SimpleMediatR
```
Or via NuGet Package Manager:
```powershell
Install-Package SimpleMediatR
```
## 🛠️ Usage
1. Register MediatR in your DI Container
```csharp
builder.Services.AddMediatR(); // Automatically registers all handlers in the assembly
```
2. Define a Request and its Handler
```csharp
class Ping : IRequest
{
public string Message { get; set; } = string.Empty;
}
class PingHandler : IRequestHandler
{
public Task HandleAsync(Ping request, CancellationToken cancellationToken)
{
return Task.FromResult($"Pong: {request.Message}");
}
}
var ping = new Ping { Message = "Hello, World!" };
// Inject `IMediatR mediatR` then use it
var result = await mediatR.SendAsync(ping);
logger.LogInformation($"Received response ({result})");
```
## 🔔 Notifications
You can also publish notifications to multiple handlers:
```csharp
class NewUserCreatedEvent : INotification
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public Guid Id { get; set; }
}
class NewUserNotificationHandler(ILogger logger) : INotificationHandler
{
public async Task HandleAsync(NewUserCreatedEvent newUser, CancellationToken cancellationToken)
{
logger.LogInformation($"Received notification: FirstName: {newUser.FirstName} LastName: {newUser.LastName} UserId: {newUser.Id}");
await Task.CompletedTask;
}
}
// Publishing when a new user is created
await mediatR.PublishAsync(new NewUserCreatedEvent
{
FirstName = request.User.FirstName,
LastName = request.User.LastName,
Id = request.User.Id
}, cancellationToken);
```
## More examples:
Checkout the examples folder for more usage examples.
## 🤝 Contributing
Contributions are welcome! Feel free to open issues, suggest features, or submit pull requests.