Skip to content

Commit

Permalink
More cleanup for mediator packages
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesRandall committed Jan 11, 2020
1 parent bd46a7c commit 9921519
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 39 deletions.
12 changes: 6 additions & 6 deletions Samples/Scratch/SwaggerBuildOut/CustomResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ namespace SwaggerBuildOut
{
public class CustomResponseHandler : IHttpResponseHandler
{
public Task<IActionResult> CreateResponse<TCommand>(TCommand command, Exception ex) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand>(TCommand command, Exception ex)
{
return null;
}

public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex) where TCommand : ICommand
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex)
{
throw new NotImplementedException();
}

public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result)
{
if (result == null)
{
Expand All @@ -34,17 +34,17 @@ public Task<IActionResult> CreateResponse<TCommand>(TCommand command)
return null;
}

public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand
public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult)
{
return null;
}

public Task<IActionResult> CreateResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand>(TCommand command, ValidationResult validationResult)
{
return null;
}

public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, ValidationResult<TResult> validationResult) where TCommand : ICommand<TResult>
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, ValidationResult<TResult> validationResult)
{
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions Scratch/FmAspNetCore/StringContentResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace FmAspNetCore
{
public class StringContentResponseHandler : IHttpResponseHandler
{
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex) where TCommand : ICommand
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex)
{
throw new NotImplementedException();
}

public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result)
{
string stringResult = result.ToString();
IActionResult contentResult = new ContentResult()
Expand All @@ -31,7 +31,7 @@ public Task<IActionResult> CreateResponse<TCommand>(TCommand command)
throw new NotImplementedException();
}

public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand
public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AzureFromTheTrenches.Commanding.Abstractions" Version="9.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IHttpResponseHandler
/// <param name="command">The command</param>
/// <param name="ex">The exception that was thrown</param>
/// <returns>An action result or null for the default Function Monkey behaviour</returns>
Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex) where TCommand : ICommand;
Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex);

/// <summary>
/// Invoked when a command with an associated result is successfully invoked.
Expand All @@ -27,7 +27,7 @@ public interface IHttpResponseHandler
/// <param name="command">The command</param>
/// <param name="result">The result</param>
/// <returns>An action result or null for the default Function Monkey behaviour</returns>
Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result) where TCommand : ICommand;
Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result);

/// <summary>
/// Invoked when a command with no associated result is successfully invoked.
Expand All @@ -46,6 +46,6 @@ public interface IHttpResponseHandler
/// <param name="command">The command</param>
/// <param name="validationResult">The validation result</param>
/// <returns>An action result or null for the default Function Monkey behaviour</returns>
Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand;
Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult);
}
}
2 changes: 1 addition & 1 deletion Source/FunctionMonkey.Abstractions/ISerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ISerializer
/// <param name="value">The serialized representation</param>
/// <param name="enforceSecurityProperties">True if SecurityPropertyAttribute behaviour should be applied, defaults to true</param>
/// <returns>A deserialized command</returns>
TCommand Deserialize<TCommand>(string value, bool enforceSecurityProperties=true) where TCommand : ICommand;
TCommand Deserialize<TCommand>(string value, bool enforceSecurityProperties=true);

