-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comments + middleware feature + CqrsVibe.AspNetCore package
- Loading branch information
Showing
49 changed files
with
1,080 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using CqrsVibe.MicrosoftDependencyInjection; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace CqrsVibe.AspNetCore | ||
{ | ||
public static class AppBuilderExtensions | ||
{ | ||
public static IApplicationBuilder UseCqrsVibe(this IApplicationBuilder app) | ||
{ | ||
return app.Use((context, next) => | ||
{ | ||
context.RequestServices.SetToHandlerResolverAccessor(); | ||
return next(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Authors>Vsevolod Elunin</Authors> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/velunin/CqrsVibe</RepositoryUrl> | ||
<PackageTags>CQRS;Mediator;Commands;Queries</PackageTags> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\CqrsVibe.MicrosoftDependencyInjection\CqrsVibe.MicrosoftDependencyInjection.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using CqrsVibe.Commands; | ||
using CqrsVibe.Commands.Pipeline; | ||
using CqrsVibe.ContextAbstractions; | ||
using CqrsVibe.FluentValidation.Extensions; | ||
using FluentValidation; | ||
using GreenPipes; | ||
|
||
namespace CqrsVibe.FluentValidation | ||
{ | ||
/// <summary> | ||
/// Command validation filter | ||
/// </summary> | ||
internal class CommandValidationFilter : IFilter<ICommandHandlingContext> | ||
{ | ||
private readonly OnFailureBehavior _behavior; | ||
|
||
public CommandValidationFilter(OnFailureBehavior behavior) | ||
{ | ||
_behavior = behavior; | ||
} | ||
|
||
public async Task Send(ICommandHandlingContext context, IPipe<ICommandHandlingContext> next) | ||
{ | ||
var commandValidatorType = typeof(IValidator<>).MakeGenericType(context.Command.GetType()); | ||
|
||
if(!context.ContextServices.TryResolveValidator(commandValidatorType, out var validator)) | ||
{ | ||
await next.Send(context); | ||
return; | ||
} | ||
|
||
ValidateBehavior(context); | ||
|
||
var validationContext = new ValidationContext<ICommand>(context.Command); | ||
var validationResult = await validator.ValidateAsync( | ||
validationContext, | ||
context.CancellationToken); | ||
|
||
switch (_behavior) | ||
{ | ||
case OnFailureBehavior.ThrowException: | ||
if (!validationResult.IsValid) | ||
{ | ||
throw new ValidationException(validationResult.Errors); | ||
} | ||
break; | ||
case OnFailureBehavior.ReturnEither: | ||
if (!validationResult.IsValid) | ||
{ | ||
var validationState = ValidationState.ErrorState(validationResult); | ||
|
||
context.Command.TryGetResultType(out var resultType); | ||
|
||
((IResultingHandlingContext)context).SetResult(Activator.CreateInstance(resultType, validationState)); | ||
|
||
return; | ||
} | ||
break; | ||
default: | ||
throw new InvalidOperationException("Undefined behavior"); | ||
} | ||
|
||
await next.Send(context); | ||
} | ||
|
||
public void Probe(ProbeContext context) | ||
{ | ||
var scope = context.CreateFilterScope("commandValidation"); | ||
scope.Add("onFailureBehavior", _behavior.ToString("G")); | ||
} | ||
|
||
private void ValidateBehavior(ICommandHandlingContext context) | ||
{ | ||
if (_behavior == OnFailureBehavior.ReturnEither) | ||
{ | ||
if (!context.Command.TryGetResultType(out var commandResultType) || | ||
!commandResultType.IsEitherWithValidationResult()) | ||
{ | ||
throw new InvalidOperationException( | ||
$"For mode with '{OnFailureBehavior.ReturnEither:G}' behavior " + | ||
"command result type must be Either<TResult,ValidationState>"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using CqrsVibe.FluentValidation.Extensions; | ||
using CqrsVibe.Queries; | ||
using CqrsVibe.Queries.Pipeline; | ||
using FluentValidation; | ||
using GreenPipes; | ||
|
||
namespace CqrsVibe.FluentValidation | ||
{ | ||
/// <summary> | ||
/// Query validation filter | ||
/// </summary> | ||
internal class QueryValidationFilter : IFilter<IQueryHandlingContext> | ||
{ | ||
private readonly OnFailureBehavior _behavior; | ||
|
||
public QueryValidationFilter(OnFailureBehavior behavior) | ||
{ | ||
_behavior = behavior; | ||
} | ||
|
||
public async Task Send(IQueryHandlingContext context, IPipe<IQueryHandlingContext> next) | ||
{ | ||
var commandValidatorType = typeof(IValidator<>).MakeGenericType(context.Query.GetType()); | ||
|
||
if(!context.ContextServices.TryResolveValidator(commandValidatorType, out var validator)) | ||
{ | ||
await next.Send(context); | ||
return; | ||
} | ||
|
||
ValidateBehavior(context); | ||
|
||
var validationContext = new ValidationContext<IQuery>(context.Query); | ||
var validationResult = await validator.ValidateAsync( | ||
validationContext, | ||
context.CancellationToken); | ||
|
||
switch (_behavior) | ||
{ | ||
case OnFailureBehavior.ThrowException: | ||
if (!validationResult.IsValid) | ||
{ | ||
throw new ValidationException(validationResult.Errors); | ||
} | ||
break; | ||
case OnFailureBehavior.ReturnEither: | ||
if (!validationResult.IsValid) | ||
{ | ||
var validationState = ValidationState.ErrorState(validationResult); | ||
|
||
context.Query.TryGetResultType(out var resultType); | ||
|
||
context.SetResult(Activator.CreateInstance(resultType, validationState)); | ||
return; | ||
} | ||
break; | ||
default: | ||
throw new InvalidOperationException("Undefined behavior"); | ||
} | ||
|
||
await next.Send(context); | ||
} | ||
|
||
public void Probe(ProbeContext context) | ||
{ | ||
var scope = context.CreateFilterScope("queryValidation"); | ||
scope.Add("onFailureBehavior", _behavior.ToString("G")); | ||
} | ||
|
||
private void ValidateBehavior(IQueryHandlingContext context) | ||
{ | ||
if (_behavior == OnFailureBehavior.ReturnEither) | ||
{ | ||
if (!context.Query.TryGetResultType(out var commandResultType) || | ||
!commandResultType.IsEitherWithValidationResult()) | ||
{ | ||
throw new InvalidOperationException( | ||
$"For mode with '{OnFailureBehavior.ReturnEither:G}' behavior " + | ||
"query result type must be Either<TResult,ValidationState>"); | ||
} | ||
} | ||
} | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
src/CqrsVibe.FluentValidation/ValidationResultTaskFactory.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.