1. Learn from the new
Remember the login we wrote in The WF series (4) step by step.Program? If you forget it, go back and check it out.
In this section, let's improve our login program.
I have been busy looking for a job recently, so I have slowed down my post. I hope you will forgive me.
3. Topic
I have always stressed that a workflow is responsible for macro work. We should isolate it from the details of the overall design, those sides and corners.
I recently wrote a series of "review design patterns" in this series.ArticleTherefore, it is important to emphasize the importance of programming interfaces.
Well, start from now on.
4. Review Logon
Let's review the original login program. In the original login project, we mixed all the work in the workflow.
Let's see what we used to be uglyCode:
Workflow2.cs:
Private void Codeactivityappsexecutecode ( Object Sender, Eventargs E ){MessageBox . Show ( "Username and password cannot be blank" );} Private void Validate ( Object Sender, Conditionaleventargs E) {e. Result = This . Username. Equals ( "Admin" )&& This . Userpassword. Equals ( "Admin" );} Private void Codeactivity2_executecode ( Object Sender, Eventargs E ){ MessageBox . Show ( "Login successful" );} Private void Codeactivity3_executecode ( Object Sender, Eventargs E ){ MessageBox . Show ( "Incorrect user name and password" );}
I think someone may have this question at the time. Isn't that a lie to you, the Lord? What is the use of a workflow? You just get the original content that should be written in the winform background to the workflow.
Well, we have improved our ugly code from now on.
5. login Design
Let's first think about the steps we performed during login, including what we did and what the system did:
Well, we think that the workflow is responsible for a macro process, so we need to extract the specific details. What exactly is the specific details?
A. user verification process. (It may be a database connection, or a connection file verification)
B. feedback to the customer. (It may be response. Write, or MessageBox. Show)
Well, we extracted the two items and thought about what triggered the workflow. The moment we clicked the login button:
Then we should use an event to listen to our logon button event. When we click Login, the workflow will be triggered.
After the analysis is complete, let's take a look at our modeling diagram:
My Visio skills are really limited, so I will use the drawing to draw these figures. I will definitely take the time to practice Visio.
The picture is not very good. Let me explain it again:
A. Enter the user name and password, and then click log on.
B. At this time, the workflow event is triggered and the information we entered is passed into the event.
C. The workflow requests the verification interface based on the information we pass in and executes the verification code.
D. Request the corresponding response Interface Based on the returned result.
E. Finally, return the result to the user.
At the end of the analysis, we can see that there is a very important thing here, that is, the interaction between the workflow and the External. Well, before we can improve the login program, learn two important activities first.
6. handlerexternalevent
Handlerexternalevent is the first event type activity we are exposed. An event type is an activity that can be triggered through an event.
To put it short, this activity is used to call the internal method of the workflow outside the workflow, that is, the event of handlerexternalevent.
I will give you a small example here and explain it with the code.
First, let's add a handlerexternalevent activity:
Next, let's create a new class library that contains an interface to interact workflow with external programs:
NamespaceInterface {[Externaldataexchange]Public interfaceIevent{EventEventhandler<Externaldataeventargs> Myevent1 ;}}
Note the following three points for this Code:
A. externaldataexchange: mark an interface as a local service interface, which can be recognized by a workflow.
B. Interface internal event: the internal workflow binds itself to the event, and the user triggers the internal method of the workflow externally through the event.
C. externaldataeventargs: identifies the parameters passed when an event is triggered by a handlerexternaleventactivity.
Okay. Now we can bind the event to the handlerexternaleventactivity of the workflow through the Properties window:
Then we call this event externally to check the complete code of the entire project:
Workflow1.cs:
Public sealed partial classWorkflow1:Sequentialworkflowactivity{PublicWorkflow1 () {initializecomponent ();}Private voidHandleexternaleventactivityincluinvoked (ObjectSender,ExternaldataeventargsE ){MessageBox. Show ("Success");}}
Winform1.cs:
Public partial class Form1 : Form , Ievent {Private Workflowruntime Runtime; Private Externaldataexchangeservice Service; Private Workflowinstance Instance; Public Form1 () {initializecomponent (); runtime = New Workflowruntime (); Service = New Externaldataexchangeservice (); Runtime. addservice (service); service. addservice ( This ); Runtime. startruntime ();} Private void Button#click ( Object Sender, Eventargs E) {instance = runtime. createworkflow ( Typeof (Workflowconsoleapplication7. Workflow1 ); Instance. Start (); Externaldataeventargs ARGs = New Externaldataeventargs (Instance. instanceid); myevent1 ( Null , ArgS );} # Region Ievent MemberPublic event Eventhandler < Externaldataeventargs > Myevent1; # Endregion }
7. callexternalmethod
Contrary to the preceding activity, this activity calls external code through this event.
Similarly, we use code to speak:
Next, we will also create a new class library that contains an interface to interact with external programs:
The difference is that the handlerexternalevent interface is an event, and this is the method:
NamespaceCallmethod {[Externaldataexchange]Public interfaceIcallexternalmethod{VoidCallexternalmethod ();}}
This is the same as above, so I will not explain it more:
Let's look at the Code directly:
Winform2.cs:
Public partial class Form2 : Form , Icallexternalmethod { Private Workflowruntime Runtime; Private Externaldataexchangeservice Service; Private Workflowinstance Instance; Public Form2 () {initializecomponent (); runtime = New Workflowruntime (); Service = New Externaldataexchangeservice (); Runtime. addservice (service); service. addservice ( This ); Runtime. startruntime ();} # Region Icallexternalmethod Member Public void Callexternalmethod (){ MessageBox . Show ( "This method is called" );} # Endregion private void Button#click ( Object Sender, Eventargs E) {instance = runtime. createworkflow ( Typeof (Workflowconsoleapplication7. Workflow2 ); Instance. Start ();}}
8. Write it later
I originally wanted to complete the transformation of our previous login in this article, but the article is too long and you don't seem to like it very much. So let's write it separately.
We have learned how to make the workflow interact with external programs, so we all try to use this to improve the workflow?
Can you?
Coming soon: Learn the WF series step by step (8)-improve the login program (II)