工作流设计困境 - 状态机,是或否
我是 WF 的初学者,但我读过一本书并进行了大量的谷歌搜索。我想编写一个库存管理服务。库存由具有状态的单个项目组成:
- 备用
- 、安装
- 、维修中的
项目可能会在每个状态花费数月时间,并且有数千个项目。
问题是,我是否为所有不同的状态创建状态机工作流程?或者我是否创建用于状态之间转换的工作流程?
如果我理解正确,如果我创建一个状态机工作流程,那么每个项目都将始终运行一个工作流程。这意味着数千个不断运行的工作流程。另外,我需要能够显示每个项目的状态快照,这意味着我必须以某种方式查询所有工作流程以了解它们当前所处的状态,或者在每次状态转换后保留到数据库。
然而,状态机工作流程在逻辑上听起来是正确的做法,因此我陷入了困境。
如果可以的话请帮助我:-)
谢谢!
更新:
假设我的状态多于上述 3 个状态,并且并非所有状态转换都是可能的。
赏金获得者:Maurice - 感谢其他人真正帮助我更多地了解工作流程、MS 工作流程基础和其他更轻量级的替代方案。不幸的是,只能有一个赏金获得者,莫里斯的回答及其评论对我帮助最大。
I'm a beginner with WF, but I've read a book and done a lot of googling. I want to write an inventory management service. The inventory is made up of individual items which have a state:
- Spare
- Installed
- In-Repair
Items may spend months in each state, and there are thousands of items.
The question is, do I create a state machine workflow for all the different states? Or do I create workflows for transitioning between states?
If I understand correctly, if I create a single state machine workflow, then there will always be a workflow running for every item. This means thousands of ever-running workflows. Also, I need to be able to display a snapshot of the status of each item, so that means I have to somehow query all the workflows for the state they are currently in, or otherwise persist to a database after each state transition.
However, a state-machine workflow logically sounds like the right thing to do, hence my dilemma.
Please help me if you can :-)
Thanks!
Update:
Assume I have more states than the above 3, and that not all state transitions are possible.
Bounty Winner: Maurice - Thanks to everyone else for really helping me understand more about workflows, the MS workflow foundation, and other more lightweight alternatives. Unfortunately, there can only be one bounty winner, and Maurice's answer along with its comments helped me the most.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
状态机是一种非常强大的实现技术,尽管我建议您考虑一个名为 由 Nicholas Blumhardt 开发的 StateLess 框架 (Autofaq Creator),因为它是状态机的极其简单的实现,避免了 Windows 工作流不必要的复杂性。他的方法避免了运行时引擎持有长时间运行的工作流的问题,因为状态是由简单的变量(例如字符串或整数)定义的。
这是一个示例状态机:
您的状态可以是一个整数,它允许您从数据库向其提供当前状态。这可以在状态机的构造函数上进行设置,如下所示:
与运行 Windows 工作流所需的多个项目相比,您只需在一个程序集中即可实现这一点。维护成本极低,他们没有为您生成代码的“设计师”,等等。同样,它很简单,而且存在着美丽。
A state machine is a very powerful technique to implement, although I would recommend that you consider a framework called StateLess by Nicholas Blumhardt (Autofaq creator), as it is an amazingly simple implementation of a state machine that avoids the unnecessary complexity of Windows Workflow. His approach avoids the issue of long running workflows being held by a runtime engine, as the state is defined by a simple variable such as a string or int.
Here is a sample state machine:
Your state can be an integer which will allow you to feed it the current state from a database. This can be set on the constructor of the state machine as follows:
You can implement this in just one assembly, compared to the multiple projects you need to run Windows Workflow. Maintenance is extremely low, their is no "designer" that generates code for your, etc. Again, it is simple, and there lies the beauty.
不确定工作流程是否是您首先要寻找的。
工作流是发生的某种业务流程。这意味着该过程的开始和结束。您的描述听起来更像是您正在跟踪库存中保留的物品的库存。
当项目状态发生变化时,工作流程听起来更合适。例如,当一个项目安装完毕后发生故障并需要修复时,您将启动一个工作流程,用可用的零件替换该零件,将损坏的零件发送出去进行修复,最后将其作为修复好的零件返回仓库空闲的。工作流程将描述此过程,从报告损坏的物品开始,到修复或丢弃和更换物品结束。
最后一个工作流程很可能是一个状态工作流程,因为该项目会经历各个阶段,例如:
Not sure if a workflow is what you are looking for in the first place.
A workflow is some kind of business process that takes place. This implies a beginning and an end to the process. Your description sounds more like you are tracking an inventory of sorts with items that stay in the inventory.
A workflow sound more appropriate when an item changes state. For example when an item is installed and breaks down and needs to be fixed you would start a workflow to get the part replaced by a a working one, send the broken part to be fixed and in the end have it back at the warehouse as a fixed spare. The workflow would describe this process and start with the report of an item being broken and end with the item being either repaired or discarded and replaced.
This last workflow could very well be a state worklfow as the item goes through various stages like:
鉴于您的添加(超过 3 个状态,并非允许所有转换),我将执行以下操作:
这可以非常轻松地实现:将成员添加到类或列到桌子等,正如其他人在他们的帖子/评论中已经提到的那样。
然后,您需要做的就是使用状态机转换您的项目,每当发生外部事件时,就会强制/暗示这一点。
Given Your additions (more than 3 states, not all transitions are allowed), I would do the following:
This can be achieved very easily: Add a member to a class or a column to a table etc., as others already mentioned in their posts/comments.
All you need to do then is to transform Your items using the state machine, whenever an extern event occurs, that forces/implies just that.
好的。让我们看看我是否可以帮助你。让我们从设计开始:
设计一个状态机和一个工作流程是有意义的。两者只是对您的问题的不同看法,并从不同的角度阐明了一些问题。事实上,经常发生这样的情况:刚接触工作流的开发人员设计状态机而不是工作流过程。工作流程过程视图主要切换图中框和转换的角色:框是可以更改状态的活动 - 在转换时,工作项可以进入新状态(好吧,这在科学上不正确,但它可能有帮助:-)
对我来说,状态机方面似乎更重要。因此,将您的软件实现为状态机。
是的 - 您应该将所有项目持久保存在数据库中。这就是您保持长时间运行的工作流程运行的方式 - 它们驻留在数据库中,直到被任何活动重新激活为止。所有现成的业务流程管理系统 (BPMS) 都是这样做的。这些工作流程不会保存在内存中。
将所有内容保存在数据库中将使创建报告变得容易。
正如其他人已经提到的,创建一个包含状态信息的新列,甚至创建一个包含元数据的新表:状态,可能是状态更改时的日志,谁更改了状态,有关即将发生的事件的信息(部分已订购)但未交付 - 几天后应有人与供应商核实是否交付已丢失?)。这将使您有机会根据需要添加元数据,而不会影响您的零件数据库。
希望有帮助!
ok. let's see if I can help you. Let's start with the design:
it makes sense to design a state machine AND a Workflow. Both are just different views on your problem and shed some light from a different perspective on it. In fact, it often happens that a developer which is new to workflows designs a state machine instead of a workflow process. The workflow process view mainly switches the roles of the boxes and transitions in the diagram: a box is an activity which can change a state - on a transition, a workitem can get into a new state (ok, thats not scientifically correct, but it might help :-)
For me it seems that the state machine aspect is more important. So implement your software as a state machine.
And yes - you should make all items persistent in a database. That's how you also keep very long running workflows running - they live in the database until they get reactivated by any activity. That's how all off the shelve Business Process Management Systems (BPMS) do it. Those workflows are NOT kept in memory.
Keeping everything in the database will make it easy to create reports.
As other already have mentioned, create a new colum with the state information or even create a new table with meta data: state, maybe a log when the state has changed, who changed the state, infos about upcoming events (a part has been ordered but not delivered - after how many days should someone check with the supplier if the delivery has been lost?). This will give you the opportunity to add meta data as needed without affecting your parts database.
Hope that Helps!
正如原帖评论中所讨论的,我对使用 WF 解决这个特定问题有一些疑问。
“项目可能在每个状态下花费数月时间,并且有数千个项目”这句话是触发这一点的:我认为工作流程(用 WF 建模)的寿命应该更短。虽然对于 WF 实例可以或应该生存多长时间没有硬性规则,但几个月甚至几年对我来说会触发一种“越界”异常,尤其是当这是一种非常常见的情况时对于数千个项目:-)
在您的评论中使用 WF 的原因是合理的,但没有解决这个特定的问题。因此,我建议您对库存项目状态之间发生的事情使用寿命较短的专用 WF 模型,但不要在 WF 状态中捕获项目状态本身。长期存在、很少改变的状态更适合数据库引擎,人们期望它们在哪里并且很容易报告它们。
As discussed in the comments to the original post I have some issues with using WF for this particular problem at all.
The sentence "Items may spend months in each state, and there are thousands of items" is the trigger for this: I believe that workflows (modeled with WF) should be shorter lived. While there is no hard rule as to how long a WF instance can or should live, several months or maybe even years triggers kind of an "out-of-bounds" exception with me, especially when this would be a very common state of affairs for thousands of items :-)
The reasons for using WF in your comments are sound, but don't address this particular point. So I would advise you to use shorter lived, specialized WF models for the things that happen between inventory item states, but don't capture the item state itself in a WF state. Long lived, seldom changed states are more suited to a database engine, where people expect them to be and where they are easily reported upon.
尽管状态机模式在技术上是正确的选项,但也可以选择使用一个巨大循环创建顺序工作流。在某些情况下,它实际上效果更好并且更容易理解。
Even though the state-machine pattern is technically the correct option, there's also an option to create a sequential workflow with one giant loop. In some cases, it actually works better and is more understandable.
你有三种不同的状态,据我所知,所有转换都是允许的。鉴于此,我不会为检查允许的转换并继续前进的一段代码意义上的状态机而烦恼。
当状态驱动业务或需要一段受信任的代码来负责“验证转换”时,状态机就有意义。
更简单地说,您只需要在任何时间点具有给定状态的实体......
You have three distinct states and as far as I can see, all transitions are allowed. Given that, I would not bother about a state-machine in the sense of a piece of code that checks the allowed transitions and does the moving forward.
State machines make sense when the states drive the business or when there needs to be a trusted single piece of code that is the sole actor in charge of "validated transitioning".
Put more simply, you just need the entities with a given state at any point in time...
Aviad,我同意您的评论,即工作流程应该在状态之间进行。正在盘点的零件的状态听起来像是该项目的状态/属性。该状态几乎可以以任何方式存储(例如数据库、文件...),因为状态之间的项目移动将存在于业务逻辑层中。
听起来是一个有趣的项目,祝你好运。
Aviad, I agree with your comment that the workflow should be between states. The states of the parts being inventoried sounds like a status/property of the item. That status can be stored in nearly any manner (e.g. database, file...) because the movement of the item between states would live in the business logic layer.
Sounds like a fun project, good luck.
我认为我们需要在这里了解一些事情。
状态机 - 将专注于表示您的实体所处的特定状态。
工作流程 - 将定义您将实体从初始状态移动到最终状态所遵循的流程。
在您的情况下,由于您的整个过程围绕单个实体进行,并且其在任何给定时间点的当前状态都是单例,因此我建议使用状态机。
建议:
Apache Commons SCXML 提供了一个轻量级、可嵌入的状态机引擎,可以在应用程序运行时轻松配置和自定义。
这里的关键点是,您必须以在实体的状态转换之间的方式定义 scxml - 您可以使用“操作”或“侦听器”指定要执行的工作流程。
示例程序:
https://www.javacodegeeks.com/2012/ 06/apache-commons-scxml-finite-state.html
有关 Commons SCXML 2.0 的更多信息
https://events .linuxfoundation.org/sites/events/files/slides/ApacheConUS2014%20-%20Apache%20Commons%20SCXML%202.0.pdf
I think we need to understand couple of things here.
State Machines - Will focus on representing a particular state your entity is in.
WorkFlow - Will define the processes you are following for moving your entity from initial state to final state.
In your case as your overall process is revolving around a single entity and its current status at any given point of time is singleton, I would recommend to go for a state machine.
Recommendation:
Apache Commons SCXML provides a lightweight, embeddable state machine engine which can easily be configured and customized at runtime within your application.
Key point here is you have to define your scxml in such a way that between the state transitions of your entity - You can specify the workflows you want to perform using 'actions' or 'listeners'.
SAMPLE PROGROM:
https://www.javacodegeeks.com/2012/06/apache-commons-scxml-finite-state.html
MORE ABOUT Commons SCXML 2.0
https://events.linuxfoundation.org/sites/events/files/slides/ApacheConUS2014%20-%20Apache%20Commons%20SCXML%202.0.pdf