Today I want to show that you can use Enterprise Architect and the AMUSE – Add-In to integrate Windows Forms and existing Class Libraries and not only for embedded solutions.
In this example we are going to create a simple authentication workflow. Here you can see the final solution.
[mediaplayer src=’/wp-content/uploads/LLAMUSE_Winforms_flows.wmv’ autoLoad=1 autoPlay=1]
To download the video click here.
To download the project files click here.
First of all have a look at the 3 different Windows Forms and the class library.
I build one solution which contains 2 different projects. One project contains the Windows Forms and one contains the Authentication service.
The first is the LoginForm which provides username and password.
If the user enters the wrong username and password we want to show him an error message.
If the authentication succeeds we want to show the user the MainForm.
The Authentication Service contains a simple method which verifies the username and password and returns true or false.
Now let’s start with the interesting part.
1. Create a new Solution and call it “AmuseAuthenticationExample”.
2. Create a new Element from Type “Class” and name it “AuthenticationExample” and add a “State Machine”.
3. Your project should look now like in the following screenshot:
4. Now let’s load the external resources.
5. Right mouse-click on the diagram. “Add-Ins”, “AMUSE”,”External Reference”.
6. Now add the “AMUSEAuthenticationService.dll”. In our example for the “AuthenticationService” it is only one “Type” the “AMUSEAuthenticationService.Authentication”.
7. The import creates a class “Authentication” for us and places it on the “StateMachine” – Diagram.
Important: DON’T delete this one from the diagram if you want to use it later in your workflow.
8. Now choose the “AMUSEAuthenticationForms.dll” and add the “Type”: “AMUSEAuthenticationForms.LoginForm”, “AMUSEAuthenticationForms.MainForm” and “AMUSEAuthenticationForms.MessageForm”.
9. Your Project Browser should look like in the following screenshot:
10. We can now start to create the workflow.
11. Add a “Initial” and “Final” element to the diagram.
12. Now place our first “State” element and call it “ShowLoginForm”.
13. We add now an “Attribute” to the “AuthenticationExample” which presents the “LoginForm”.
14. Attribute Name: MyLoginForm, Type: LoginForm, Scope: Public, Initial: new LoginForm()
15. Ok connect now the “Initial”, “ShowLoginForm” and “Final” element.
16. Execute the workflow and you should get following error:
This is because we have to specify the namespaces we used in the winform solution.
17. To resolve the error we have to go to the “AuthenticationExample”–class in the “Project Explorer”, click the right mouse button and choose “Generate Code”.
18. Now we can add the namespaces at the “Import(s) / Header(s)” section and save it:
using AMUSEAuthenticationService;using AMUSEAuthenticationForms;
19. If you try now the workflow again everything should work fine.
20. We want now that the workflow shows the “LoginForm”.
Add an “Operation” to the “AuthenticationExample” class and call it “OpenLoginForm”.
21. At the “Behaviour” section add following code to the “Initial Code” to show the “LoginForm”:
22. Now we add an “Operation” to the “ShowLoginForm” state element and call it “Show”.
Name: Show,Action: entry
23. Save and add at the “Behaviour” section the “OpenLoginForm()” operation.
24. If you now start the workflow again the LoginForm should appear.
25. Ok now in the next steps we want to add the “AuthenticationService”.
26. For that we will add under the “AuthenticationExample” one “Attribute” and name it “ValidUser”: Name: ValidUser, Type: bool, Scope: Public
We also add an “Operation” called “ValidateUser”:
Name: ValidateUser, Return Type: void, Scope: Public
At the “Behaviour” section we add following code to the “Initial Code”:
ValidUser = Authentication.ValidateUser(MyLoginForm.txtUsername.Text,MyLoginForm.txtPassword.Text);
This is the part where we call the AuthenticationService. Interesting is now that we have direct access to the LoginForm!
If we wouldn’t make an own attribute for the LoginForm (MyLoginForm) we could not access this values!
Also important is that the txtUsername and txtPassword in the Windows Forms solution has to be public otherwise we also can not access this values!!
27. Now we add an operation to the “UserValidation” state element and call it “Validate”:
Name: Validate, Action: entry, Scope: Public
At the “Behaviour” section we can call the “ValidateUser” operation.
28. Now we have to show either the “MainForm” or the “MessageForm” depending on the “ValidUser”.
29. Add 2 new states elements and call them “ShowMainForm” and “ShowMessageForm”.
Connect them from the “UserValidation”.
30. Now set the “UserValidation” and “ShowMainForm” transition-“Guard” property:
“ValidUser” and for the “UserValidation” to “ShowMessageForm” – “!ValidUser”.
You should have now a diagram as in the following screenshot:
31. Now start and run the workflow and try one time the combination username: admin/password: admin and one time something different.
32. The last part is now just to show the “MainForm” and the “MessageForm”.
At the moment we don’t need any access to the forms so we can call the operations directly.
33. Add an “Operation” to the “ShowMainForm” and call it “OpenMainForm”:
Name: OpenMainForm, Action: entry, Scope: Public
At the “Behaviour” you can add now the “ShowDialog” method directly from the “MainForm” class.
34. Now we do the same for the “ShowMessageForm” but we use “OpenMessageForm” as operation name and we add “ShowDialog” from the “MessageForm” class.
Finally you should have following “StateMachine” diagram:
This example shows how simple it is to integrate external resources like external libraries or even Windows Forms to use it with the Enterprise Architect Plug-In AMUSE.
Finde das Tutorial total interessant und leicht nachzumachen.