Skip to content

Commit 16bf3c6

Browse files
committed
Added SOFTURE.Common.CQRS project with middleware and validation behavior
1 parent dfc6e9a commit 16bf3c6

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

API/API.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.Observabilit
1414
EndProject
1515
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.Resilience", "SOFTURE.Common.Resilience\SOFTURE.Common.Resilience.csproj", "{8518B31B-05FC-43A8-87BB-8732037F3562}"
1616
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.CQRS", "SOFTURE.Common.CQRS\SOFTURE.Common.CQRS.csproj", "{1AC78C07-8CFE-4F97-9111-427F8F074797}"
18+
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1921
Debug|Any CPU = Debug|Any CPU
@@ -48,5 +50,9 @@ Global
4850
{8518B31B-05FC-43A8-87BB-8732037F3562}.Debug|Any CPU.Build.0 = Debug|Any CPU
4951
{8518B31B-05FC-43A8-87BB-8732037F3562}.Release|Any CPU.ActiveCfg = Release|Any CPU
5052
{8518B31B-05FC-43A8-87BB-8732037F3562}.Release|Any CPU.Build.0 = Release|Any CPU
53+
{1AC78C07-8CFE-4F97-9111-427F8F074797}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54+
{1AC78C07-8CFE-4F97-9111-427F8F074797}.Debug|Any CPU.Build.0 = Debug|Any CPU
55+
{1AC78C07-8CFE-4F97-9111-427F8F074797}.Release|Any CPU.ActiveCfg = Release|Any CPU
56+
{1AC78C07-8CFE-4F97-9111-427F8F074797}.Release|Any CPU.Build.0 = Release|Any CPU
5157
EndGlobalSection
5258
EndGlobal
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using CSharpFunctionalExtensions;
2+
using FluentValidation;
3+
using MediatR;
4+
using SOFTURE.Results;
5+
6+
namespace SOFTURE.Common.CQRS.Middlewares.Behaviours
7+
{
8+
internal class CommandValidationBehaviour<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators)
9+
: IPipelineBehavior<TRequest, Result>
10+
where TRequest : IRequest<Result>
11+
{
12+
public async Task<Result> Handle(
13+
TRequest request,
14+
RequestHandlerDelegate<Result> next,
15+
CancellationToken cancellationToken)
16+
{
17+
if (!validators.Any()) return await next();
18+
19+
var context = new ValidationContext<TRequest>(request);
20+
21+
var failures = validators
22+
.Select(v => v.Validate(context))
23+
.SelectMany(result => result.Errors)
24+
.Where(f => f != null)
25+
.Select(f => new Error(f.PropertyName, f.ErrorMessage).Build())
26+
.ToList();
27+
28+
if (failures.Count != 0)
29+
{
30+
return Result.Failure<TResponse>(failures.First());
31+
}
32+
33+
return await next();
34+
}
35+
}
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using MediatR;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using SOFTURE.Common.CQRS.Middlewares.Behaviours;
4+
5+
namespace SOFTURE.Common.CQRS.Middlewares
6+
{
7+
public static class DependencyInjection
8+
{
9+
public static IServiceCollection AddMiddlewares(this IServiceCollection services)
10+
{
11+
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CommandValidationBehaviour<,>));
12+
13+
return services;
14+
}
15+
}
16+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
10+
<PackageReference Include="FluentValidation" Version="11.11.0" />
11+
<PackageReference Include="MediatR" Version="12.4.1" />
12+
<PackageReference Include="SOFTURE.Results" Version="0.1.1" />
13+
</ItemGroup>
14+
15+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
16+
<PackageReference Include="FluentValidation" Version="11.11.0" />
17+
<PackageReference Include="MediatR" Version="12.4.1" />
18+
<PackageReference Include="SOFTURE.Results" Version="0.1.1" />
19+
</ItemGroup>
20+
21+
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
22+
<PackageReference Include="FluentValidation" Version="11.11.0" />
23+
<PackageReference Include="MediatR" Version="12.4.1" />
24+
<PackageReference Include="SOFTURE.Results" Version="0.1.1" />
25+
</ItemGroup>
26+
27+
<PropertyGroup>
28+
<AssemblyName>SOFTURE.Common.CQRS</AssemblyName>
29+
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
30+
<ImplicitUsings>enable</ImplicitUsings>
31+
<LangVersion>latest</LangVersion>
32+
<Title>$(AssemblyName)</Title>
33+
<Authors>SOFTURE</Authors>
34+
<Copyright>Copyright (c) 2024 $(Authors)</Copyright>
35+
<Description>SOFTURE - CQRS</Description>
36+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
37+
<IncludeSymbols>true</IncludeSymbols>
38+
<PackageId>$(AssemblyName)</PackageId>
39+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
40+
<PackageProjectUrl>https://github.com/SOFTURE/API</PackageProjectUrl>
41+
<PackageReadmeFile>README.md</PackageReadmeFile>
42+
<PackageReleaseNotes>See $(PackageProjectUrl)/blob/master/CHANGELOG.md for release notes.</PackageReleaseNotes>
43+
<PackageTags>SOFTURE</PackageTags>
44+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
45+
<RepositoryType>Git</RepositoryType>
46+
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl>
47+
</PropertyGroup>
48+
49+
<ItemGroup>
50+
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
51+
<None Include="..\..\LICENSE" Pack="true" PackagePath="\"/>
52+
</ItemGroup>
53+
54+
</Project>

0 commit comments

Comments
 (0)