diff --git a/README.md b/README.md index a4d0a94..76df3ba 100644 --- a/README.md +++ b/README.md @@ -116,9 +116,12 @@ - **Cortex.Telemetry.OpenTelemetry:** Adds support for Open Telemetry. [![NuGet Version](https://img.shields.io/nuget/v/Cortex.Telemetry.OpenTelemetry?label=Cortex.Telemetry.OpenTelemetry)](https://www.nuget.org/packages/Cortex.Telemetry.OpenTelemetry) -- **Cortex.Types:** Use complex types like OneOf, AllOf and AnyOf +- **Cortex.Types:** Use complex types like OneOf, AllOf and AnyOf. [![NuGet Version](https://img.shields.io/nuget/v/Cortex.Types?label=Cortex.Types)](https://www.nuget.org/packages/Cortex.Types) +- **Cortex.Mediator:** implementation of the Mediator pattern for .NET applications, designed to power clean, modular architectures like **Vertical Slice Architecture** and **CQRS**. +[![NuGet Version](https://img.shields.io/nuget/v/Cortex.Mediator?label=Cortex.Mediator)](https://www.nuget.org/packages/Cortex.Mediator) + ## Getting Started diff --git a/src/Cortex.Mediator/Cortex.Mediator.csproj b/src/Cortex.Mediator/Cortex.Mediator.csproj index fdb6c14..fd02b3e 100644 --- a/src/Cortex.Mediator/Cortex.Mediator.csproj +++ b/src/Cortex.Mediator/Cortex.Mediator.csproj @@ -1,7 +1,7 @@  - net9;net8;net7;net6 + net9;net8;net7;netstandard2.1 1.0.0 1.0.0 @@ -29,8 +29,22 @@ Just as the Cortex in our brains handles complex processing efficiently, Cortex Data Framework brings brainpower to your data management! https://buildersoft.io/ + Cortex Mediator + README.md + + + + + + + + True + \ + Always + + True @@ -49,9 +63,9 @@ - - - + + + diff --git a/src/Cortex.Mediator/README.md b/src/Cortex.Mediator/README.md new file mode 100644 index 0000000..d6ddb74 --- /dev/null +++ b/src/Cortex.Mediator/README.md @@ -0,0 +1,183 @@ +# Cortex.Mediator 🧠 + +**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**. + + +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: + + +- ✅ Commands & Queries +- ✅ Notifications (Events) +- ✅ Pipeline Behaviors +- ✅ FluentValidation +- ✅ Logging +- ✅ Transaction Handling + +--- + +[![GitHub License](https://img.shields.io/github/license/buildersoftio/cortex)](https://github.com/buildersoftio/cortex/blob/master/LICENSE) +[![NuGet Version](https://img.shields.io/nuget/v/Cortex.Mediator?label=Cortex.Mediator)](https://www.nuget.org/packages/Cortex.Mediator) +[![GitHub contributors](https://img.shields.io/github/contributors/buildersoftio/cortex)](https://github.com/buildersoftio/cortex) +[![Discord Shield](https://discord.com/api/guilds/1310034212371566612/widget.png?style=shield)](https://discord.gg/JnMJV33QHu) + + +## 🚀 Getting Started + +### Install via NuGet + +```bash +dotnet add package Cortex.Mediator +``` + +## 🛠️ Setup +In `Program.cs` or `Startup.cs`: +```csharp +builder.Services.AddCortexMediator( + builder.Configuration, + new[] { typeof(Program) }, // Assemblies to scan for handlers + options => options.AddDefaultBehaviors() // Logging, Validation, Transaction +); +``` + +## 📦 Folder Structure Example (Vertical Slice) +```bash +Features/ + CreateUser/ + CreateUserCommand.cs + CreateUserCommandHandler.cs + CreateUserValidator.cs + CreateUserEndpoint.cs +``` + +## ✏️ Defining a Command + +```csharp +public class CreateUserCommand : ICommand +{ + public string UserName { get; set; } + public string Email { get; set; } +} +``` + +### Handler +```csharp +public class CreateUserCommandHandler : ICommandHandler +{ + public async Task Handle(CreateUserCommand command, CancellationToken cancellationToken) + { + // Logic here + } +} +``` + +### Validator (Optional, via FluentValidation) +```csharp +public class CreateUserValidator : AbstractValidator +{ + public CreateUserValidator() + { + RuleFor(x => x.UserName).NotEmpty(); + RuleFor(x => x.Email).NotEmpty().EmailAddress(); + } +} +``` + +--- + +## 🔍 Defining a Query + +```csharp +public class GetUserQuery : IQuery +{ + public int UserId { get; set; } +} +``` +```csharp +public class GetUserQueryHandler : IQueryHandler +{ + public async Task Handle(GetUserQuery query, CancellationToken cancellationToken) + { + return new GetUserResponse { UserId = query.UserId, UserName = "Andy" }; + } +} + +``` + +## 📢 Notifications (Events) + +```csharp +public class UserCreatedNotification : INotification +{ + public string UserName { get; set; } +} + +public class SendWelcomeEmailHandler : INotificationHandler +{ + public async Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken) + { + // Send email... + } +} +``` +```csharp +await mediator.PublishAsync(new UserCreatedNotification { UserName = "Andy" }); +``` + +## 🔧 Pipeline Behaviors (Built-in) +Out of the box, Cortex.Mediator supports: + +- `ValidationCommandBehavior` +- `LoggingCommandBehavior` +- `TransactionCommandBehavior` + +You can also register custom behaviors: +```csharp +options.AddOpenCommandPipelineBehavior(typeof(MyCustomBehavior<>)); +``` + +## 💬 Contributing +We welcome contributions from the community! Whether it's reporting bugs, suggesting features, or submitting pull requests, your involvement helps improve Cortex for everyone. + +### 💬 How to Contribute +1. **Fork the Repository** +2. **Create a Feature Branch** +```bash +git checkout -b feature/YourFeature +``` +3. **Commit Your Changes** +```bash +git commit -m "Add your feature" +``` +4. **Push to Your Fork** +```bash +git push origin feature/YourFeature +``` +5. **Open a Pull Request** + +Describe your changes and submit the pull request for review. + +## 📄 License +This project is licensed under the MIT License. + +## 📚 Sponsorship +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. + +### How to Sponsor +* **Financial Contributions**: Support us through [GitHub Sponsors](https://github.com/sponsors/buildersoftio) or other preferred platforms. +* **Corporate Sponsorship**: If your organization is interested in sponsoring Cortex, please contact us directly. + +Contact Us: cortex@buildersoft.io + + +## Contact +We'd love to hear from you! Whether you have questions, feedback, or need support, feel free to reach out. + +- Email: cortex@buildersoft.io +- Website: https://buildersoft.io +- GitHub Issues: [Cortex Data Framework Issues](https://github.com/buildersoftio/cortex/issues) +- Join our Discord Community: [![Discord Shield](https://discord.com/api/guilds/1310034212371566612/widget.png?style=shield)](https://discord.gg/JnMJV33QHu) + + +Thank you for using Cortex Data Framework! We hope it empowers you to build scalable and efficient data processing pipelines effortlessly. + +Built with ❤️ by the Buildersoft team. diff --git a/src/Cortex.States.ClickHouse/Cortex.States.ClickHouse.csproj b/src/Cortex.States.ClickHouse/Cortex.States.ClickHouse.csproj index 92a3681..96a8544 100644 --- a/src/Cortex.States.ClickHouse/Cortex.States.ClickHouse.csproj +++ b/src/Cortex.States.ClickHouse/Cortex.States.ClickHouse.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/Cortex.States.MSSqlServer/Cortex.States.MSSqlServer.csproj b/src/Cortex.States.MSSqlServer/Cortex.States.MSSqlServer.csproj index 7ca2e78..787a452 100644 --- a/src/Cortex.States.MSSqlServer/Cortex.States.MSSqlServer.csproj +++ b/src/Cortex.States.MSSqlServer/Cortex.States.MSSqlServer.csproj @@ -46,7 +46,7 @@ - + diff --git a/src/Cortex.States.MongoDb/Cortex.States.MongoDb.csproj b/src/Cortex.States.MongoDb/Cortex.States.MongoDb.csproj index 2135316..2e96daa 100644 --- a/src/Cortex.States.MongoDb/Cortex.States.MongoDb.csproj +++ b/src/Cortex.States.MongoDb/Cortex.States.MongoDb.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/Cortex.States.PostgreSQL/Cortex.States.PostgreSQL.csproj b/src/Cortex.States.PostgreSQL/Cortex.States.PostgreSQL.csproj index 84285e2..5941724 100644 --- a/src/Cortex.States.PostgreSQL/Cortex.States.PostgreSQL.csproj +++ b/src/Cortex.States.PostgreSQL/Cortex.States.PostgreSQL.csproj @@ -45,7 +45,7 @@ - + diff --git a/src/Cortex.States.RocksDb/Cortex.States.RocksDb.csproj b/src/Cortex.States.RocksDb/Cortex.States.RocksDb.csproj index 44c31b0..4c7d902 100644 --- a/src/Cortex.States.RocksDb/Cortex.States.RocksDb.csproj +++ b/src/Cortex.States.RocksDb/Cortex.States.RocksDb.csproj @@ -46,7 +46,7 @@ - + diff --git a/src/Cortex.States.SQLite/Cortex.States.SQLite.csproj b/src/Cortex.States.SQLite/Cortex.States.SQLite.csproj index 7581fd5..aefe0c6 100644 --- a/src/Cortex.States.SQLite/Cortex.States.SQLite.csproj +++ b/src/Cortex.States.SQLite/Cortex.States.SQLite.csproj @@ -45,7 +45,7 @@ - + diff --git a/src/Cortex.Streams.AWSSQS/Cortex.Streams.AWSSQS.csproj b/src/Cortex.Streams.AWSSQS/Cortex.Streams.AWSSQS.csproj index 36b4f05..8d86452 100644 --- a/src/Cortex.Streams.AWSSQS/Cortex.Streams.AWSSQS.csproj +++ b/src/Cortex.Streams.AWSSQS/Cortex.Streams.AWSSQS.csproj @@ -51,7 +51,7 @@ - + diff --git a/src/Cortex.Streams.AzureBlobStorage/Cortex.Streams.AzureBlobStorage.csproj b/src/Cortex.Streams.AzureBlobStorage/Cortex.Streams.AzureBlobStorage.csproj index 2825d59..3ba9e1d 100644 --- a/src/Cortex.Streams.AzureBlobStorage/Cortex.Streams.AzureBlobStorage.csproj +++ b/src/Cortex.Streams.AzureBlobStorage/Cortex.Streams.AzureBlobStorage.csproj @@ -51,8 +51,8 @@ - - + + diff --git a/src/Cortex.Streams.AzureServiceBus/Cortex.Streams.AzureServiceBus.csproj b/src/Cortex.Streams.AzureServiceBus/Cortex.Streams.AzureServiceBus.csproj index 54f975e..1e66dc9 100644 --- a/src/Cortex.Streams.AzureServiceBus/Cortex.Streams.AzureServiceBus.csproj +++ b/src/Cortex.Streams.AzureServiceBus/Cortex.Streams.AzureServiceBus.csproj @@ -51,8 +51,8 @@ - - + + diff --git a/src/Cortex.Streams.Elasticsearch/Cortex.Streams.Elasticsearch.csproj b/src/Cortex.Streams.Elasticsearch/Cortex.Streams.Elasticsearch.csproj index 7c4a12d..4cdba7a 100644 --- a/src/Cortex.Streams.Elasticsearch/Cortex.Streams.Elasticsearch.csproj +++ b/src/Cortex.Streams.Elasticsearch/Cortex.Streams.Elasticsearch.csproj @@ -45,8 +45,8 @@ - - + + diff --git a/src/Cortex.Streams.Kafka/Cortex.Streams.Kafka.csproj b/src/Cortex.Streams.Kafka/Cortex.Streams.Kafka.csproj index 67b0f6a..bd11617 100644 --- a/src/Cortex.Streams.Kafka/Cortex.Streams.Kafka.csproj +++ b/src/Cortex.Streams.Kafka/Cortex.Streams.Kafka.csproj @@ -52,9 +52,9 @@ - - - + + + diff --git a/src/Cortex.Streams.MSSqlServer/Cortex.Streams.MSSqlServer.csproj b/src/Cortex.Streams.MSSqlServer/Cortex.Streams.MSSqlServer.csproj index 13fb809..163be79 100644 --- a/src/Cortex.Streams.MSSqlServer/Cortex.Streams.MSSqlServer.csproj +++ b/src/Cortex.Streams.MSSqlServer/Cortex.Streams.MSSqlServer.csproj @@ -45,8 +45,8 @@ - - + + diff --git a/src/Cortex.Streams.MongoDb/Cortex.Streams.MongoDb.csproj b/src/Cortex.Streams.MongoDb/Cortex.Streams.MongoDb.csproj index 97807be..7ac2907 100644 --- a/src/Cortex.Streams.MongoDb/Cortex.Streams.MongoDb.csproj +++ b/src/Cortex.Streams.MongoDb/Cortex.Streams.MongoDb.csproj @@ -45,7 +45,7 @@ - + diff --git a/src/Cortex.Streams.PostgreSQL/Cortex.Streams.PostgreSQL.csproj b/src/Cortex.Streams.PostgreSQL/Cortex.Streams.PostgreSQL.csproj index cecee4b..09abc2d 100644 --- a/src/Cortex.Streams.PostgreSQL/Cortex.Streams.PostgreSQL.csproj +++ b/src/Cortex.Streams.PostgreSQL/Cortex.Streams.PostgreSQL.csproj @@ -45,7 +45,7 @@ - + diff --git a/src/Cortex.Streams.Pulsar/Cortex.Streams.Pulsar.csproj b/src/Cortex.Streams.Pulsar/Cortex.Streams.Pulsar.csproj index 9f64003..52ddf04 100644 --- a/src/Cortex.Streams.Pulsar/Cortex.Streams.Pulsar.csproj +++ b/src/Cortex.Streams.Pulsar/Cortex.Streams.Pulsar.csproj @@ -53,10 +53,10 @@ - + - - + + diff --git a/src/Cortex.Streams.S3/Cortex.Streams.S3.csproj b/src/Cortex.Streams.S3/Cortex.Streams.S3.csproj index 268d309..b6b978c 100644 --- a/src/Cortex.Streams.S3/Cortex.Streams.S3.csproj +++ b/src/Cortex.Streams.S3/Cortex.Streams.S3.csproj @@ -51,7 +51,7 @@ - + diff --git a/src/Cortex.Telemetry.OpenTelemetry/Cortex.Telemetry.OpenTelemetry.csproj b/src/Cortex.Telemetry.OpenTelemetry/Cortex.Telemetry.OpenTelemetry.csproj index c4f1d6c..9490770 100644 --- a/src/Cortex.Telemetry.OpenTelemetry/Cortex.Telemetry.OpenTelemetry.csproj +++ b/src/Cortex.Telemetry.OpenTelemetry/Cortex.Telemetry.OpenTelemetry.csproj @@ -51,10 +51,10 @@ - - - - + + + + diff --git a/src/Cortex.Tests/Cortex.Tests.csproj b/src/Cortex.Tests/Cortex.Tests.csproj index 8b8a08e..406cf1b 100644 --- a/src/Cortex.Tests/Cortex.Tests.csproj +++ b/src/Cortex.Tests/Cortex.Tests.csproj @@ -10,14 +10,14 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive