Orchard EventBus 事件匯流排及 IEventHandler作用

來源:互聯網
上載者:User

標籤:

事件匯流排介面定義:
public interface IEventBus : IDependency {        IEnumerable Notify(string messageName, IDictionary<string, object> eventData);    }

 

messageName 參數說明 :

_eventBus.Notify(interfaceName + "." + methodName, data/*介面方法參數*/);

 

事件匯流排作用:

當調用Notify時,會將應用程式內所有interfaceName實類的方法輪流調用一遍,相當於廣播通知. 大部分情況下 Notify並不會直接被調用, 而是通過Orchard內建的AOP機制調用.

直接使用Notify的兩個例子:

 

AOP調用原理 :

因為 interfaceName 均繼承了 IEventHandler 空介面, 而在Autofac IoC 解析父介面為IEventHandler 時,將通過 Castle dynamic proxy 建立了一個 CreateInterfaceProxyWithoutTarget 代理, 並添加了一個攔截器, 該攔截器將調用轉化為通過事件匯流排調用

public void Intercept(IInvocation invocation) {            var interfaceName = invocation.Method.DeclaringType.Name;            var methodName = invocation.Method.Name;            var data = invocation.Method.GetParameters()                .Select((parameter, index) => new { parameter.Name, Value = invocation.Arguments[index] })                .ToDictionary(kv => kv.Name, kv => kv.Value);            //攔截方法調用,將其轉化為事件匯流排調用,廣播調用訊息.
            var results = _eventBus.Notify(interfaceName + "." + methodName, data);            invocation.ReturnValue = Adjust(results, invocation.Method.ReturnType);        }
 
實現步驟: 
1.自訂Autofac Module
2.在Autofac Module中自訂解析過程,將所有父介面為 IEventHandler 的服務採用Castle動態代理攔截.
3.攔截器將調用轉化為事件匯流排方式,廣播給應用程式中所有該介面的執行個體,輪流調用.

Orchard EventBus 事件匯流排及 IEventHandler作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.