.net 何时使用 Windows Workflow Foundation?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/104099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:00:12  来源:igfitidea点击:

When to use Windows Workflow Foundation?

.networkflowworkflow-foundation

提问by Sumrak

Some things are easier to implement just by hand (code), but some are easier through WF. It looks like WF can be used to create (almost) any kind of algorithm. So (theoretically) I can do all my logic in WF, but it's probably a bad idea to do it for all projects.

有些事情只需手动(代码)就更容易实现,但有些事情通过 WF 更容易实现。看起来 WF 可用于创建(几乎)任何类型的算法。所以(理论上)我可以在 WF 中完成我的所有逻辑,但对所有项目都这样做可能是一个坏主意。

In what situations is it a good idea to use WF and when will it make things harder then they have to be? What are pros and cons/cost of WF vs. coding by hand?

在什么情况下使用 WF 是一个好主意,什么时候它会使事情变得更难?WF 与手工编码的优缺点/成本是什么?

采纳答案by Panos

You may need WF only if any of the following are true:

仅当以下任一情况为真时,您才可能需要 WF:

  1. You have a long-running process.
  2. You have a process that changes frequently.
  3. You want a visual model of the process.
  1. 你有一个长期运行的过程。
  2. 您有一个经常变化的流程。
  3. 您需要过程的可视化模型。

For more details, see Paul Andrew's post: What to use Windows Workflow Foundation for?

有关更多详细信息,请参阅 Paul Andrew 的帖子:Windows Workflow Foundation 的用途是什么?

Please do not confuse or relate WF with visual programming of any kind. It is wrong and can lead to very bad architecture/design decisions.

请不要将 WF 与任何类型的可视化编程混淆或联系起来。这是错误的,可能会导致非常糟糕的架构/设计决策。

回答by Ronnie Overby

Never. You will probably regret it:

绝不。你可能会后悔:

  • Steep learning curve
  • Difficult to debug
  • Difficult to maintain
  • Doesn't provide enough power, flexibility, or productivity gain to justify its use
  • Can and will corrupt application state that cannot be recovered
  • 陡峭的学习曲线
  • 调试困难
  • 难以维护
  • 没有提供足够的功能、灵活性或生产力增益来证明其使用是合理的
  • 可以并且将会破坏无法恢复的应用程序状态

The only time I could ever conceive of using WF is if I wanted to host the designer for an end-user and probably not even then.

我唯一能想到使用 WF 的情况是,如果我想为最终用户托管设计器,甚至可能不会。

Trust me, nothing will ever be as straightforward, powerful, or flexible as the code that you write to do exactly what you need it to do. Stay away from WF.

相信我,没有什么能像您编写的代码一样直接、强大或灵活,以完全满足您的需要。远离WF。

Of course, this is only my opinion, but I think it's a damn good one. :)

当然,这只是我的观点,但我认为这是一个非常好的观点。:)

回答by craigb

