|
| 1 | +# Cortex.Mediator 🧠 |
| 2 | + |
| 3 | +**Cortex.Mediator** is a lightweight and extensible implementation of the Mediator pattern for .NET applications, designed to power clean, modular architectures like **Vertical Slice Architecture** and **CQRS**. |
| 4 | + |
| 5 | + |
| 6 | +Built as part of the [Cortex Data Framework](https://github.com/buildersoftio/cortex), this library simplifies command and query handling with built-in support for: |
| 7 | + |
| 8 | + |
| 9 | +- ✅ Commands & Queries |
| 10 | +- ✅ Notifications (Events) |
| 11 | +- ✅ Pipeline Behaviors |
| 12 | +- ✅ FluentValidation |
| 13 | +- ✅ Logging |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +[](https://github.com/buildersoftio/cortex/blob/master/LICENSE) |
| 18 | +[](https://www.nuget.org/packages/Cortex.Mediator) |
| 19 | +[](https://github.com/buildersoftio/cortex) |
| 20 | +[](https://discord.gg/JnMJV33QHu) |
| 21 | + |
| 22 | + |
| 23 | +## 🚀 Getting Started |
| 24 | + |
| 25 | +### Install via NuGet |
| 26 | + |
| 27 | +```bash |
| 28 | +dotnet add package Cortex.Mediator |
| 29 | +``` |
| 30 | + |
| 31 | +## 🛠️ Setup |
| 32 | +In `Program.cs` or `Startup.cs`: |
| 33 | +```csharp |
| 34 | +builder.Services.AddCortexMediator( |
| 35 | + builder.Configuration, |
| 36 | + new[] { typeof(Program) }, // Assemblies to scan for handlers |
| 37 | + options => options.AddDefaultBehaviors() // Logging |
| 38 | +); |
| 39 | +``` |
| 40 | + |
| 41 | +## 📦 Folder Structure Example (Vertical Slice) |
| 42 | +```bash |
| 43 | +Features/ |
| 44 | + CreateUser/ |
| 45 | + CreateUserCommand.cs |
| 46 | + CreateUserCommandHandler.cs |
| 47 | + CreateUserValidator.cs |
| 48 | + CreateUserEndpoint.cs |
| 49 | +``` |
| 50 | + |
| 51 | +## ✏️ Defining a Command |
| 52 | + |
| 53 | +```csharp |
| 54 | +public class CreateUserCommand : ICommand<Guid> |
| 55 | +{ |
| 56 | + public string UserName { get; set; } |
| 57 | + public string Email { get; set; } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Handler |
| 62 | +```csharp |
| 63 | +public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand,Guid> |
| 64 | +{ |
| 65 | + public async Task<Guid> Handle(CreateUserCommand command, CancellationToken cancellationToken) |
| 66 | + { |
| 67 | + // Logic here |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Validator (Optional, via FluentValidation) - Coming in the next release v1.8 |
| 73 | +```csharp |
| 74 | +public class CreateUserValidator : AbstractValidator<CreateUserCommand> |
| 75 | +{ |
| 76 | + public CreateUserValidator() |
| 77 | + { |
| 78 | + RuleFor(x => x.UserName).NotEmpty(); |
| 79 | + RuleFor(x => x.Email).NotEmpty().EmailAddress(); |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## 🔍 Defining a Query |
| 87 | + |
| 88 | +```csharp |
| 89 | +public class GetUserQuery : IQuery<GetUserResponse> |
| 90 | +{ |
| 91 | + public int UserId { get; set; } |
| 92 | +} |
| 93 | +``` |
| 94 | +```csharp |
| 95 | +public class GetUserQueryHandler : IQueryHandler<GetUserQuery, GetUserResponse> |
| 96 | +{ |
| 97 | + public async Task<GetUserResponse> Handle(GetUserQuery query, CancellationToken cancellationToken) |
| 98 | + { |
| 99 | + return new GetUserResponse { UserId = query.UserId, UserName = "Andy" }; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +## 📢 Notifications (Events) |
| 106 | + |
| 107 | +```csharp |
| 108 | +public class UserCreatedNotification : INotification |
| 109 | +{ |
| 110 | + public string UserName { get; set; } |
| 111 | +} |
| 112 | + |
| 113 | +public class SendWelcomeEmailHandler : INotificationHandler<UserCreatedNotification> |
| 114 | +{ |
| 115 | + public async Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken) |
| 116 | + { |
| 117 | + // Send email... |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | +```csharp |
| 122 | +await mediator.PublishAsync(new UserCreatedNotification { UserName = "Andy" }); |
| 123 | +``` |
| 124 | + |
| 125 | +## 🔧 Pipeline Behaviors (Built-in) |
| 126 | +Out of the box, Cortex.Mediator supports: |
| 127 | + |
| 128 | +- `ValidationCommandBehavior` - Coming in the next release v1.8 |
| 129 | +- `LoggingCommandBehavior` |
| 130 | + |
| 131 | +You can also register custom behaviors: |
| 132 | +```csharp |
| 133 | +options.AddOpenCommandPipelineBehavior(typeof(MyCustomBehavior<>)); |
| 134 | +``` |
| 135 | + |
| 136 | +## 💬 Contributing |
| 137 | +We welcome contributions from the community! Whether it's reporting bugs, suggesting features, or submitting pull requests, your involvement helps improve Cortex for everyone. |
| 138 | + |
| 139 | +### 💬 How to Contribute |
| 140 | +1. **Fork the Repository** |
| 141 | +2. **Create a Feature Branch** |
| 142 | +```bash |
| 143 | +git checkout -b feature/YourFeature |
| 144 | +``` |
| 145 | +3. **Commit Your Changes** |
| 146 | +```bash |
| 147 | +git commit -m "Add your feature" |
| 148 | +``` |
| 149 | +4. **Push to Your Fork** |
| 150 | +```bash |
| 151 | +git push origin feature/YourFeature |
| 152 | +``` |
| 153 | +5. **Open a Pull Request** |
| 154 | + |
| 155 | +Describe your changes and submit the pull request for review. |
| 156 | + |
| 157 | +## 📄 License |
| 158 | +This project is licensed under the MIT License. |
| 159 | + |
| 160 | +## 📚 Sponsorship |
| 161 | +Cortex is an open-source project maintained by BuilderSoft. Your support helps us continue developing and improving Cortex. Consider sponsoring us to contribute to the future of resilient streaming platforms. |
| 162 | + |
| 163 | +### How to Sponsor |
| 164 | +* **Financial Contributions**: Support us through [GitHub Sponsors](https://github.com/sponsors/buildersoftio) or other preferred platforms. |
| 165 | +* **Corporate Sponsorship**: If your organization is interested in sponsoring Cortex, please contact us directly. |
| 166 | + |
| 167 | +Contact Us: cortex@buildersoft.io |
| 168 | + |
| 169 | + |
| 170 | +## Contact |
| 171 | +We'd love to hear from you! Whether you have questions, feedback, or need support, feel free to reach out. |
| 172 | + |
| 173 | +- Email: support@buildersoft.io |
| 174 | +- Website: https://buildersoft.io |
| 175 | +- GitHub Issues: [Cortex Data Framework Issues](https://github.com/buildersoftio/cortex/issues) |
| 176 | +- Join our Discord Community: [](https://discord.gg/JnMJV33QHu) |
| 177 | + |
| 178 | + |
| 179 | +Thank you for using Cortex Data Framework! We hope it empowers you to build scalable and efficient data processing pipelines effortlessly. |
| 180 | + |
| 181 | +Built with ❤️ by the Buildersoft team. |
0 commit comments