MicroFlow icon indicating copy to clipboard operation
MicroFlow copied to clipboard

Flow Usage Question

Open pirvudoru opened this issue 8 years ago • 1 comments

I am trying to implement BL using Flows. Is there any opinionated way to:

  • set input to a Flow
  • get output for a Flow
  • use an existing Dependency container (Microsoft.Extensions.DependencyInjection)

From what i see from the docs / src a flow does not have an extensibility point to replace the DI container. An option of setting input is to pass it when constructing the flow via TInput Ctor arg / Property and get the output after run from another Property IValue<TOutput> that is updated after the last activity is executed.

A few observations about the design:

  • Flows must be transient - downside the FlowDescriptor gets rebuilt at every run, container is reconfigured every time
  • No option for composing flows (eg: a Flow to be considered an activity on it's own) - not an issue actually, maybe not actually desired to allow flow composition

Sample for Flow with I/O (most probably not best way to do it):

public abstract class Flow<TInput, TOutput> : Flow
    {
        public override string Name => GetType().Name;

        protected TInput Input { get; set; }

        protected Variable<TOutput> Output { get; set; }

    // Sync example
        public TOutput Run(TInput input)
        {
            Input = input;

            Run().Wait();

            return Output.CurrentValue;
        }

        protected abstract void BuildCore(FlowBuilder builder);

        protected override void Build(FlowBuilder builder)
        {
            Output = builder.Variable<TOutput>();

            BuildCore(builder);

            builder.WithDefaultCancellationHandler<DefaultCancelationandler>()
                .WithDefaultFaultHandler<DefaultFaultHandler>();
        }
    }


public class XFlow : Flow<XInput, XOutput>
{
    protected override void BuildCore(FlowBuilder builder)
    {
        var activity = builder.Activity<XActivity>();
        activity.Bind(a => a.Input).To(Input);
        activity.OnCompletionUpdate(Output, activityOutput => new XOutput());

        builder.WithInitialNode(activity);
    }
}

Usage:

XOutput output = new XFlow().Run(new XInput());

pirvudoru avatar Mar 01 '16 15:03 pirvudoru

Hi Doru! Thanks for your feedback.

Yes, you correctly understood how to set input and output. I'll work on supporing generic flows, using an existing DI container and other topics you mentioned. Some of these features will be available soon, some a little bit later. Stay tuned!

Thanks

akarpov89 avatar Mar 07 '16 09:03 akarpov89