项目学习——后台事件监听并触发相应操作
NopCommerce项目学习——后台事件监听并触发相应操作
1、Nop.Services.Customers. CustomerService 中操作数据时,调用事件发布者泛型扩展类Nop.Services.Events.EventPublisherExtensions中的扩展方法
_eventPublisher.EntityUpdated(customer);
如
public virtual void UpdateCustomer(Customer customer)
{
if (customer == null)
throw new ArgumentNullException("customer");
_customerRepository.Update(customer);
//event notification
_eventPublisher.EntityUpdated(customer);
}
2、Nop.Services.Events.EventPublisherExtensions中的扩展方法调用EventPublisher : IEventPublisher 事件发布者及接口
public virtual void Publish<T>(T eventMessage)
{
var subscriptions = _subscriptionService.GetSubscriptions<T>();
subscriptions.ToList().ForEach(x => PublishToConsumer(x, eventMessage));
}
(1) public IList<IConsumer<T>> GetSubscriptions<T>()
{
//获取所有实现<IConsumer<T>的消费者,其中T为InsertEntity<Order>类似的具体对象
return EngineContext.Current.ResolveAll<IConsumer<T>>();
}
(2)PublishToConsumer(x, eventMessage))
x.HandleEvent(eventMessage);调用Consumer的HandleEvent方法 如Nop.Services.Catalog.Cache. PriceCacheEventConsumer