DotLee

开源项目dropthings的工作流接口实现WorkflowHelper

开源项目dropthings的工作流接口实现WorkflowHelper

 

(注:本项目中,在业务层中使用工作流的方式引起了一些争议。

但我的本意是了解这个开源项目,所以,文章会继续下去,不管结果如何。)

系统版本 Dropthings-2.1
Dropthings.Business.Workflows.WorkflowHelper

用途:在业务层中,工作流被同步执行;而默认下,是异步。并且,
     异步工作流的返回值的取得,是比较复杂的。
     在asp.net环境中,异步执行需要另外的线程,
     如:线程的间接启动,停止额外的线程,
     等待线程,线程间的数据传递等。
     因此,同步比较好。
    
     所以,此类用于工作流环境的设置。
  
   一、创建工作流执行环境 
   ==================
 其中:    //创建默认的asp.net工作流环境为同步
      public static WorkflowRuntime CreateDefaultRuntime()
        {
            //创建工作流运行库
            WorkflowRuntime workflowRuntime = new WorkflowRuntime();

            //添加【负责同步执行工作流的服务ManualWorkflowSchedulerService】
            var manualService = new ManualWorkflowSchedulerService();
            workflowRuntime.AddService(manualService);
           
            //Activities.CallWorkflowService服务,支持在一个工作流中运行另外的工作流
            var syncCallService = new Activities.CallWorkflowService();
            workflowRuntime.AddService(syncCallService);

            workflowRuntime.StartRuntime();

            return workflowRuntime;
        }
为了确保一个应用程序域只有一个工作流执行环境,在Global.asax中如下设置:
void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序开始时执行
        // 在应用程序中创建唯一的工作流环境并保存到应用程序环境中
        System.Workflow.Runtime.WorkflowRuntime runtime = Dropthings.Business.Workflows.WorkflowHelper.CreateDefaultRuntime();
        Application[APPLICATION_WORKFLOW_RUNTIME_KEY] = runtime;//这里是弱类型?????

        //Unity是微软Patterns & Practices团队所开发的一个轻量级的,可扩展的依赖注入(Dependency Injection)容器
        //http://unity.codeplex.com
        //负责工作流执行环境生命周期和容器一致。当离开容器的作用域范围时,容器会被销毁,同时工作流执行环境也被销毁。
        //当容器被显式销毁时,工作流执行环境也被销毁。在容器有效的作用域范围,
        //使用Resolve/ResolveAll方法获取工作流执行环境的时候,容器会在第一次调用时创建实例,但在后面的调用中,始终只得到相同的实例,利用这种Manager实现Singleton模式。
        //工作流执行环境不是强类型引用的时候,可能会被GC给回收。
 
        //此句不知道用中文如何表达:Setup default Dependencies for regular execution where all dependencies are real
        ObjectContainer.RegisterInstanceExternalLifetime<WorkflowRuntime>(runtime);
       
        //注入IWorkflowHelper的实例WorkflowHelper,在每个线程中返回同一个对象实例WorkflowHelper
        ObjectContainer.RegisterTypePerThread<IWorkflowHelper, WorkflowHelper>();
    }
   
    二、销毁工作流运行环境
    ===============
        void Application_End(object sender, EventArgs e)
    {
        //关闭应用程序是运行
        //关闭工作流运行环境
        System.Workflow.Runtime.WorkflowRuntime runtime = Application[APPLICATION_WORKFLOW_RUNTIME_KEY] as System.Workflow.Runtime.WorkflowRuntime;
        if (null != runtime)
            Dropthings.Business.Workflows.WorkflowHelper.TerminateDefaultRuntime(runtime);

        //拆卸依赖容器
        Dropthings.Business.Container.ObjectContainer.Dispose();
    }
   
    三、运行实际的工作流
    public static TResponse Run<TWorkflow, TRequest, TResponse>(TRequest request)
            where TResponse : new()
        {
            return ObjectContainer.Resolve<IWorkflowHelper>().ExecuteWorkflow<TWorkflow, TRequest, TResponse>(
                ObjectContainer.Resolve<WorkflowRuntime>(),
                request);
        }
       
       
    四、ObjectContainer 依赖注入。
     
   
   
   

posted on 2009-05-25 14:56  DotLee  阅读(1268)  评论(0编辑  收藏  举报