Skip to content

Commit

Permalink
fix: abstracted some props to common baseclass
Browse files Browse the repository at this point in the history
  • Loading branch information
pksorensen committed Jun 25, 2024
1 parent 7c7012d commit f226b11
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
37 changes: 31 additions & 6 deletions src/WorkflowEngine.Core/ActionImplementationMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,34 @@

namespace WorkflowEngine.Core
{
public class ActionImplementationMetadata<T> : IActionImplementationMetadata
where T: IActionImplementation
public class ActionImplementationMetadata
{
public string Type { get; set; }
public Type Implementation => typeof(T);
public Type Implementation { get; protected set; }

public static IActionImplementationMetadata FromType(Type type, string name)
{
var metadata = Activator.CreateInstance(typeof(ActionImplementationMetadata<>).MakeGenericType(type)) as ActionImplementationMetadata;
metadata.Type = name ?? type.Name;

return metadata as IActionImplementationMetadata;
}
public static IActionImplementationMetadata FromType(Type type,Type inputType, string name)
{
var metadata = Activator.CreateInstance(typeof(ActionImplementationMetadata<,>).MakeGenericType(type,inputType)) as ActionImplementationMetadata;
metadata.Type = name ?? type.Name;

return metadata as IActionImplementationMetadata;
}
}
public class ActionImplementationMetadata<T> : ActionImplementationMetadata,IActionImplementationMetadata
where T: IActionImplementation
{
public ActionImplementationMetadata()
{
Implementation = typeof(T);
}

public async ValueTask<ActionResult> ExecuteAsync(IServiceProvider services, IRunContext context, IWorkflow workflow, IAction action)
{
var implementation = services.GetRequiredService(Implementation) as IActionImplementation;
Expand All @@ -28,11 +50,14 @@ public async ValueTask<ActionResult> ExecuteAsync(IServiceProvider services, IRu
}
}

public class ActionImplementationMetadata<T,TInput> : IActionImplementationMetadata
public class ActionImplementationMetadata<T,TInput> : ActionImplementationMetadata, IActionImplementationMetadata
where T : IActionImplementation<TInput>
{
public string Type { get; set; }
public Type Implementation => typeof(T);
public ActionImplementationMetadata()
{
Implementation = typeof(T);
}


public async ValueTask<ActionResult> ExecuteAsync(IServiceProvider services, IRunContext context,IWorkflow workflow,IAction action )
{
Expand Down
8 changes: 6 additions & 2 deletions src/WorkflowEngine.Core/IActionImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace WorkflowEngine.Core
{
public interface IActionImplementation
public interface IActionForRegistration
{

}
public interface IActionImplementation : IActionForRegistration
{


ValueTask<object> ExecuteAsync(IRunContext context, IWorkflow workflow, IAction action);


}
public interface IActionImplementation<TInput>
public interface IActionImplementation<TInput> : IActionForRegistration
{


Expand Down
29 changes: 26 additions & 3 deletions src/WorkflowEngine.Core/IActionImplementationExtenssions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System;

namespace WorkflowEngine.Core
{
public static class IActionImplementationExtenssions
{
public static IServiceCollection AddAction<T>(this IServiceCollection services, string type = null)
where T: class, IActionImplementation
where T: class, IActionForRegistration
{

// Check if T implements IActionImplementation<TInput>
var interfaceType = typeof(T).GetInterfaces().FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IActionImplementation<>));
if (interfaceType != null)
{
// Extract the TInput type argument
var inputType = interfaceType.GetGenericArguments()[0];

// Get the method definition for AddAction<T, TInput>
var method = typeof(IActionImplementationExtenssions).GetMethod(nameof(AddAction), 2,
new Type[] { typeof(IServiceCollection), typeof(string) }).MakeGenericMethod(new Type[] { typeof(T), inputType });

// Invoke AddAction<T, TInput> using reflection
return (IServiceCollection) method.Invoke(null, new object[] { services, type });
}


return services.AddTransient<T>()
.AddSingleton< IActionImplementationMetadata>(new ActionImplementationMetadata<T> { Type = type ?? typeof(T).Name });
.AddSingleton<IActionImplementationMetadata>(ActionImplementationMetadata.FromType(typeof(T),type));
}




public static IServiceCollection AddAction<T,TInput>(this IServiceCollection services, string type = null)
where T : class, IActionImplementation<TInput>
{
return services.AddTransient<T>()
.AddSingleton<IActionImplementationMetadata>(new ActionImplementationMetadata<T,TInput> { Type = type ?? typeof(T).Name });
.AddSingleton<IActionImplementationMetadata>(ActionImplementationMetadata.FromType(typeof(T),typeof(TInput), type));
}

public static IServiceCollection AddWorkflow<T>(this IServiceCollection services) where T :class, IWorkflow
Expand Down
30 changes: 30 additions & 0 deletions src/WorkflowEngine.Core/Workflow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace WorkflowEngine.Core
{
Expand Down Expand Up @@ -28,6 +32,32 @@ public class Workflow<TInput> : Workflow, IWorkflowInputs<TInput>
where TInput : class
{

public static IDictionary<string, object> ForwardInputs<TTarget>()
{
var targetkeys = typeof(TTarget).GetProperties().Select(x => x.GetCustomAttribute<JsonPropertyAttribute>().PropertyName).ToHashSet();
return typeof(TInput).GetProperties()
.Where(x => targetkeys.Contains(x.GetCustomAttribute<JsonPropertyAttribute>().PropertyName))
.ToDictionary(x => x.GetCustomAttribute<JsonPropertyAttribute>().PropertyName,
v => $"@triggerBody()?.payload?.values?.{v.GetCustomAttribute<JsonPropertyAttribute>().PropertyName}" as object);
}

public static T WorkflowInput<T>(Expression<Func<TInput,object>> selector)
{
throw new NotImplementedException();
// return "@triggerBody()?.data?.values?.name";
}
public static T Expr<T>(Expression<Func<TInput, T>> selector)
{
throw new NotImplementedException();
// return "@triggerBody()?.data?.values?.name";
}
public static TOut Fn<TFunc,TOut>()
{
throw new NotImplementedException();
// return "@triggerBody()?.data?.values?.name";
}


}


Expand Down

0 comments on commit f226b11

Please sign in to comment.