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
46 changes: 44 additions & 2 deletions Cortex.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34607.79
# Visual Studio Version 18
VisualStudioVersion = 18.0.11111.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Mediator", "src\Cortex.Mediator\Cortex.Mediator.csproj", "{F1CC775A-95DA-4A5A-879F-66BFCB0FDCC9}"
EndProject
Expand Down Expand Up @@ -60,6 +60,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Vectors", "src\Corte
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Mediator.Behaviors.FluentValidation", "src\Cortex.Mediator.Behaviors.FluentValidation\Cortex.Mediator.Behaviors.FluentValidation.csproj", "{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cortex.Serialization.Yaml", "src\Cortex.Serialization.Yaml\Cortex.Serialization.Yaml.csproj", "{472BC645-9E2F-4205-A571-4D9184747EC5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Mediator", "Mediator", "{1C5D462D-168D-4D3F-B96E-CCE5517DB197}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Streams", "Streams", "{4C68702C-1661-4AD9-83FD-E0B52B791969}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "States", "States", "{C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Serialization", "Serialization", "{7F9E0AEA-721E-46F8-90ED-8EA8423647FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +129,7 @@ Global
{D51C6B82-ABD9-4C43-820E-237EDBD706A1}.Release|Any CPU.ActiveCfg = Release|AnyCPU
{D51C6B82-ABD9-4C43-820E-237EDBD706A1}.Release|Any CPU.Build.0 = Release|AnyCPU
{C9A7699A-9BCF-4B51-B29F-ABF78DCEA553}.Debug|Any CPU.ActiveCfg = Debug|AnyCPU
{C9A7699A-9BCF-4B51-B29F-ABF78DCEA553}.Debug|Any CPU.Build.0 = Debug|AnyCPU
{C9A7699A-9BCF-4B51-B29F-ABF78DCEA553}.Release|Any CPU.ActiveCfg = Release|AnyCPU
{C9A7699A-9BCF-4B51-B29F-ABF78DCEA553}.Release|Any CPU.Build.0 = Release|AnyCPU
{D376D6CA-3192-4EDC-B840-31F58B6457DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -181,10 +192,41 @@ Global
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73}.Release|Any CPU.Build.0 = Release|Any CPU
{472BC645-9E2F-4205-A571-4D9184747EC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{472BC645-9E2F-4205-A571-4D9184747EC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{472BC645-9E2F-4205-A571-4D9184747EC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{472BC645-9E2F-4205-A571-4D9184747EC5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F1CC775A-95DA-4A5A-879F-66BFCB0FDCC9} = {1C5D462D-168D-4D3F-B96E-CCE5517DB197}
{1C8605F4-91CB-49A4-A080-0A6DFE1FB010} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{96701658-663E-41C5-9EF9-843C79CE727A} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{EE799E10-7469-428E-AF8C-F2807F6CE7E5} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{34CA231A-1E3A-4CA4-820C-946DC8E2737F} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{16FA00B1-973B-443C-BF84-9B76DCAF341B} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{1F60F66A-5DC0-4C1D-A7B7-66F568A26911} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{2B949637-FC31-4F51-A391-19F76B57CC28} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{6E5AC0AC-A364-4DB3-9E9A-C2FB54BD6D1E} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{6019F9E6-C377-416D-9E3B-3D7104FAEB63} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{20FAE1F1-D677-4E0E-B3A7-E2B1485C874C} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{D376D6CA-3192-4EDC-B840-31F58B6457DD} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{447970B9-C5AA-41D9-A07F-330A251597D0} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{00358701-D117-4953-A673-D60625D38466} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{77AD462F-A248-43AF-9212-43031F22F23D} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{980EDBFE-40C2-4EFD-96C2-FED1032FB5E6} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{0F9FCB99-D00F-4396-8E2B-6E627076ADA0} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{20BD7107-8199-4CA8-815B-4D156B522B82} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{19167D25-6383-46B4-9449-B9E364F809FF} = {C31F8C0F-8BCF-4959-9BA1-8645D058EAA0}
{81A01446-A8AA-4F9D-BB9B-B66E21B2C348} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{0E60F75D-C44B-428A-9252-A11C365E2C56} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{FC86D3AB-778D-45D7-AF36-1F89FC16DE55} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{4D1F117D-48D7-47AD-9DAC-3B2DB45E628A} = {4C68702C-1661-4AD9-83FD-E0B52B791969}
{44A166BD-01E9-4A4B-9BC5-7DE01B472E73} = {1C5D462D-168D-4D3F-B96E-CCE5517DB197}
{472BC645-9E2F-4205-A571-4D9184747EC5} = {7F9E0AEA-721E-46F8-90ED-8EA8423647FB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E20303B6-8AC9-4FFF-B645-4608309ADA94}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<Version>1.0.0</Version>
<PackageLicenseFile>license.md</PackageLicenseFile>
<PackageIcon>andyX.png</PackageIcon>
<PackageIcon>cortex.png</PackageIcon>
<PackageId>Cortex.Mediator.Behaviors.FluentValidation</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IsPublishable>True</IsPublishable>
Expand All @@ -46,7 +46,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<None Include="Assets\andyX.png">
<None Include="Assets\cortex.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Cortex.Mediator.DependencyInjection;

namespace Cortex.Mediator.Behaviors.FluentValidation.DependencyInjection
{
public static class MediatorOptionsExtensions
{
public static MediatorOptions AddFluentValidationBehaviors(this MediatorOptions options)
{
options.AddOpenCommandPipelineBehavior(typeof(ValidationCommandBehavior<,>))
.AddOpenQueryPipelineBehavior(typeof(ValidationQueryBehavior<,>))
.AddOpenCommandPipelineBehavior(typeof(ValidationCommandBehavior<>));

return options;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;

namespace Cortex.Mediator.Behaviors.FluentValidation.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddFluentValidationValidators(this IServiceCollection services, Type[] validationAssemblyMarkerTypes)
{
services.AddValidatorsFromAssemblies(validationAssemblyMarkerTypes.Select(t => t.Assembly));
return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Cortex.Mediator.Commands;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -16,12 +15,17 @@ public sealed class ValidationCommandBehavior<TCommand, TResult> : ICommandPipel
{
private readonly IEnumerable<IValidator<TCommand>> _validators;

public ValidationCommandBehavior(IEnumerable<IValidator<TCommand>> validators)
{
_validators = validators;
}

public async Task<TResult> Handle(TCommand command, CommandHandlerDelegate<TResult> next, CancellationToken cancellationToken)
{
var context = new ValidationContext<TCommand>(command);
var failures = _validators
.Select(v => v.Validate(context))
.Select(async v => await v.ValidateAsync(context))
.Select(r => r.Result)
.SelectMany(r => r.Errors)
.Where(f => f != null)
.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ public sealed class ValidationQueryBehavior<TQuery, TResult> : IQueryPipelineBeh

private readonly IEnumerable<IValidator<TQuery>> _validators;

public ValidationQueryBehavior(IEnumerable<IValidator<TQuery>> validators)
{
_validators = validators;
}


public async Task<TResult> Handle(TQuery query, QueryHandlerDelegate<TResult> next, CancellationToken cancellationToken)
{
var context = new ValidationContext<TQuery>(query);
var failures = _validators
.Select(v => v.Validate(context))
.Select(async v => await v.ValidateAsync(context))
.Select(r => r.Result)
.SelectMany(r => r.Errors)
.Where(f => f != null)
.ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Cortex.Mediator.Commands;
using FluentValidation;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Cortex.Mediator.Behaviors
{
/// <summary>
/// Pipeline behavior for validation command execution.
/// </summary>
public sealed class ValidationCommandBehavior<TCommand> : ICommandPipelineBehavior<TCommand>
where TCommand : ICommand
{
private readonly IEnumerable<IValidator<TCommand>> _validators;

public ValidationCommandBehavior(IEnumerable<IValidator<TCommand>> validators)
{
_validators = validators;
}


public async Task Handle(TCommand command, CommandHandlerDelegate next, CancellationToken cancellationToken)
{
var context = new ValidationContext<TCommand>(command);
var failures = _validators
.Select(async v => await v.ValidateAsync(context))
.Select(r => r.Result)
.SelectMany(r => r.Errors)
.Where(f => f != null)
.ToList();

if (failures.Count() > 0)
{
var errors = failures
.GroupBy(f => f.PropertyName)
.ToDictionary(
g => g.Key,
g => g.Select(f => f.ErrorMessage).ToArray());

throw new Exceptions.ValidationException(errors);
}

await next();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCortexMediator(
this IServiceCollection services,
IConfiguration configuration,
Type[] handlerAssemblyMarkerTypes,
Action<MediatorOptions>? configure = null)
Action<MediatorOptions> configure = null)
{
var options = new MediatorOptions();
configure?.Invoke(options);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/Cortex.Serialization.Yaml/Assets/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2025 Buildersoft

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 changes: 47 additions & 0 deletions src/Cortex.Serialization.Yaml/Attributes/YamlIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace Cortex.Serialization.Yaml.Attributes
{
/// <summary>
/// Indicates that a field or property should be ignored during YAML serialization and deserialization.
/// </summary>
/// <remarks>
/// When applied to a field or property, this attribute instructs the YAML serializer to completely
/// exclude the member from both serialization (writing to YAML) and deserialization (reading from YAML).
/// This is useful for:
/// <list type="bullet">
/// <item>Excluding computed or derived properties that shouldn't be persisted</item>
/// <item>Ignoring internal state or temporary fields that are irrelevant to serialization</item>
/// <item>Preventing sensitive data (like passwords or tokens) from being serialized</item>
/// <item>Omitting properties that would cause circular references during serialization</item>
/// </list>
/// </remarks>
/// <example>
/// The following example shows how to use YamlIgnoreAttribute to exclude properties from YAML serialization:
/// <code>
/// public class UserSettings
/// {
/// public string UserName { get; set; }
/// public string Email { get; set; }
///
/// [YamlIgnore]
/// public string PasswordHash { get; set; }
///
/// [YamlIgnore]
/// public DateTime LastAccess { get; set; } // Computed property
///
/// // This property will be serialized normally
/// public bool IsActive { get; set; }
/// }
/// </code>
/// When serialized to YAML, the output will not include the PasswordHash and LastAccess properties:
/// <code>
/// UserName: john_doe
/// Email: john@example.com
/// IsActive: true
/// </code>
/// </example>
/// <seealso cref="YamlPropertyAttribute"/>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class YamlIgnoreAttribute : Attribute { }
}
49 changes: 49 additions & 0 deletions src/Cortex.Serialization.Yaml/Attributes/YamlPropertyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;

namespace Cortex.Serialization.Yaml.Attributes
{
/// <summary>
/// Specifies a custom YAML property name for a field or property during serialization and deserialization.
/// </summary>
/// <remarks>
/// When applied to a field or property, this attribute allows you to specify an alternative name
/// that should be used in the YAML representation, different from the actual member name in code.
/// This is particularly useful for:
/// <list type="bullet">
/// <item>Mapping between different naming conventions (e.g., camelCase in YAML and PascalCase in C#)</item>
/// <item>Using YAML property names that are not valid C# identifiers</item>
/// <item>Maintaining compatibility with existing YAML schemas</item>
/// </list>
/// </remarks>
/// <example>
/// The following example shows how to use YamlPropertyAttribute to specify custom YAML property names:
/// <code>
/// public class SerializableObject
/// {
/// [YamlProperty(Name = "output-format")]
/// public string OutputFormat { get; set; }
///
/// [YamlProperty(Name = "maxItems")]
/// public int MaximumItems { get; set; }
/// }
/// </code>
/// This will serialize to YAML as:
/// <code>
/// output-format: SomeValue
/// maxItems: 42
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public sealed class YamlPropertyAttribute : Attribute
{
/// <summary>
/// Gets the custom name to use for the YAML property.
/// If null or empty, the default name (the member name) will be used.
/// </summary>
/// <value>
/// A string representing the custom property name to use in YAML serialization,
/// or null to use the default behavior.
/// </value>
public string? Name { get; init; }
}
}
25 changes: 25 additions & 0 deletions src/Cortex.Serialization.Yaml/Common/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Cortex.Serialization.Yaml.Common
{
internal static class StringUtils
{
public static string ToSnakeCase(string s)
{
if (string.IsNullOrEmpty(s)) return s;
var sb = new System.Text.StringBuilder();
for (int i = 0; i < s.Length; i++)
{
var c = s[i];
if (char.IsUpper(c))
{
if (i > 0) sb.Append('_');
sb.Append(char.ToLowerInvariant(c));
}
else
sb.Append(c);
}
return sb.ToString();
}
public static string ToKebabCase(string s) => ToSnakeCase(s).Replace('_', '-');
public static string ToCamelCase(string s) => string.IsNullOrEmpty(s) || !char.IsUpper(s[0]) ? s : char.ToLowerInvariant(s[0]) + s[1..];
}
}
Loading
Loading