The code generated by WF is nasty. The value that WF brings is in the visual representation of the system, although I have yet to see anything (6-7 projects at work now with WF that i've been involved with) where I would not have preferred a simpler hand coded project.

WF 生成的代码很讨厌。WF 带来的价值在于系统的可视化表示,尽管我还没有看到任何我不喜欢更简单的手工编码项目的东西(现在有 6-7 个项目与我参与的 WF 一起工作) .

回答by Mas

In general, if you do not need the persistence and tracking features (which in my opinion are the main features), you shouldn't use Workflow Foundation.

一般来说,如果您不需要持久性和跟踪功能(我认为这是主要功能),则不应使用 Workflow Foundation。

Here are the advantages and disadvantages of Workflow Foundation I gathered from my experience:

以下是我从经验中收集到的 Workflow Foundation 的优缺点:

Advantages

好处

  • Persistence: If you're going to have many long running processes (think days, weeks, months), then Workflows are great for this. Idle workflow instances are persisted to the database so it doesn't use up memory.
  • Tracking: WF provides the mechanism to track each activity executed in a workflow
  • *Visual Designer: I put this as a *, because I think this is really only useful for marketing purposes. As a developer, I prefer to write code rather than snapping things together visually. And when you have a non-developer making workflows, you often end up with a huge confusing mess.
  • 持久性:如果您将有许多长时间运行的流程(想想几天、几周、几个月),那么工作流非常适合于此。空闲的工作流实例被持久化到数据库中,因此它不会耗尽内存。
  • 跟踪:WF 提供了跟踪工作流中执行的每个活动的机制
  • *视觉设计师:我把它作为*,因为我认为这真的只对营销目的有用。作为一名开发人员,我更喜欢编写代码,而不是在视觉上将事物拼凑在一起。当你有一个非开发人员制作工作流程时,你经常会遇到一个巨大的混乱。

Disadvantages

缺点

  • Programming Model: You're really limited in programming features. Think about all the great features you have in C#, then forget about them. Simple one or two line statements in C# becomes a fairly large block activities. This is particularly a pain for input validation. Having said that, if you're really careful to keep only high-level logic in workflows, and everything else in C#, then it might not be a problem.
  • Performance: Workflows use up a large amount of memory. If you're deploying a lot of workflows on a server, make sure you have tons of memory. Also be aware that workflows are much slower than normal C# code.
  • Steep learning curve, hard to debug: As mentioned above. You're going to spend a lot of time figuring out how to get things to work, and figuring out the best way to do something.
  • Workflow Version Incompatibility: If you deploy a workflow with persistence, and you need to make updates to the workflow, the old workflow instances are no longer compatible. Supposedly this is fixed in .NET 4.5.
  • You have to use VB expressions (.NET 4.5 allows for C# expressions).
  • Not flexible: If you need some special or specific functionality not provided by Workflow Foundation, prepare for a lot of pain. In some cases, it might not even be possible. Who knows until you try? There's a lot of risk here.
  • WCF XAML services without interfaces: Normally with WCF services, you develop against an interface. With WCF XAML Services, you cannot ensure a WCF XAML Service has implemented everything in an interface. You don't even need to define an interface. (as far as I know...)
  • 编程模型:您在编程功能方面确实受到限制。想想您在 C# 中拥有的所有强大功能,然后忘记它们。在 C# 中简单的一两行语句变成了一个相当大的活​​动块。这对于输入验证来说尤其痛苦。话虽如此,如果您真的很小心地在工作流中只保留高级逻辑,而在 C# 中保留其他所有内容,那么这可能不是问题。
  • 性能:工作流占用大量内存。如果您在服务器上部署大量工作流,请确保您有大量内存。另请注意,工作流比普通 C# 代码慢得多。
  • 陡峭的学习曲线,难以调试:如上所述。您将花费大量时间来弄清楚如何让事情发挥作用,并找出做某事的最佳方式。
  • 工作流版本不兼容:如果您部署具有持久性的工作流,并且您需要对工作流进行更新,则旧的工作流实例将不再兼容。据说这是在 .NET 4.5 中修复的。
  • 您必须使用 VB 表达式(.NET 4.5 允许使用 C# 表达式)。
  • 不灵活:如果您需要一些 Workflow Foundation 未提供的特殊或特定功能,请准备好承受很多痛苦。在某些情况下,它甚至可能是不可能的。谁知道直到你尝试?这里有很大的风险。
  • 没有接口的 WCF XAML 服务:通常使用 WCF 服务,您针对接口进行开发。使用 WCF XAML 服务,您无法确保 WCF XAML 服务已在接口中实现所有内容。您甚至不需要定义接口。(据我所知...)

回答by Tegan Mulholland

The major reason I've found for using workflow foundation is how much it brings you out of the box in terms of tracking and persistence. It's very easy to get the persistence service up and running, which brings reliability and load distribution between multiple instances and hosts.

我发现使用工作流基础的主要原因是它在跟踪和持久性方面让您开箱即用。启动和运行持久性服务非常容易,这带来了多个实例和主机之间的可靠性和负载分配。

On the other hand, just like forms apps, the code patterns that the workflow designer pushes you towards are bad. But you can avoid problems by writing no code in the workflow and delegating all work to other classes, which can be organized and unit tested more gracefully than the workflow. Then you get the cool visual aspect of the designer without the cruft of spaghetti code behind.

另一方面,就像表单应用程序一样,工作流设计者推给你的代码模式是糟糕的。但是您可以通过在工作流中不编写代码并将所有工作委托给其他类来避免问题,这些类可以比工作流更优雅地进行组织和单元测试。然后,您将获得设计师的酷炫视觉效果,而无需背后繁琐的意大利面条式代码。

回答by Rob

Personally, I'm not sold on WF. It's usefulness wasn't as obvious to me as other new MS technologies, like WPF or WCF.

就个人而言,我不在 WF 上出售。它的用处对我来说不像其他新的 MS 技术(如 WPF 或 WCF)那么明显。

I think WF will be used heavily in business applications in the future, but I have no plans to use it because it doesn't seem like the right tool for the job for my projects.

我认为 WF 将来会在业务应用程序中大量使用,但我没有使用它的计划,因为它似乎不是我项目工作的正确工具。

回答by Rob

The company I am currently working for set up a Windows Workflow Foundation (WF) and the reasons they chose to use it was because the rules would frequently be changing and that would force them to do a recompile of the various dll's etc and so their solution was to place the rules in the DB and call them from there. This way they could change the rules and not have to recompile and redistribute the dlls etc.

我目前工作的公司设立了 Windows Workflow Foundation (WF),他们选择使用它的原因是因为规则经常变化,这将迫使他们重新编译各种 dll 等,因此他们的解决方案是将规则放在数据库中并从那里调用它们。这样他们就可以更改规则,而不必重新编译和重新分发 dll 等。

回答by Owain Glynd?r

Windows Workflows seduce non-coding IT managers, BAs and the like as does its cousin BizTalk but in practice unit testing, debugging and code coverage are just three of the many pitfalls. You can overcome some of them but you have to invest heavily in achieving that whereas with plain code you just get that. If you genuinely have a long-running requirement then you probably need something more sophisticated. I've heard the argument about being able to drop new xaml files into production without re-compiling dlls but honestly the time that Workflows will consume could be better used to improve your Continuous Integration to the point where compiled deploys aren't a problem.

Windows 工作流吸引非编码 IT 经理、BA 等,就像它的表亲 BizTalk 一样,但在实践中,单元测试、调试和代码覆盖率只是众多陷阱中的三个。您可以克服其中的一些问题,但您必须投入大量资金来实现这一目标,而使用普通代码,您只能做到这一点。如果您确实有长期运行的需求,那么您可能需要更复杂的东西。我听说过关于无需重新编译 dll 即可将新的 xaml 文件投入生产的争论,但老实说,工作流将消耗的时间可以更好地用于改进您的持续集成,使编译部署不成问题。

回答by BinaryMisfit

I would use it in any environment where I need to work with workflow, however when using it in conjuction with K2 or even SharePoint 2007 the power of the platform is truly useful. When developing business applications with BI specialist the use of the platform is recommended and this would normally only be relevant to streamline and improve business processes.

我会在需要处理工作流的任何环境中使用它,但是当将它与 K2 甚至 SharePoint 2007 结合使用时,该平台的强大功能确实非常有用。在与 BI 专家一起开发业务应用程序时,建议使用该平台,这通常仅与简化和改进业务流程相关。

For the record WF was developed in conjunction with K2's development team and the new K2 Blackpearl is built on top of WF, so is MOSS 2007 and WSS 3.0's workflow engines.

郑重声明,WF 是与 K2 的开发团队共同开发的,新的 K2 Blackpearl 构建在 WF 之上,MOSS 2007 和 WSS 3.0 的工作流引擎也是如此。

回答by sparksustc

When you don't want to manually write all those codes to maintain the visual interface, tracking and persistence, it's a wise choice to vote for WF.

当您不想手动编写所有这些代码来维护可视化界面、跟踪和持久性时,投票给 WF 是一个明智的选择。