(For more information, see the source)
Although the delegate structure is flexible, and the number and type of parameters that the method signature can adapt to are more abundant, we may use events in the project to integrate with other third-party software and systems) more opportunities. The event is a special case of delegation. The "special" has the following four aspects:
- Return Value: void
- The first parameter sender is system. object.
- The second parameter inherits from eventargs or its subclass.
- Two parameters only
Just like the two sides of a thing, event constraints are more for ourselves and for other developers, so in the Project integration process, if the definition is too complex, the delegation may become a fault between both parties. Additionally, an adapter or observer is required to solve the problem, which is easier to implement asynchronous notification integration based on events. Next we will look at a simulated monitoringProgram:
This is the requirement. We need to record the addition and modification of orders. The addition is relatively easy to say, its entry API is fixed, but there are many order fields and the business process involved is also complicated, the modified API is relatively scattered. Our monitoring program only needs to know how many new items have been added and how many modifications have been made in total. At this time, the application layer can use event monitoring as a method.
C #
Business Entity and event monitor
Using System;
Namespace Marvellousworks. practicalpattern. concept. Delegating
{
/// Business Entity Public Class Order
{
Public Void Create () {eventmonitor. Added ( This , Null );}
Public Void Changedate () {eventmonitor. Modified ( This , Null );}
Public Void Changeowner () {eventmonitor. Modified (This , Null );}
Public Void Changeid (){} // Do nothing } /// Event monitor Public Static Class Eventmonitor
{
Public Static Eventhandler <eventargs> modified;
Public Static Eventhandler <eventargs> added;
Static Eventmonitor ()
{
Modified = onmodified;
Added = onadded;
}
Public Static Int Modifiedtimes
{
Get;
Private Set;
}
Public Static Int Addedtimes
{
Get;
Private Set;
}
Static Void Onmodified ( Object Sender, eventargs ARGs) {modifiedtimes ++ ;}
Static Void Onadded ( Object Sender, eventargs ARGs) {addedtimes ++ ;}
}
}
C # unit test
[Testmethod]
Public VoidEventmonitorsimulate ()
{
Order Order1 =NewOrder ();
Order1.create ();// Add 1Order1.changedate ();// Modify 1Order1.changedate ();// Modify 2Order1.changeowner ();// Modify 3Order order2 =NewOrder ();
Order2.create ();// Add 2Order2.changeowner ();// Modify 4Order2.changeid ();// Modify still 4Assert. areequal <Int> (2, eventmonitor. addedtimes );
Assert. areequal <Int> (4, eventmonitor. modifiedtimes );
}
The unit test results show the feasibility of using event monitoring:
- The monitoring information of scattered business entity objects can be centrally reflected in the same monitor.
- The monitor can update the count by event delegation.