-
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.
Added IRequestFilter with Behaviour and IResultFilter with Behaviour (#4
) * Added IRequestFilter with Behaviour to filter request of query or command. * Added IResultFilter with Behaviour to filter result of query or command.
- Loading branch information
1 parent
acc9fac
commit ba6ee27
Showing
16 changed files
with
199 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Cross.CQRS.Behaviors; | ||
|
||
internal sealed class RequestFilterBehavior<TRequest, TResult> : IPipelineBehavior<TRequest, TResult> | ||
where TRequest : IRequest<TResult> | ||
{ | ||
private readonly IEnumerable<IRequestFilter<TRequest>> _filters; | ||
|
||
public RequestFilterBehavior(IEnumerable<IRequestFilter<TRequest>> filters) | ||
{ | ||
_filters = filters; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<TResult> Handle(TRequest request, RequestHandlerDelegate<TResult> next, CancellationToken cancellationToken) | ||
{ | ||
var filters = _filters.ToArray(); | ||
if (filters.Length <= 0) | ||
{ | ||
return await next(); | ||
} | ||
|
||
var result = request; | ||
foreach (var filter in filters) | ||
{ | ||
result = await filter.ApplyFilterAsync(result, cancellationToken); | ||
} | ||
|
||
return await 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,32 @@ | ||
namespace Cross.CQRS.Behaviors; | ||
|
||
internal sealed class ResultFilterBehavior<TRequest, TResult> : IPipelineBehavior<TRequest, TResult> | ||
where TRequest : IRequest<TResult> | ||
{ | ||
private readonly IEnumerable<IResultFilter<TRequest, TResult>> _filters; | ||
|
||
public ResultFilterBehavior(IEnumerable<IResultFilter<TRequest, TResult>> filters) | ||
{ | ||
_filters = filters; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<TResult> Handle(TRequest request, RequestHandlerDelegate<TResult> next, CancellationToken cancellationToken) | ||
{ | ||
var filters = _filters.ToArray(); | ||
if (filters.Length <= 0) | ||
{ | ||
return await next(); | ||
} | ||
|
||
var responce = await next(); | ||
|
||
var result = responce; | ||
foreach (var filter in filters) | ||
{ | ||
result = await filter.ApplyFilterAsync(result, cancellationToken); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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,26 @@ | ||
namespace Cross.CQRS.Filters; | ||
|
||
/// <summary> | ||
/// Defines a filter for a particular type. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use it if you need to perform filter operation on the <see cref="TRequest"/>, | ||
/// after the query <see cref="IQuery{TResponse}"/> or command <see cref="ICommand"/>. | ||
/// </remarks> | ||
public interface IRequestFilter<TRequest> | ||
{ | ||
/// <summary> | ||
/// Filter the specified instance. | ||
/// </summary> | ||
/// <param name="result">The instance to filter</param> | ||
/// <returns>A ValidationResult object containing any validation failures.</returns> | ||
TRequest ApplyFilter(TRequest result); | ||
|
||
/// <summary> | ||
/// Filter the specified instance asynchronously. | ||
/// </summary> | ||
/// <param name="result">The instance to filter</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>A ValidationResult object containing any validation failures.</returns> | ||
Task<TRequest> ApplyFilterAsync(TRequest result, CancellationToken cancellationToken); | ||
} |
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,26 @@ | ||
namespace Cross.CQRS.Filters; | ||
|
||
/// <summary> | ||
/// Defines a filter for a particular type. | ||
/// </summary> | ||
/// <remarks> | ||
/// Use it if you need to perform filter operation on the <see cref="TResult"/>, | ||
/// after the query <see cref="IQuery{TResponse}"/> or command <see cref="ICommand"/>. | ||
/// </remarks> | ||
public interface IResultFilter<TRequest, TResult> | ||
{ | ||
/// <summary> | ||
/// Filter the specified instance. | ||
/// </summary> | ||
/// <param name="result">The instance to filter</param> | ||
/// <returns>A ValidationResult object containing any validation failures.</returns> | ||
TResult ApplyFilter(TResult result); | ||
|
||
/// <summary> | ||
/// Filter the specified instance asynchronously. | ||
/// </summary> | ||
/// <param name="result">The instance to filter</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>A ValidationResult object containing any validation failures.</returns> | ||
Task<TResult> ApplyFilterAsync(TResult result, CancellationToken cancellationToken); | ||
} |
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,15 @@ | ||
namespace Cross.CQRS.Filters; | ||
|
||
/// <summary> | ||
/// Base query/command filter. | ||
/// </summary> | ||
public abstract class RequestFilter<TRequest, TResult> : IRequestFilter<TRequest> | ||
where TRequest : IRequest<TResult> | ||
{ | ||
/// <inheritdoc /> | ||
public TRequest ApplyFilter(TRequest request) | ||
=> ApplyFilterAsync(request, CancellationToken.None).GetAwaiter().GetResult(); | ||
|
||
/// <inheritdoc /> | ||
public abstract Task<TRequest> ApplyFilterAsync(TRequest request, CancellationToken cancellationToken); | ||
} |
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,15 @@ | ||
namespace Cross.CQRS.Filters; | ||
|
||
/// <summary> | ||
/// Base query/command filter. | ||
/// </summary> | ||
public abstract class ResultFilter<TRequest, TResult> : IResultFilter<TRequest, TResult> | ||
where TRequest : IRequest<TResult> | ||
{ | ||
/// <inheritdoc /> | ||
public TResult ApplyFilter(TResult result) | ||
=> ApplyFilterAsync(result, CancellationToken.None).GetAwaiter().GetResult(); | ||
|
||
/// <inheritdoc /> | ||
public abstract Task<TResult> ApplyFilterAsync(TResult result, CancellationToken cancellationToken); | ||
} |
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
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,11 @@ | ||
namespace SampleWebApp.Modules.Some.Handlers; | ||
|
||
public class SomeResultFilter : ResultFilter<SomeQuery, IEnumerable<string>> | ||
{ | ||
public override Task<IEnumerable<string>> ApplyFilterAsync(IEnumerable<string> response, CancellationToken cancellationToken) | ||
{ | ||
// do some filter actions | ||
|
||
return Task.FromResult(response); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dotnet build --configuration release ..\Cross.CQRS.sln | ||
nuget.exe pack config.nuspec -Symbols -SymbolPackageFormat snupkg | ||
REM nuget.exe pack config.nuspec -Symbols -SymbolPackageFormat snupkg | ||
nuget.exe pack config.nuspec -Symbols |