forked from OData/RESTier
-
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.
Clean some code and add IOperationProcessor
- Loading branch information
1 parent
78717f7
commit 4e9d643
Showing
19 changed files
with
274 additions
and
252 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
106 changes: 106 additions & 0 deletions
106
src/Microsoft.Restier.Core/Conventions/ConventionBasedOperationProcessor.cs
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,106 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Restier.Core.Operation; | ||
|
||
namespace Microsoft.Restier.Core.Conventions | ||
{ | ||
/// <summary> | ||
/// A convention-based change set item filter. | ||
/// </summary> | ||
internal class ConventionBasedOperationProcessor : IOperationProcessor | ||
{ | ||
private Type targetType; | ||
|
||
private ConventionBasedOperationProcessor(Type targetType) | ||
{ | ||
Ensure.NotNull(targetType, "targetType"); | ||
this.targetType = targetType; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public static void ApplyTo( | ||
IServiceCollection services, | ||
Type targetType) | ||
{ | ||
Ensure.NotNull(services, "services"); | ||
Ensure.NotNull(targetType, "targetType"); | ||
services.AddService<IOperationProcessor>( | ||
(sp, next) => new ConventionBasedOperationProcessor(targetType)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Task OnExecutingOperationAsync( | ||
OperationContext context, | ||
CancellationToken cancellationToken) | ||
{ | ||
return this.InvokeProcessorMethodAsync( | ||
context, ConventionBasedChangeSetConstants.FilterMethodNamePreFilterSuffix); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Task OnExecutedOperationAsync( | ||
OperationContext context, | ||
CancellationToken cancellationToken) | ||
{ | ||
return this.InvokeProcessorMethodAsync( | ||
context, ConventionBasedChangeSetConstants.FilterMethodNamePostFilterSuffix); | ||
} | ||
|
||
private static bool ParametersMatch(ParameterInfo[] methodParameters, object[] parameters) | ||
{ | ||
return methodParameters.Length == parameters.Length | ||
&& !methodParameters.Where((mp, i) => !mp.ParameterType.IsInstanceOfType(parameters[i])).Any(); | ||
} | ||
|
||
private Task InvokeProcessorMethodAsync( | ||
OperationContext context, | ||
string methodNameSuffix) | ||
{ | ||
string methodName = ConventionBasedChangeSetConstants.FilterMethodActionInvocationExecute + | ||
methodNameSuffix + context.OperationName; | ||
object[] parameters = null; | ||
if (context.ParametersValue != null) | ||
{ | ||
context.ParametersValue.ToArray(); | ||
} | ||
|
||
MethodInfo method = this.targetType.GetQualifiedMethod(methodName); | ||
|
||
if (method != null && | ||
(method.ReturnType == typeof(void) || | ||
typeof(Task).IsAssignableFrom(method.ReturnType))) | ||
{ | ||
object target = null; | ||
if (!method.IsStatic) | ||
{ | ||
target = context.GetApiService<ApiBase>(); | ||
if (target == null || | ||
!this.targetType.IsInstanceOfType(target)) | ||
{ | ||
return Task.WhenAll(); | ||
} | ||
} | ||
|
||
ParameterInfo[] methodParameters = method.GetParameters(); | ||
if (ParametersMatch(methodParameters, parameters)) | ||
{ | ||
object result = method.Invoke(target, parameters); | ||
Task resultTask = result as Task; | ||
if (resultTask != null) | ||
{ | ||
return resultTask; | ||
} | ||
} | ||
} | ||
|
||
return Task.WhenAll(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.