-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test framework now includes validators
- Loading branch information
1 parent
b8eaa7f
commit ca15475
Showing
6 changed files
with
98 additions
and
13 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
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,60 @@ | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AzureFromTheTrenches.Commanding.Abstractions; | ||
using AzureFromTheTrenches.Commanding.Abstractions.Model; | ||
using FunctionMonkey.Abstractions.Validation; | ||
using FunctionMonkey.Commanding.Abstractions.Validation; | ||
|
||
namespace FunctionMonkey.Testing | ||
{ | ||
public class ValidatingDispatcher : ICommandDispatcher | ||
{ | ||
private readonly ICommandDispatcher _underlyingDispatcher; | ||
private readonly IValidator _validator; | ||
|
||
public ValidatingDispatcher(ICommandDispatcher underlyingDispatcher, IValidator validator) | ||
{ | ||
_underlyingDispatcher = underlyingDispatcher; | ||
_validator = validator; | ||
} | ||
|
||
public async Task<CommandResult<TResult>> DispatchAsync<TResult>(ICommand<TResult> command, CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
Validate(command); | ||
|
||
return await _underlyingDispatcher.DispatchAsync(command, cancellationToken); | ||
} | ||
|
||
public async Task<CommandResult> DispatchAsync(ICommand command, CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
Validate(command); | ||
return await _underlyingDispatcher.DispatchAsync(command, cancellationToken); | ||
} | ||
|
||
public ICommandExecuter AssociatedExecuter { get; } | ||
|
||
private void Validate(ICommand command) | ||
{ | ||
// The .Validate method uses its generic parameter to determine the type of the | ||
// command as it uses generics with the IServiceCollecton, this has the unfortunate | ||
// side-effect of meaning you can't ask it to validate a none-concretely typed ICommand | ||
// This isn't an issue in Function Monkey itself as we have the specific command type | ||
// but does cause a problem here as if we call .Validate(command) command will be resolved | ||
// as being of type ICommand and so no validator will be found. | ||
// | ||
// To work round this we construct a method call using the concrete type of the command. | ||
// In test code this isn't performance sensitive so we don't bother compiling an expression | ||
// for it. | ||
MethodInfo methodInfo = _validator.GetType().GetMethod("Validate"); | ||
MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(command.GetType()); | ||
ValidationResult validationResult = (ValidationResult)genericMethodInfo.Invoke(_validator, new [] {command}); | ||
|
||
//ValidationResult validationResult = _validator.Validate(command); | ||
if (!validationResult.IsValid) | ||
{ | ||
throw new ValidationException(validationResult); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
using System; | ||
using FunctionMonkey.Commanding.Abstractions.Validation; | ||
|
||
namespace FunctionMonkey.Testing | ||
{ | ||
public class ValidationException : Exception | ||
{ | ||
public ValidationException(ValidationResult validationResult) | ||
{ | ||
ValidationResult = validationResult; | ||
} | ||
|
||
public ValidationResult ValidationResult { get; } | ||
} | ||
} |