Overview of the Windows Workflow Foundation on the example of building an electronic document management system [Part 1]
What is WF?
Windows Workflow Foundation (WF) is no longer Microsoft's new technology designed to create and execute Workflows . However, at the moment it is not used very actively, and many developers have not heard about it at all. And my acquaintance with her happened relatively recently. While the use of WF in the implementation of certain categories of tasks can be more than justified. In this regard, I want to talk about the technology itself, its scope and consider an example of its use in the implementation of a specific project.
The key concept in WF is Activity - a class that performs a unit of work in the WF runtime. The terms Workflow and Activity are synonymous in the context of WF. Each Activity performs an action - literally program code (for example, in C #). Activities have input and output parameters, variables. An Activity can be a composition of several child Activities, in this case, in the process, the parent Activity controls the launch of its children in the runtime in accordance with its internal logic. For example, Parallel Activity from the core Activity library (included with the .NET Framework) launches children in parallel. And if workflow, as it is not difficult to guess from the name, launches one of two child elements depending on the result of checking the given condition.
Thus, in the end, the creation of a workflow usually comes down to drawing up a block diagram in the designer based on the Activities of the base library in combination with Activities of its own design. The workflow built in the designer is encoded in XAML (XML extension). The appearance of the designer is shown in the figure.

“Good, but what is it for me? - you ask. “I'd rather write a kilogram of code than dig into blocks and arrows.”
Indeed, this was not worth starting. We developers are used to writing and reading code, we do it well and love to do it. The code is more compact. In the code we can leave comments. Changes to the code are easier to track in the version control system. You can come up with another 100,500 arguments, I guess. But it turns out that WF technology has the following remarkable features.
Firstly, an algorithm expressed as a WF scheme can automatically save and restore its state. This not only frees us from work, but also opens up great opportunities for scaling our application. It is better to show it in the following picture.

At time A, on the first computer, the workflow reaches the point where input is required to continue. Further, the state is saved (the values of all variables, arguments at the specified point). Then, after a certain period of time, the required input data arrives, and at time B, the state is restored, the algorithm continues to work from the saved point on another computer. It would be great if any C # code possessed this property, wouldn't it?
Now it is customary to divide the logic into independent sections of code. In particular, in order to ensure scalability. For example, consider two methods: LogOn and GetData . So far, it is obvious that the LogOn method is called first , followed by GetData . But when there are a lot of such methods, it can be difficult for us to understand the logic (it can be “spread out” throughout the application) and the order of their execution. Using WF removes this problem: we have separate tasks that connect the thread of the general workflow, displayed in a simple to understand flowchart, so that even an inexperienced developer can quickly figure out the rules for launching.
The next completely applied feature of WF, which should not be ignored, is the ability to move software logic into the system configuration area if such flexibility is needed . That is, from the components of the standard .NET Framework delivery, you can assemble a designer as part of your software product. At the system setup stage, workflows are modified, their properties are managed (for example, launch conditions). Further, during operation at the required points, not a “hard code” is executed, but previously created Activities. Thus, WF in combination with Dynamic LINQ is a powerful tool to give the system such quality as customizability. But, of course, it cannot be said that .NET programming skills are not required at all to configure such a system.
It is important that the use of a framework is truly justified. I see a classic example of an anti-pattern of unreasonable application of technology in the current project in an application implemented by the customer on their own. It uses the WF scheme in fact as a replacement for the code in C #, which in the end also degraded greatly due to the ignorance of the technology by all the developers who made the modification. But I must say that the resulting “Frankenstein” is slowly, but nevertheless fulfills its duties 1 .
You can not use WF without using the above advantages. In this case, we exploit the disadvantages of technology, despite and, perhaps, not understanding, the merits. This often happens when learning a new technology, but as many people think, an additional line in the resume is the employer’s labor to support the inappropriate code. But, in fairness, I want to say that in this case the benefit from the knowledge and skills gained will not be very great, as it seems to me.
When our team received a task to implement a User Store of the type: “As a director, I want applications for leave of employees to be coordinated with the immediate supervisor in the system, then they will go to the human resources department to draw up the order, and the order, after my signature, the accounting department left for vacation pay to eliminate errors and delays, reduce costs ”, - it turned out that the WF technology is ideal for its implementation. In this case, all the above-described features can be used.
In the next part, we will consider a practical illustration of the application of WF in this example.
______________
1 The project uses outdated WF 3.0. Microsoft .NET Framework 4.0 is known to have completely rewritten WF in the new namespace. Performance optimization is one of the improvements.
