Inter Module Communication (IMC) in DotNetNuke (DNN) allows different modules within a DNN portal to share data and communicate with each other, enabling more dynamic and integrated functionalities across the site. As the name suggests, if you want to communicate or send data from one module to another, IMC is a method to achieve this.
Inter Module Communication (IMC) was introduced in DotNetNuke with the 2.0 release, designed to invoke events and transfer simple data between modules within your portals. The implementation supports defining senders, targets, message types, and data.
There are several methods to share information between different sections of the same page in an ASP.NET application and same applicable to DotNetNuke as DNN already built on Microsoft ASP.NET framework. Here are some common approaches:
ViewState
ViewState stores data on the client side within a hidden field, which is transmitted back to the server during subsequent post backs. This technique is effective for preserving state across post backs.
ViewState["Key"] = "SomeValue";
var value = ViewState["Key"].ToString();```
Session State
Session State allows you to store user-specific data that can be accessed across multiple pages and postbacks during a user's session.
Session["Key"] = "SomeValue";
var value = Session["Key"].ToString();
Query Strings
You can transfer data between sections or different pages using query strings in the URL. This method is useful for small amounts of data and for sharing information between different pages.
Response.Redirect("AnotherPage.aspx?key=SomeValue");
var value = Request.QueryString["key"];
Hidden Fields
Hidden fields enable you to store data within a form, which can be accessed during postbacks. This provides a simple method to maintain state across postbacks without storing data on the server.
<asp:HiddenField ID="HiddenField1" runat="server" Value="SomeValue" />
var value = HiddenField1.Value;
Cookies
Cookies can be used to store small amounts of data on the client side, which can then be accessed on subsequent requests.
Response.Cookies["Key"].Value = "SomeValue";
var value = Request.Cookies["Key"].Value;
Application State
Application State is used to store data that is shared across all users and sessions in an application. This can be useful for global information that does not change often.
Application["Key"] = "SomeValue";
var value = Application["Key"].ToString();
Control Properties
If you need to share data between controls on the same page, you can use properties of the controls. For instance, a value entered in a `TextBox` can be accessed by other controls on the same page.
var value = TextBox1.Text;
Custom Events
You can define custom events in your code-behind and raise them to share data between different controls or sections on the same page.
public event EventHandler MyCustomEvent;
protected void RaiseCustomEvent()
{
if (MyCustomEvent != null)
{
MyCustomEvent(this, EventArgs.Empty);
}
}
JavaScript and AJAX
JavaScript can be used to dynamically share data between sections of a page without requiring a postback. AJAX can be used to asynchronously exchange data with the server and update parts of the page.
document.getElementById('controlID').value = 'SomeValue';
Context Items
ASP.NET provides a context for each request, which allows you to store and retrieve data during the processing of a single request.
HttpContext.Current.Items["Key"] = "SomeValue";
var value = HttpContext.Current.Items["Key"].ToString();
Each of these methods has its own use cases and limitations. The choice of method depends on factors like the scope of the data (per-user, per-session, per-application), the size of the data, and security considerations.
DotNetNuke IMC (Inter Module Communication)
The basic exercise demonstration involves taking input from a textbox in Module A and displaying it in a label in Module B.
1. To enable this communication, you will use the `IModuleCommunicator` and `IModuleListener` interfaces. Implement `IModuleCommunicator` in the class of the module you want to send data from, and implement `IModuleListener` in the class of the module where you want to receive the data.
2. Multiple listeners can be supported. In this case, each listener will act as a receiver for the data sent by the communicator module.
3. Use the `ModuleCommunicationEventArgs` class to send data from the communicator module class to the listener module class.
4. The namespace utilized for this process is `DotNetNuke.Entities.Modules.Communications`.
Event-Based Communication
IMC in DNN typically relies on event-based communication, where modules can publish and subscribe to events. This is facilitated through the DotNetNuke.Entities.Modules.Communications namespace, which provides the necessary interfaces and classes to implement IMC.
- IActionable Interface: This interface is used to define actions that a module can perform. These actions can be communicated to other modules, allowing for interactivity and data sharing.
- IEventListener Interface: This interface allows a module to listen for specific events that might be published by other modules. By implementing this interface, a module can respond to events and take appropriate actions.
Module Actions
Modules can define actions that other modules can interact with. These actions are usually specified in the form of commands that can trigger specific functionalities within the module. The IActionable interface helps in defining these actions.
Data Sharing
For data sharing between modules, the most common approach is to use a shared database or in-memory data structures. Modules can expose certain data through public properties or methods, which other modules can access. This can be done via direct references if the modules are aware of each other, or through more decoupled methods like web services or API endpoints within the DNN framework.
Example Workflow
- Publisher Module: A module that publishes data or events implements the necessary interfaces and methods to notify other modules of changes or actions. For example, a calendar module might publish an event when a new appointment is added.
- Subscriber Module: Another module that is interested in the data or events from the publisher subscribes to these events. This module implements the IEventListener interface to handle the incoming events. For instance, a notification module might display a message when a new appointment is added to the calendar.
Implementation Example
Suppose you have a calendar module that publishes events when appointments are added, and a notification module that listens to these events:
- Calendar Module:
public class CalendarModule : PortalModuleBase, IActionable
{
public event EventHandler<AppointmentEventArgs> AppointmentAdded;
protected void OnAppointmentAdded(AppointmentEventArgs e)
{
if (AppointmentAdded != null)
AppointmentAdded(this, e);
}
// Method to add appointment
public void AddAppointment(Appointment appointment)
{
// Add appointment logic
OnAppointmentAdded(new AppointmentEventArgs(appointment));
}
}
2. Notification Module:
public class NotificationModule : PortalModuleBase, IEventListener
{
public void OnEvent(object sender, EventArgs e)
{
if (e is AppointmentEventArgs)
{
var appointment = ((AppointmentEventArgs)e).Appointment;
// Display notification logic
}
}
}
In this example, the CalendarModule publishes an event when an appointment is added, and the NotificationModule listens for this event to display a notification.
By leveraging IMC, DNN modules can achieve a higher level of interaction and integration, providing a seamless user experience across different functionalities within a DNN portal.