/// <summary>
/// Deserializes a command from the provided string
Expand Down
13 changes: 10 additions & 3 deletions Source/FunctionMonkey.Compiler.Core/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using FunctionMonkey.Compiler.Core.Implementation.AspNetCore;
using FunctionMonkey.Compiler.Core.Implementation.AzureFunctions;
using FunctionMonkey.Infrastructure;
using FunctionMonkey.Model;
using FunctionMonkey.Model.OutputBindings;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -203,10 +204,16 @@ private bool ValidateCommandTypes(FunctionHostBuilder builder)
bool errorFound = false;
foreach (AbstractFunctionDefinition functionDefinition in builder.FunctionDefinitions)
{
if (!typeSafetyEnforcer.IsValidType(functionDefinition.CommandType))
// SignalRBindingExpressionNegotiateCommand is a type used only to make a non dispatching HTTP function
// definition work, it doesn't get used with any mediator and is defined within Function Monkey. It is
// exempt from mediator type checking
if (functionDefinition.CommandType != typeof(SignalRBindingExpressionNegotiateCommand))
{
errorFound = true;
_compilerLog.Error($"Command type {functionDefinition.CommandType.Name} does not conform to the requirements of the mediator. {typeSafetyEnforcer.Requirements}");
if (!typeSafetyEnforcer.IsValidType(functionDefinition.CommandType))
{
errorFound = true;
_compilerLog.Error($"Command type {functionDefinition.CommandType.Name} does not conform to the requirements of the mediator. {typeSafetyEnforcer.Requirements}");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace FunctionMonkey.Testing.Implementation
{
internal class DefaultHttpResponseHandler : IHttpResponseHandler
{
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex) where TCommand : ICommand
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex)
{
return null;
}

// We can't use where TCommand : ICommand<TResult> due to the function injection - we can't bind the generic return type
// (will revisit and check)

public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result)
{
return null;
}
Expand All @@ -27,7 +27,7 @@ public Task<IActionResult> CreateResponse<TCommand>(TCommand command)
return null;
}

public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand
public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace FunctionMonkey.Model
{
public class SignalRBindingExpressionNegotiateCommand : ICommand { }
public class SignalRBindingExpressionNegotiateCommand { }

public class SignalRBindingExpressionNegotiateFunctionDefinition : HttpFunctionDefinition
{
Expand Down
2 changes: 1 addition & 1 deletion Source/FunctionMonkey/PluginFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace FunctionMonkey
{
public class PluginFunctions : AbstractPluginFunctions // where TCommand : ICommand
public class PluginFunctions : AbstractPluginFunctions
{
public Func<string, Task<ClaimsPrincipal>> ValidateToken { get; set; }

Expand Down
5 changes: 0 additions & 5 deletions Source/FunctionMonkey/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public static void InitializeFromStartup(IServiceCollection serviceCollection)
var _ = RuntimeInstance.Value;
}

/// <summary>
/// Retrieves the command dispatcher from the dependency resolver
/// </summary>
public static ICommandDispatcher CommandDispatcher => ServiceProvider.GetService<ICommandDispatcher>();

public static IMediatorDecorator Mediator => ServiceProvider.GetService<IMediatorDecorator>();
}
}
14 changes: 9 additions & 5 deletions Source/FunctionMonkey/RuntimeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private void CreatePluginFunctions(
var validator = (FunctionMonkey.Abstractions.Validation.IValidator)
ServiceProvider.GetService(
typeof(FunctionMonkey.Abstractions.Validation.IValidator));
var validationResult = validator.Validate((ICommand) command);
var validationResult = validator.Validate(command);
return validationResult;
};
}
Expand Down Expand Up @@ -357,29 +357,29 @@ private void CreatePluginFunctions(
var responseHandler =
(IHttpResponseHandler) ServiceProvider.GetService(
httpFunctionDefinition.HttpResponseHandlerType);
return responseHandler.CreateValidationFailureResponse((ICommand) command, (ValidationResult)validationResult);
return responseHandler.CreateValidationFailureResponse(command, (ValidationResult)validationResult);
};

pluginFunctions.CreateResponseForResult = (command, result) =>
{
var responseHandler =
(IHttpResponseHandler) ServiceProvider.GetService(
httpFunctionDefinition.HttpResponseHandlerType);
return responseHandler.CreateResponse((ICommand) command, result);
return responseHandler.CreateResponse(command, result);
};
pluginFunctions.CreateResponse = command =>
{
var responseHandler =
(IHttpResponseHandler) ServiceProvider.GetService(
httpFunctionDefinition.HttpResponseHandlerType);
return responseHandler.CreateResponse((ICommand) command);
return responseHandler.CreateResponse(command);
};
pluginFunctions.CreateResponseFromException = (command, exception) =>
{
var responseHandler =
(IHttpResponseHandler) ServiceProvider.GetService(
httpFunctionDefinition.HttpResponseHandlerType);
return responseHandler.CreateResponseFromException((ICommand) command, exception);
return responseHandler.CreateResponseFromException(command, exception);
};
}
else
Expand Down Expand Up @@ -546,6 +546,10 @@ private FunctionHostBuilder CreateBuilderFromConfiguration(

private void RegisterCommandHandlersForCommandsWithNoAssociatedHandler(FunctionHostBuilder builder, ICommandRegistry commandRegistry)
{
// TODO: We can improve this so that auto-registration is decoupled and can be provided by a mediator package
if (builder.MediatorType != typeof(DefaultMediatorDecorator))
return;

// IN PROGRESS: This looks from the loaded set of assemblies and looks for a command handler for each command associated with a function.
// If the handler is not already registered in the command registry then this registers it.
IRegistrationCatalogue registrationCatalogue = (IRegistrationCatalogue) commandRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public NamingStrategyJsonSerializer(NamingStrategy deserializerNamingStrategy, N
/// <param name="enforceSecurityProperties">True if [SecurityProperty] should be honoured (not used in deserializtion)</param>
/// <typeparam name="TCommand">The type</typeparam>
/// <returns>The deserialized object</returns>
public TCommand Deserialize<TCommand>(string json, bool enforceSecurityProperties) where TCommand : ICommand
public TCommand Deserialize<TCommand>(string json, bool enforceSecurityProperties)
{
JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace FunctionMonkey.Tests.Integration.AspNetCore
{
public class CustomResponseHandler : IHttpResponseHandler
{
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex) where TCommand : ICommand
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex)
{
return Task.FromResult((IActionResult)new OkObjectResult("CreateResponseFromException<TCommand>"));
}

// We can't use where TCommand : ICommand<TResult> due to the function injection - we can't bind the generic return type
// (will revisit and check)
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result)
{
return Task.FromResult((IActionResult)new OkObjectResult("CreateResponse<TCommand,TResult>"));
}
Expand All @@ -26,7 +26,7 @@ public Task<IActionResult> CreateResponse<TCommand>(TCommand command)
return Task.FromResult((IActionResult)new OkObjectResult("CreateResponse<TCommand>"));
}

public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand
public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult)
{
return Task.FromResult((IActionResult)new OkObjectResult("CreateValidationFailureResponse<TCommand>"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace FunctionMonkey.Tests.Integration.Functions
{
public class CustomResponseHandler : IHttpResponseHandler
{
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex) where TCommand : ICommand
public Task<IActionResult> CreateResponseFromException<TCommand>(TCommand command, Exception ex)
{
return Task.FromResult((IActionResult)new OkObjectResult("CreateResponseFromException<TCommand>"));
}

// We can't use where TCommand : ICommand<TResult> due to the function injection - we can't bind the generic return type
// (will revisit and check)
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result) where TCommand : ICommand
public Task<IActionResult> CreateResponse<TCommand, TResult>(TCommand command, TResult result)
{
return Task.FromResult((IActionResult)new OkObjectResult("CreateResponse<TCommand,TResult>"));
}
Expand All @@ -26,7 +26,7 @@ public Task<IActionResult> CreateResponse<TCommand>(TCommand command)
return Task.FromResult((IActionResult)new OkObjectResult("CreateResponse<TCommand>"));
}

public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult) where TCommand : ICommand
public Task<IActionResult> CreateValidationFailureResponse<TCommand>(TCommand command, ValidationResult validationResult)
{
return Task.FromResult((IActionResult)new OkObjectResult("CreateValidationFailureResponse<TCommand>"));
}
Expand Down

0 comments on commit 9921519

Please sign in to comment.