1
0
mirror of synced 2024-11-14 22:33:50 +00:00
Tapeti/Tapeti.Flow/Default/FlowStarter.cs

94 lines
3.8 KiB
C#
Raw Permalink Normal View History

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Tapeti.Config;
namespace Tapeti.Flow.Default
{
/// <inheritdoc />
/// <summary>
/// Default implementation for IFlowStarter.
/// </summary>
internal class FlowStarter : IFlowStarter
{
private readonly ITapetiConfig config;
/// <summary>
/// </summary>
2019-08-15 10:04:03 +00:00
public FlowStarter(ITapetiConfig config)
{
this.config = config;
}
/// <inheritdoc />
2019-08-15 10:04:03 +00:00
public async Task Start<TController>(Expression<Func<TController, Func<IYieldPoint>>> methodSelector) where TController : class
{
2019-08-15 10:04:03 +00:00
await CallControllerMethod<TController>(GetExpressionMethod(methodSelector), value => Task.FromResult((IYieldPoint)value), new object[] { });
}
/// <inheritdoc />
2019-08-15 10:04:03 +00:00
public async Task Start<TController>(Expression<Func<TController, Func<Task<IYieldPoint>>>> methodSelector) where TController : class
{
2019-08-15 10:04:03 +00:00
await CallControllerMethod<TController>(GetExpressionMethod(methodSelector), value => (Task<IYieldPoint>)value, new object[] {});
}
/// <inheritdoc />
2019-08-15 10:04:03 +00:00
public async Task Start<TController, TParameter>(Expression<Func<TController, Func<TParameter, IYieldPoint>>> methodSelector, TParameter parameter) where TController : class
{
2019-08-15 10:04:03 +00:00
await CallControllerMethod<TController>(GetExpressionMethod(methodSelector), value => Task.FromResult((IYieldPoint)value), new object[] {parameter});
}
/// <inheritdoc />
2019-08-15 10:04:03 +00:00
public async Task Start<TController, TParameter>(Expression<Func<TController, Func<TParameter, Task<IYieldPoint>>>> methodSelector, TParameter parameter) where TController : class
{
2019-08-15 10:04:03 +00:00
await CallControllerMethod<TController>(GetExpressionMethod(methodSelector), value => (Task<IYieldPoint>)value, new object[] {parameter});
}
2019-08-15 10:04:03 +00:00
private async Task CallControllerMethod<TController>(MethodInfo method, Func<object, Task<IYieldPoint>> getYieldPointResult, object[] parameters) where TController : class
{
var controller = config.DependencyResolver.Resolve<TController>();
var yieldPoint = await getYieldPointResult(method.Invoke(controller, parameters));
2019-08-15 10:04:03 +00:00
var context = new FlowHandlerContext
{
Config = config,
2019-08-15 10:04:03 +00:00
Controller = controller,
Method = method
};
var flowHandler = config.DependencyResolver.Resolve<IFlowHandler>();
2019-08-15 10:04:03 +00:00
await flowHandler.Execute(context, yieldPoint);
}
private static MethodInfo GetExpressionMethod<TController, TResult>(Expression<Func<TController, Func<TResult>>> methodSelector)
{
var callExpression = (methodSelector.Body as UnaryExpression)?.Operand as MethodCallExpression;
var targetMethodExpression = callExpression?.Object as ConstantExpression;
var method = targetMethodExpression?.Value as MethodInfo;
if (method == null)
throw new ArgumentException("Unable to determine the starting method", nameof(methodSelector));
return method;
}
2019-08-15 10:04:03 +00:00
private static MethodInfo GetExpressionMethod<TController, TResult, TParameter>(Expression<Func<TController, Func<TParameter, TResult>>> methodSelector)
{
var callExpression = (methodSelector.Body as UnaryExpression)?.Operand as MethodCallExpression;
var targetMethodExpression = callExpression?.Object as ConstantExpression;
var method = targetMethodExpression?.Value as MethodInfo;
if (method == null)
throw new ArgumentException("Unable to determine the starting method", nameof(methodSelector));
return method;
}
}
}