Back to directory
I wrote an article about event subscriptionArticle(Benefits of events ~ It is closed to modifications and open to extensions !~ ), But it mainly subscribes to static events. Today, we mainly talk about instance events, that is, when an event publisher is instantiated, it subscribes to events in it, then, when the event publisher triggers the event, it executes the content you subscribe to. There is nothing to say and everything is normal.
However, in the B/S system, there is often a requirement that the Order class has a method generatororder, that is, the method for generating orders, this method is called by many methods on the UI Layer to purchase different business products. For example, after purchasing a home appliance product, go to the success page related to the home appliance; after purchasing daily necessities, you should go to the pages related to daily users. In the future, there will be successful pages for other business products that need to be implemented.
Method 1: use static events and static events, but there is a problem because it is an event for the class, so when user a subscribes to an event, when user B views it through a browser, it will also subscribe to this event. Therefore, static events cannot meet our requirements (static events are generally used for global work)
Method 2: directly write the successful pages of all products in the generatororder method and handle them separately through the switch. This method can solve this problem, but suchProgramWithout any scalability, it is not really an object-oriented program.
Method 3: Use a common event to pass the event publisher to the generatororder method in the form of parameters after product A subscribes to the event. This is the most standard and more reliable method.
CodeAs follows:
1 /// <Summary> 2 /// Event publisher 3 /// </Summary> 4 Public Partial Class Returnmsgclass 5 { 6 Partial Void Onload (); 7 Public Returnmsgclass () 8 { 9 Onload (); 10 } 11 12 # Region Events 13 /// <Summary> 14 /// Return message event [instance event] 15 /// </Summary> 16 Public Event Func < String > Returnmsg; 17 # Endregion 18 19 # Region Onevents Methods20 /// <Summary> 21 /// Trigger returnmsg event 22 /// </Summary> 23 Public String Onreturnmsg () 24 { 25 If ((This . Returnmsg! = Null )) 26 { 27 Return This . Returnmsg (); 28 } 29 Else 30 Return " No event subscription " ; 31 } 32 # Endregion 33 }
The following is the event subscriber. It should input the event Publisher Object to subscribe to the corresponding event.
1 /// <Summary> 2 /// Event subscriber 3 /// </Summary> 4 Public Class Subscriber 5 { 6 Public Subscriber (returnmsgclass, String URL) 7 { 8 Returnmsgclass. returnmsg + = Delegate (){ Return URL ;}; 9 } 10 }
For A-type products, after the purchase is successful, then execute some logic
1 Public Partial Class Homecontroller 2 { 3 Public Actionresult test () 4 { 5 Returnmsgclass Rm = New Returnmsgclass (); 6 Subscriber sub = New Subscriber (RM, " Http://www.sina.com " );// If the subscription is successful, return the address 7 Return View ( " Index " , RM ); 8 } 9 }
On the purchase success page, there will be such a redirection logic
1 @{2Response. Redirect (ModelAsTew.v3.controllers. returnmsgclass ??NewTew.v3.controllers. returnmsgclass (). onreturnmsg ());3}
Finally, the effect is that when a user accesses the test page, the user subscribes to the event and automatically jumps to the specified page.
Back to directory