The event publication and subscription in Nopcommerce is relatively canonical,
1, Iconsumer consumers
public interface iconsumer<t>
{
void Handleevent (T eventmessage);
}
2. Ieventpublisher Event Publisher
Event caller, program entry,
<summary>
Evnt Publisher
</summary>
public interface Ieventpublisher
{
<summary>
Publish Event
</summary>
<typeparam name= "T" >Type</typeparam>
<param name= "Eventmessage" >event message</param>
void publish<t> (T eventmessage);
}
3. ISubscriptionService Event Subscription Service
<summary>
Event subscription Service
</summary>
public interface ISubscriptionService
{
<summary>
Get Subscriptions
</summary>
<typeparam name= "T" >Type</typeparam>
<returns>event consumers</returns>
Ilist<iconsumer<t>> getsubscriptions<t> ();
}
Get events for all consumers
Case:
There is a requirement, user login module, after the user log in I need to save log to the database, and I need to put the user has permissions to the cache
Traditional practices
public class Userservice:iuserservice
{
Public Log (User model)
{
Verify the user, verify success
//Call the log service to log the login information to the database
Call the caching service and put the user rights in the cache
}
}
Note that the green sector is the user logged in after the operation of things, we will not consider these two operations as an event, that is, the two equivalent of the user login associated with the consumer, the use of event publishing way to handle.
After evolution:
To define user logon events:
User Consumer
public class Customereventconsumer:iconsumer<customerloggedinevent>,iconsumer<customerregisteredevent >
{
<summary>
Handling User logon events
</summary>
<param name= "Eventmessage" ></param>
public void Handleevent (Customerloggedinevent eventmessage)
{
Record log
Console.Write ("Landing success");
Log Login Logs
}
public void Handleevent (Customerregisteredevent eventmessage)
{
Record log
Console.Write ("registered success");
Update data
}
}
The following changes are followed:
public class Userservice:iuserservice
{
Public Log (User model)
{
Verify the user, verify success
Invoke Event Publishing
_eventpublisher.publish (new Customerloggedinevent (Customer));
}
}
Simply write a little bit
The Eventbus in Nopcommerce Mall system