Recently, I was working on a project and encountered a piece of WWF code written by a programmer named aosan. the ruleset, rulevalidation, and ruleexecution methods are used. These three methods make me very depressed. first, I can understand these methods, that is, deserializing the generated rules into a ruleset object through the deserializeruleset method, and then declaring the object to be operated (objclass) using the rulevalidation object ), then let the ruleset object be executed directly based on the previously written object .... I removed the try code.
The catch and if statements are as follows.
- Private ruleset deserializeruleset (string rulesetxmldefinition)
- {
- Ruleset result = NULL;
- System. workflow. Activities. Rules. Rule rule = new system. workflow. Activities. Rules. Rule ();
- If (! String. isnullorempty (rulesetxmldefinition ))
- {
- Xmlreader reader = xmlreader. Create (New stringreader (rulesetxmldefinition ));
- // Reader = xmltextreader. Create (@ "D:/user. Rules ");
- Result = serializer. deserialize (Reader) as ruleset;
- }
- Return result;
- }
- Public void executeruleset (Object objclass, string xmlstring)
- {
- Rulevalidation = new rulevalidation (objclass. GetType (), null );
- Ruleexecution = new ruleexecution (rulevalidation, objclass );
- Ruleset = deserializeruleset (xmlstring );
- Ruleset. Execute (ruleexecution );
- }
I did not understand how ruleset was created, so I consulted A3. the result was "we wrote a tool to generate rules. You only need to use it ". i'm so upset about this. By the way, BS.
So I decided to study this and read the format of the Rules stored in the source database. It should be generated by the ruleset editor. however, sadly, I created a WWF project and read out the rules file (ruleset Editor) generated by the policy as a parameter of executeruleset. When the result is deserialized, failed. the reason is that the method for creating rules is incorrect. so I wandered online for nearly two weeks. After several failures, I finally found the method for generating ruleset. after countless nights of meditation and Internet searches, I finally found a solution.
To generate this ruleset rule, I specially created a Windows project. the tool used to generate the ruleset engine of "A San" is probably such a thing. It is nothing more than that it can serialize the rules and add the result to the database. here is not what I want to do. I want to find out how he generates this ruleset.
- Private void form1_load (Object sender, eventargs E)
- {
- Type entitytype = typeof (users );
- Ruleset = new ruleset ();
- Rulesetdialog dialog = new rulesetdialog (entitytype, null, ruleset );
- Dialogresult result = dialog. showdialog (this );
- If (result = dialogresult. OK)
- {
- Serializeruleset (dialog. ruleset );
- }
- }
- Private void serializeruleset (ruleset)
- {
- Stringbuilder rulesetxml = new stringbuilder ();
- Using (xmlwriter = xmlwriter. Create (rulesetxml ))
- {
- New workflowmarkupserializer (). serialize (xmlwriter, ruleset );
- }
- Xmldocument xmldoc = new xmldocument ();
- Xmldoc. loadxml (rulesetxml. tostring ());
- Xmldoc. Save ("user. Rules ");
- }
Users is the object to be verified. Here I create a users class, which contains several Members, age, name. this is actually not important. Here I will mainly talk about how to use this ruleset engine. When using rulesetdialog, A dialog box will pop up, which is the same as when you edit the policy or workflow in a graphical way. you need to add rules. after adding. the returned result is returned. The dialog ruleset method is taken out and serialized. save as user. rules, of course, what format is saved. it doesn't matter where it is saved. it is important that this rule has been generated.
Finally, return to the beginning and substitute the users class defined by myself and the generated ruels object into executeruleset (). The verification is successful ....
As for how to add rules, it does not fall into the scope discussed here. There are many online WWF tutorials.
My reference: http://melgrubb.spaces.live.com/Blog/cns! A44bb98a805c8996! 182. Entry
My environment is the namespace used by code above Visual Studio 2008 perfes1_framework3.5 SP1.
- Using system. workflow. Activities. Rules;
- Using system. workflow. componentmodel. compiler;
- Using system. workflow. componentmodel. serialization;
- Using system. workflow. Activities. Rules. design;
- Using system. xml;