请注意,本站并不支持低于IE8的浏览器,为了获得最佳效果,请下载最新的浏览器,推荐下载 Chrome浏览器
欢迎光临。交流群:166852192

orchard工作流中动作(Task)和事件(Event)简介


orchard中动作(Task 估且这么叫吧)和事件(Event 不同于事件总线的事件IEventHandler)都是接口IActivity(位于Orchard.Workflows.Services名称空间下)的两个抽象类,两者的区别在于Event可以启动工作流,需要触发,而Task仅执行任务,其实Task和Event没有本质上区分,两个还可以转化。
看下接口IActivity:
public interface IActivity : IDependency {
        string Name { get; }
        LocalizedString Category { get; }
        LocalizedString Description { get; }
        bool IsEvent { get; }
        bool CanStartWorkflow { get; }
        string Form { get; }
        /// <summary>
        /// List of possible outcomes when the activity is executed.
        /// </summary>
        IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext);
        /// <summary>
        /// Whether the activity can transition to the next outcome. Can prevent the activity from being transitioned
        /// because a condition is not valid.
        /// </summary>
        bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext);
        /// <summary>
        /// Executes the current activity
        /// </summary>
        /// <returns>The names of the resulting outcomes.</returns>
        IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext);
        /// <summary>
        /// Called on each activity when a workflow is about to start
        /// </summary>
        void OnWorkflowStarting(WorkflowContext context, CancellationToken cancellationToken);


        /// <summary>
        /// Called on each activity when a workflow has started
        /// </summary>
        void OnWorkflowStarted(WorkflowContext context);


        /// <summary>
        /// Called on each activity when a workflow is about to be resumed
        /// </summary>
        void OnWorkflowResuming(WorkflowContext context, CancellationToken cancellationToken);


        /// <summary>
        /// Called on each activity when a workflow is resumed
        /// </summary>
        void OnWorkflowResumed(WorkflowContext context);


        /// <summary>
        /// Called on each activity when an activity is about to be executed
        /// </summary>
        void OnActivityExecuting(WorkflowContext workflowContext, ActivityContext activityContext, CancellationToken cancellationToken);


        /// <summary>
        /// Called on each activity when an activity has been executed
        /// </summary>
        void OnActivityExecuted(WorkflowContext workflowContext, ActivityContext activityContext);


    }
加红部分就是动作和事件的区分,看下具体的代码。
public abstract class Task : IActivity {


        public abstract string Name { get; }
        public abstract LocalizedString Category { get; }
        public abstract LocalizedString Description { get; }


        public virtual bool IsEvent {
            get { return false; }
        }


        public bool CanStartWorkflow { get { return false; } }


        public virtual string Form {
            get { return null; }
        }


        public abstract IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext);


        public virtual bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext) {
            return true;
        }


        public abstract IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext);


        public virtual void OnWorkflowStarting(WorkflowContext context, CancellationToken cancellationToken) {
        }


        public virtual void OnWorkflowStarted(WorkflowContext context) {
        }


        public virtual void OnWorkflowResuming(WorkflowContext context, CancellationToken cancellationToken) {
        }


        public virtual void OnWorkflowResumed(WorkflowContext context) {
        }


        public virtual void OnActivityExecuting(WorkflowContext workflowContext, ActivityContext activityContext, CancellationToken cancellationToken) {
        }


        public virtual void OnActivityExecuted(WorkflowContext workflowContext, ActivityContext activityContext) {
        }
    }
public abstract class Event : IActivity {


        public abstract string Name { get; }
        public abstract LocalizedString Category { get; }
        public abstract LocalizedString Description { get; }


        public virtual bool IsEvent {
            get { return true; }
        }


        public virtual string Form {
            get { return null; }
        }


        public virtual bool CanStartWorkflow {
            get { return false; }
        }


        public abstract IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext);


        public virtual bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext) {
            return true;
        }


        public abstract IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext);
        
        public virtual void OnWorkflowStarting(WorkflowContext context, CancellationToken cancellationToken) {
        }


        public virtual void OnWorkflowStarted(WorkflowContext context) {
        }


        public virtual void OnWorkflowResuming(WorkflowContext context, CancellationToken cancellationToken) {
        }


        public virtual void OnWorkflowResumed(WorkflowContext context) {
        }


        public virtual void OnActivityExecuting(WorkflowContext workflowContext, ActivityContext activityContext, CancellationToken cancellationToken) {
        }


        public virtual void OnActivityExecuted(WorkflowContext workflowContext, ActivityContext activityContext) {
        }
    }
实际应用中,启动工作流的事件都继承自Event类,并将
public virtual bool CanStartWorkflow {
            get { return false; }
        }
改为,以便启动工作流
public bool CanStartWorkflow {
            get { return true; }
        }



作者原创内容不容易,如果觉得内容不错,请点击右侧“打赏”,赏俩给作者花花,也算是对作者付出的肯定,也可以鼓励作者原创更多更好内容。
更多详情欢迎到QQ群 166852192 交流。