Skip to content

Instantly share code, notes, and snippets.

@fjeldstad
Last active October 5, 2015 07:56
Show Gist options
  • Save fjeldstad/cb8da53be209a7a0775b to your computer and use it in GitHub Desktop.
Save fjeldstad/cb8da53be209a7a0775b to your computer and use it in GitHub Desktop.
public interface INode
{
}
public interface IMessageToPublish
{
object Message { get; }
TimeSpan? Delay { get; }
}
public class MessageToPublish
{
public object Message { get; }
public TimeSpan? Delay { get; }
public MessageToPublish(object message, TimeSpan? delay = null)
{
Message = message;
Delay = delay;
}
}
public abstract class Node<TState> : INode
{
public interface IHandle<TMessage>
{
Task<IMemory> AccessMemory(TMessage message);
ITransformResult Transform(TMessage message, TState state);
}
public interface IMemory
{
TState State { get; }
Func<TState, Task> Overwrite { get; }
}
public interface ITransformResult
{
TState NextState { get; }
IMessageToPublish[] MessagesToPublish { get; }
}
public class Memory : IMemory
{
public TState State { get; }
public Func<TState, Task> Overwrite { get; }
public Memory(TState state, Func<TState, Task> overwrite)
{
State = state;
Overwrite = overwrite;
}
}
public class TransformResult : ITransformResult
{
public TState NextState { get; }
public IMessageToPublish[] MessagesToPublish { get; }
public TransformResult(TState nextState, params IMessageToPublish[] messagesToPublish)
{
NextState = nextState;
MessagesToPublish = messagesToPublish ?? new IMessageToPublish[0];
}
}
}
@fjeldstad
Copy link
Author

To get all node implementations in the currently executing assembly (for example) together with the types of input messages they expect:

Assembly.GetExecutingAssembly()
    .GetTypes()
    .Where(x => typeof(INode).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract)
    .Select(x => new
    {
        NodeType = x,
        InputMessageTypes = x
            .GetInterfaces()
            .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(Node<>.IHandle<>))
            .Select(i => i.GetGenericArguments()[1])
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment