which design pattern is suitable for this workflow

2019-06-14 11:10发布

问题:

I have a requirement where in I have a set of jobs to be executed from the screen (each job could take hours to execute) and I need to put some rules in place to execute those jobs. it can be represented using a graph.

  1. each job is given a sequence number and jobs should be executed in sequence
  2. however there are set of jobs that can be run in any order and each of them may have a path of their own. for example once A and B are finished, D, E, F can be run in any order, but there can be a rule like G can run only after D is done, H can run only after E is done something like that.
  3. some jobs can be run if anyone of its preceding parallel jobs is successful. if the jobs are A, B, C, D and A, B, C can be run in any order and D can be run only if A or B or C is completed.
  4. some jobs can be run only if all of the preceding parallel jobs are completed. in the above for example there can be a rule like D can be run if A and B and C are completed
  5. there are some checkpoints which means once the checkpoint job is completed, none of the previous can be rerun including the checkpoint job.

somehow I need to be able to express a condition against a job which can be evaluated at runtime. something like E = (A and B) OR (C and D); which means job E can run if A and B are successful OR C and D are successful.

that pretty much summarises different types of rules and my question is, Is there a design pattern that I can use to implement this? Ideally I would like to persist this workflow in database and validate whether a job is allowed to run or not based on that. the windows workflow might be an overkill for this and I am looking for simpler solution that may not be the best, but takes less time.

technology to be used: ASP.NET 3.5, C# 3.0, SQL Server 2008

回答1:

Sounds like you may want to take a look at the Workflow Foundation, in WF4 there is a WorkflowApplication class that allows you to easily embed a workflow into a GUI application.

Check out the "Hosting workflow in WPF" and "Hosting workflow in a Windows Form" sections of this page: http://www.packtpub.com/article/hosting-workflow-applications-microsoft-windows-workflow-foundation-40



回答2:

http://code.djangoproject.com/wiki/GoFlow/sampleprocess that is helped for me. That is powerful arhitecture



回答3:

Ok well I dont think you have implement your requirements with so many governing rules using a simple, straight forward design pattern. You can use a combination of more than one design pattern to arrive at a good design.

But, looking at your requirements I think you can use the template pattern. This design pattern will allow you to define your program skeleton - behavior and sequence. But, since you have a few other rules where the sequence can be changed or parallel processing can occur - template pattern can be used to override the algorithm steps to allow different behavior by using subclasses or template pattern can be used with a hook to skip certain steps and induce different behavior.

I am just thinking out loud here, you will have to carefully weigh your requirements and check if this is the best design pattern for your implementation.



回答4:

each job is given a sequence number and jobs should be executed in sequence

This to me indicates that you need a Queue for adding your jobs to. I'd start by finding an implementation of Priority Queue and adopt it to being aware of all other jobs that are in the queue and assign the priority on the basis of that. That would make sure they were all run in the correct sequence depending on their 'type'.



回答5:

I solved this by storing these job dependencies in a table in a tree format and evaluating it using a recursive function. may not be the best, but it works and configurable.