The Eventbus in Nopcommerce Mall system

Source: Internet
Author: User
Tags nopcommerce eventbus

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.