如何实现模块间通信[DNN 3.2.2]

/*仅个人理解 F8*/


摘要

介绍在DNN 3.2.2中,如何利用IMC实现模块间的通信,并粗略说明了IMC的实现原理。请点击标题查看全文。

 

1.       通信示例

现在假设页面中,点击link模块中的btnSend按钮后,需要将用户输入的text信息发送到blog模块中进行处理,即blog模块需要接收到用户输入的text信息。这里就涉及到link模块和blog模块间的通信问题。

 

具体实现,如下:

link模块(事件引发者)

1.       声明Communications 的命名空间

Imports DotNetNuke.Entities.Modules.Communications

 

2.       需要指定IModuleCommunicator接口

    Public MustInherit Class Links

        Inherits Entities.Modules.PortalModuleBase

        Implements IModuleCommunicator

        Implements Entities.Modules.IActionable

 

3.       定义事件

Public Event ModuleCommunication(ByVal sender As Object, ByVal e As ModuleCommunicationEventArgs) Implements IModuleCommunicator.ModuleCommunication

 

4.       btnSend按钮事件中发送ModuleCommunication事件

 1 None.gif Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
 2 None.gif            Try
 3 None.gif                 ' To raise the event for the intermodule communication you need this code
 4 None.gif                  ' Event arguments to send to listeners
 5 None.gif                 Dim args As New ModuleCommunicationEventArgs
 6 None.gif                 ' Event properties (optional). Use whatever properties you need.
 7 None.gif                  ' Property Text: Some text you want to send
 8 None.gif                 args.Text  =   " Some text "
 9 None.gif                 ' Property Sender: Sender ' s Identification. Can be the name of the module starting the communication
10 None.gif                 ' Can be used to filter events originating from a specific module
11 None.gif                 args.Sender  =   " Link "
12 None.gif                 ' Property Target: Target ' s Identification. Can be the name of the module to whom  this   event   is  sent
13 None.gif                 ' Can be used to filter events directed to a specific module
14 None.gif                 args.Target  =   " BLOG "
15 None.gif                 ' Property Value: Any object you want to send to the listeners. You can put whatever object you need in this variable
16 None.gif                 args.Value  =   " A String Object "
17 None.gif                 ' Property Type: A type specification. Can be used to filter event senders
18 None.gif                 args.Type  =  Me.GetType.ToString()
19 None.gif                 ' Raise the event (use the same name as defined above)
20 None.gif                 RaiseEvent ModuleCommunication(Me, args)
21 None.gif            Catch exc As Exception
22 None.gif                ProcessModuleLoadException(Me, exc)
23 None.gif            End Try
24 None.gif        End Sub
25 None.gif


blog模块(事件监听者)

 

1.       声明Communications 的命名空间

Imports DotNetNuke.Entities.Modules.Communications

 

2.       需要指定IModuleListener接口

    Public MustInherit Class BLOG

        Inherits Entities.Modules.PortalModuleBase

        Implements IModuleListener

         Implements Entities.Modules.IActionable

         Implements Entities.Modules.IPortable

        Implements Entities.Modules.ISearchable

3.     实现事件处理程序

 

        Public Sub OnModuleCommunication(ByVal s As Object, ByVal e As ModuleCommunicationEventArgs) Implements IModuleListener.OnModuleCommunication

            Me.txtListen.Text = e.Text.ToString()

        End Sub


2.原理

IMC的实现在ModuleCommunication.vb文件中。

IMC存在两个list,一个存放所有事件引发者ModuleCommunicators,一个存放所有事件监听者ModuleListeners

 

每个模块在加载时,都会调用LoadCommunicator()函数,检查该模块是否实现了IModuleCommunicator接口或者是IModuleListener接口。如果实现了,便加入到上面提到的对应的list中。

 1 None.gif         Public Sub LoadCommunicator(ByVal ctrl As System.Web.UI.Control)
 2 None.gif            System.Diagnostics.Debug.WriteLine( " LoadCommunicator " )
 3 None.gif             '  Check and see if the module implements IModuleCommunicator 
 4 None.gif             If TypeOf ctrl Is IModuleCommunicator Then
 5 None.gif                Me.Add(CType(ctrl, IModuleCommunicator))
 6 None.gif            End If
 7 None.gif
 8 None.gif             '  Check and see if the module implements IModuleListener 
 9 None.gif             If TypeOf ctrl Is IModuleListener Then
10 None.gif                Me.Add(CType(ctrl, IModuleListener))
11 None.gif            End If
12 None.gif
13 None.gif        End Sub


并利用AddHandler将事件与事件处理程序相关联。

For i = 0 To _ModuleListeners.Count - 1

                   AddHandler item.ModuleCommunication, AddressOf _ModuleListeners(i).OnModuleCommunication

Next i

由于IMC是广播式发布事件和事件监听。所以对于某个事件是否在该事件处理程序中进行处理,需要在事件处理程序中自行根据参数进行鉴别。

 

参考:

参考了国外一个url中的介绍,但后来找的时候,一直找不到URL了。


posted on 2006-03-19 15:13 半空 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zhangwenbo/archive/2006/03/19/DnnIMC.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值