Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 18 additions & 4 deletions src/Cortex.Mediator/Cortex.Mediator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net9;net8;net7;net6</TargetFrameworks>
<TargetFrameworks>net9;net8;net7;netstandard2.1</TargetFrameworks>

<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
Expand Down Expand Up @@ -29,8 +29,22 @@
<RepositoryType></RepositoryType>
<PackageReleaseNotes>Just as the Cortex in our brains handles complex processing efficiently, Cortex Data Framework brings brainpower to your data management! </PackageReleaseNotes>
<PackageProjectUrl>https://buildersoft.io/</PackageProjectUrl>
<Title>Cortex Mediator</Title>
<PackageReadmeFile>README.md</PackageReadmeFile>

</PropertyGroup>

<ItemGroup>
<None Remove="README.md" />
</ItemGroup>

<ItemGroup>
<Content Include="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="Assets\andyX.png">
<Pack>True</Pack>
Expand All @@ -49,9 +63,9 @@
<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.11.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
<PackageReference Include="Scrutor" Version="6.0.1" />
</ItemGroup>
</Project>
183 changes: 183 additions & 0 deletions src/Cortex.Mediator/README.md
Original file line number Diff line number Diff line change
@@ -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<CreateUserCommand>
{
public async Task Handle(CreateUserCommand command, CancellationToken cancellationToken)
{
// Logic here
}
}
```

### Validator (Optional, via FluentValidation)
```csharp
public class CreateUserValidator : AbstractValidator<CreateUserCommand>
{
public CreateUserValidator()
{
RuleFor(x => x.UserName).NotEmpty();
RuleFor(x => x.Email).NotEmpty().EmailAddress();
}
}
```

---

## 🔍 Defining a Query

```csharp
public class GetUserQuery : IQuery<GetUserResponse>
{
public int UserId { get; set; }
}
```
```csharp
public class GetUserQueryHandler : IQueryHandler<GetUserQuery, GetUserResponse>
{
public async Task<GetUserResponse> 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<UserCreatedNotification>
{
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.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ClickHouse.Client" Version="7.9.1" />
<PackageReference Include="ClickHouse.Client" Version="7.13.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@


<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Cortex.States.MongoDb/Cortex.States.MongoDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="3.1.0" />
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="9.0.2" />
<PackageReference Include="Npgsql" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Cortex.States.RocksDb/Cortex.States.RocksDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@


<ItemGroup>
<PackageReference Include="RocksDB" Version="9.4.0.50294" />
<PackageReference Include="RocksDB" Version="9.10.0.55496" />
</ItemGroup>


Expand Down
2 changes: 1 addition & 1 deletion src/Cortex.States.SQLite/Cortex.States.SQLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Cortex.Streams.AWSSQS/Cortex.Streams.AWSSQS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.SQS" Version="3.7.400.49" />
<PackageReference Include="AWSSDK.SQS" Version="3.7.400.129" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.23.0" />
<PackageReference Include="Polly" Version="8.5.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.24.0" />
<PackageReference Include="Polly" Version="8.5.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.2" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.4" />
<PackageReference Include="System.Text.Json" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.17.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.17.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Cortex.Streams.Kafka/Cortex.Streams.Kafka.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@


<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="2.6.0" />
<PackageReference Include="Google.Protobuf" Version="3.28.3" />
<PackageReference Include="protobuf-net" Version="3.2.45" />
<PackageReference Include="Confluent.Kafka" Version="2.9.0" />
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
<PackageReference Include="protobuf-net" Version="3.2.46" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Cortex.Streams.MongoDb/Cortex.Streams.MongoDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="3.1.0" />
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="9.0.2" />
<PackageReference Include="Npgsql" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Cortex.Streams.Pulsar/Cortex.Streams.Pulsar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@


<ItemGroup>
<PackageReference Include="DotPulsar" Version="3.4.0" />
<PackageReference Include="DotPulsar" Version="4.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Google.Protobuf" Version="3.28.3" />
<PackageReference Include="protobuf-net" Version="3.2.45" />
<PackageReference Include="Google.Protobuf" Version="3.30.2" />
<PackageReference Include="protobuf-net" Version="3.2.46" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading