计划通过输入参数实现接口的子活动 [英] Scheduling child activity that implements an interface with input parameters

查看:67
本文介绍了计划通过输入参数实现接口的子活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public sealed class Parent : NativeActivity
{
    public Parent()
    {
        Childrens = new Collection<Activity>();
        Variables = new Collection<Variable>();

        _currentActivityIndex = new Variable<int>();
        CurrentCustomTypeInstance= new Variable<MyCustomType>();
    }

    [Browsable(false)]
    public Collection<Activity> Childrens { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        _currentActivityIndex.Set(context, 0);
        context.ScheduleActivity(FirstActivity, Callback);
    }

    private void Callback(NativeActivityContext context, ActivityInstance completedInstance, MyCustomType customTypeInstance)
    {
        CurrentCustomTypeInstance.Set(context, customTypeInstance);
        ScheduleNextChildren(context, completedInstance);
    }

    private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
    {
        int nextActivityIndex = _currentActivityIndex.Get(context) + 1;
        if (nextActivityIndex >= Childrens.Count)
            return;

        Activity nextActivity = Childrens[nextActivityIndex];

        IFoo nextActivityAsIFoo = nextActivity as IFoo;
        if (nextActivityAsIFoo != null)
        {
            var currentCustomTypeInstance = CurrentCustomTypeInstance.Get(context);
            // HERE IS MY EXCEPTION
            nextActivityAsIFoo.FooField.Set(context, currentCustomTypeInstance);
        }

        context.ScheduleActivity(nextActivity);
        _currentActivityIndex.Set(context, nextActivityIndex);

    }
}

并在注册元数据中:

metadata.SetChildrenCollection(Childrens);

我已经阅读 http://msmvps.com/blogs/theproblemsolver/archive/2011/04/05/ schedule-child-activities-with-input-parameters.aspx ,但在我的情况下,父母不知道孩子的活动

I've already read http://msmvps.com/blogs/theproblemsolver/archive/2011/04/05/scheduling-child-activities-with-input-parameters.aspx but in my case, parent does not know the child activity

编辑

类似于:活动无法设置在其范围内定义的变量?

Activity '1.1: Parent' cannot access this variable because it is declared at the scope
of activity '1.1: Parent'. An activity can only access its own implementation variables.

但就我而言,我不需要获取返回值,因此希望更轻松。只需隐式传递FooField即可,而不是让它流作者。
我需要隐式地做!如果根本不起作用,我将使用NativeActivityContext Properties

But in my case, I don't need to get the return value, so, hope to be easier. Just need to pass FooField implicitly instead of leting it to flow author. I need to do it implicitly! If it doesn't work at all, I will go with NativeActivityContext Properties

推荐答案

知道了!看着莫里斯(Maurice)的博客,我受到启发去做

Got it! Looking at the Maurice's blog I got inspired to do it

工作流程(非常简单!)

static void Main(string[] args)
{
    Parent parent = new Parent();
    parent.Childrens.Add(new FooWriter());
    parent.Childrens.Add(new FooFormater());
    parent.Childrens.Add(new FooWriter());

    WorkflowInvoker.Invoke(parent);
    Console.Read();
}

输出

What's the Foo name?
Implicit FTW!
Im a custom Foo Handler, my Foo name is: Implicit FTW!
Im a custom Foo Handler, my Foo name is: IMPLICIT FTW!

实施

using System;
using System.Activities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WorkflowConsoleApplication2
{
// Parent class that creates a Foo and passes it to their childrens
public sealed class Parent : NativeActivity
{
    private Variable<int> _currentActivityIndex;
    private Variable<Foo> _currentFoo;

    public Parent()
    {
        Childrens = new Collection<Activity>();
        _executionChildrens = new Collection<Tuple<Activity, ActivityAction<Foo>>>();

        _currentActivityIndex = new Variable<int>();
        _currentFoo = new Variable<Foo>();
    }

    [Browsable(false)]
    public Collection<Activity> Childrens { get; set; }
    private Collection<Tuple<Activity, ActivityAction<Foo>>> _executionChildrens;

    protected override void Execute(NativeActivityContext context)
    {
        Console.WriteLine("What's the Foo name?");
        _currentFoo.Set(context, new Foo { Name = Console.ReadLine() });

        _currentActivityIndex.Set(context, 0);
        ScheduleNextChildren(context, null);
    }

    private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
    {
        int currentActivityIndex = _currentActivityIndex.Get(context);
        if (currentActivityIndex >= Childrens.Count)
            return;

        Tuple<Activity, ActivityAction<Foo>> nextActivity = _executionChildrens[currentActivityIndex];

        if (IsFooHandler(nextActivity))
        {
            context.ScheduleAction(nextActivity.Item2, _currentFoo.Get(context),
                ScheduleNextChildren);
        }
        else
        {
            context.ScheduleActivity(nextActivity.Item1,
                ScheduleNextChildren);
        }

        _currentActivityIndex.Set(context, currentActivityIndex + 1);

    }

    protected override void CacheMetadata(NativeActivityMetadata metadata)
    {
        metadata.SetArgumentsCollection(metadata.GetArgumentsWithReflection());

        metadata.AddImplementationVariable(_currentActivityIndex);
        metadata.AddImplementationVariable(_currentFoo);

        RegisterChildrens(metadata, Childrens);
        // remove "base.Cachemetadata" to "Childrens collection" doesn't become a child   again        }

    public void RegisterChildrens(NativeActivityMetadata metadata, IEnumerable<Activity> childrens)
    {
        foreach (Activity child in childrens)
        {
            IFooHandler childAsIFooHandler = child as IFooHandler;
            if (childAsIFooHandler != null)
            {
                ActivityAction<Foo> childsWrapperAction = new ActivityAction<Foo>();

                var activityToActionBinderArgument = new DelegateInArgument<Foo>();
                childsWrapperAction.Argument =  activityToActionBinderArgument;
                childAsIFooHandler.Foo =        activityToActionBinderArgument;

                childsWrapperAction.Handler = child;

                metadata.AddDelegate(childsWrapperAction);
                _executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, childsWrapperAction));
            }
            else
            {
                metadata.AddChild(child);
                _executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, null));
            }
        }
    }

    public static bool IsFooHandler(Tuple<Activity, ActivityAction<Foo>> activity)
    {
        return activity.Item2 != null;
    }
}

// samples of Foo handlers
public class FooWriter : CodeActivity, IFooHandler
{
    /// When FooWriter is direct child of "Parent" this argument is passed implicitly
    public InArgument<Foo> Foo { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        Console.WriteLine("Im a custom Foo Handler, my Foo name is: {0}", Foo.Get(context).Name);
    }
}

public class FooFormater : CodeActivity, IFooHandler
{
    public InArgument<Foo> Foo { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        Foo foo = Foo.Get(context);
        foo.Name = foo.Name.ToUpper();
    }
}

// sample classes
public class Foo
{
    public string Name { get; set; }
}

public interface IFooHandler
{
    InArgument<Foo> Foo { get; set; }
}
}

如果有人知道如何做得更好方式请随意告诉我。我还需要将值传递给嵌套活动

If anyone know how to do it in a better way please fell free to tell me. I will also need to pass the values to nested activities

这篇关于计划通过输入参数实现接口的子活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