哪种设计模式适合此工作流程要求?
我有一个要求,其中有一组要从屏幕执行的作业(每个作业可能需要几个小时才能执行),并且我需要制定一些规则来执行这些作业。它可以用图表来表示。
- 每个作业都有一个序列号,并且作业应该按顺序执行,
- 但是有一组作业可以按任何顺序运行,并且每个作业都可以有自己的路径。例如,一旦A和B完成,D,E,F可以以任何顺序运行,但是可以有一个规则,比如G只能在D完成后运行,H只能在E完成后运行,类似的事情。
- 如果某些作业之前的任何并行作业成功,则可以运行某些作业。如果作业是 A、B、C、D,并且 A、B、C 可以按任意顺序运行,并且只有 A、B 或 C 完成后才能运行 D。
- 有些作业只有在前面的所有并行作业都完成后才能运行。例如,在上面的例子中,如果 A、B 和 C 完成,则可以运行 D 之类的规则。
- 存在一些检查点,这意味着一旦检查点作业完成,之前的任何作业都不能重新运行,包括检查点作业。
不知何故,我需要能够针对可以在运行时评估的作业表达一个条件。类似 E = (A 和 B) OR (C 和 D);这意味着如果 A 和 B 成功或者 C 和 D 成功,作业 E 就可以运行。
这几乎总结了不同类型的规则,我的问题是,是否有一种设计模式可以用来实现这个?理想情况下,我想将这个工作流程保留在数据库中,并根据该工作流程验证是否允许运行作业。 Windows 工作流程对此可能有点过分,我正在寻找更简单的解决方案,该解决方案可能不是最好的,但需要更少的时间。
使用的技术:ASP.NET 3.5、C# 3.0、SQL Server 2008
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.
- each job is given a sequence number and jobs should be executed in sequence
- 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.
- 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.
- 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
- 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来您可能想看看 Workflow Foundation,在 WF4 中,有一个
WorkflowApplication
类,可让您轻松地将工作流嵌入到 GUI 应用程序中。查看本页的“在 WPF 中托管工作流”和“在 Windows 窗体中托管工作流”部分:http://www.packtpub.com/article/hosting-workflow-applications-microsoft-windows-workflow-foundation-40
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
http://code.djangoproject.com/wiki/GoFlow/sampleprocess 有助于我。这就是强大的架构
http://code.djangoproject.com/wiki/GoFlow/sampleprocess that is helped for me. That is powerful arhitecture
好吧,我不认为您已经使用简单、直接的设计模式通过如此多的管理规则来实现您的需求。您可以组合使用多种设计模式来获得良好的设计。
但是,考虑到您的要求,我认为您可以使用模板模式。这种设计模式将允许您定义程序骨架 - 行为和顺序。但是,由于您还有一些其他规则可以更改序列或可以进行并行处理 - 模板模式可用于覆盖算法步骤,以通过使用子类来允许不同的行为,或者模板模式可与钩子一起使用以跳过某些步骤并诱发不同的行为。
我只是在这里大声思考,您必须仔细权衡您的需求并检查这是否是最适合您实现的设计模式。
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.
这表明您需要一个队列来添加作业。我首先找到 优先级队列 的实现并采用它了解队列中的所有其他作业并据此分配优先级。这将确保它们都根据它们的“类型”以正确的顺序运行。
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'.
我通过将这些作业依赖项以树格式存储在表中并使用递归函数对其进行评估来解决了这个问题。可能不是最好的,但它有效且可配置。
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.