98 Part II Message Endpoints
event in the code behind the OrderProcess.aspx page. You could also use this
event to publish an order addition by placing the code previously shown into
the same event handler. More appropriately, if the Pet Shop application had an
Add button on the Order page, you could respond to the Add button’s onClick
We have demonstrated the simplest form of application endpoint by integrating
to the application within its own code base. Although this is not the most ele-
gant method of integration, it is certainly acceptable in some situations and can
be a preferred method as you begin to adopt service-oriented architectures and
write applications with integration in mind. If you have the opportunity to write
integration code into the application, this method is certainly the simplest.
In the next section, we will look at how to further separate the application
code from integration code by using a technique known as multicasting.
Multicasting with Events and Delegates
One technique for integrating to applications with minimal intrusion is to use
events within the code. As mentioned in the previous section, you can have
code respond to events that occur already within the application. The Microsoft
.NET Framework provides an excellent event model for us to use for this pur-
pose. This event model is based on delegates that are really type-safe function
Of course, when you have access to the code, it is fairly simple to add to it to
accomplish what you want. If you adhere to the guideline of interfering as little
as possible with the applications you integrate to, you should achieve the same
result in a more loosely coupled manner.
One solution is to use events from the application to run the integration code.
In .NET, this means dealing with delegates. In this section, we will introduce
delegates and events, and examine their role in the EI arena. We will start by
showing an example of a delegate in its simplest form.
First, create a class named DelegateExample and add the following code
to it.
Chapter 5 Application Endpoints 99
Imports System
Namespace EISolutions.CH05
Public Class DelegateExample
Public Delegate Sub callback()
Public aDelegate As New callback(AddressOf DoJob1)
Public Sub DoJob1()
Console.WriteLine("Job1 Done!")
End Sub
End Class
End Namespace
This unassuming piece of code performs a surprising number of tasks.
The line
Public Delegate Sub callback()
declares a signature for the delegate. We will use this declaration in the next
Public aDelegate As New callback(AddressOf DoJob1)
This line creates an instance of the delegate. Note that its constructor takes the
AddressOf of the DoJob1 method as a parameter. This is the address of the func-
tion that will be called by the delegate when it is invoked.
To invoke the delegate, create a console application that includes the pre-
vious class and add the following code to the method.
Imports System
Imports System.IO
Namespace EISolutions.CH05
Module DelegateConsole
Sub Main()
Dim Example As New EISolutions.CH05.DelegateExample()
End Sub
End Module
End Namespace
100 Part II Message Endpoints
The output from this code will print “Job1 Done!” to the console. As you
can see, the first line creates an instance of the class, and the second line
invokes the function addressed by aDelegate, in this case, DoJob1. The delegate
aDelegate is simply a pointer to the function DoJob1 and behaves much like a
reference when invoked directly, as in this example. In fact, when you run the
console application, you will see the output from the DoJob1 method. This is
the simplest form of a delegate. As with much of the .NET Framework, the true
power of the delegate class becomes apparent only when you push it a little bit
The .NET Framework uses delegates for its event model. This allows more
than one handler to respond to the same event. Consider the following class.
Imports System
Imports System.IO
Namespace EISolutions.CH05
Public Class EventExample
Inherits System.Web.UI.WebControls.WebControl
Public Event Job2Complete(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Public Sub DoJob2()
Console.WriteLine("Job2 is Executing.")
RaiseEvent Job2Complete(Me, New System.EventArgs())
End Sub
End Class
End Namespace
This class sets up a public event named Job2Complete and a method
DoJob2( ) that raises the event. You can then write multiple event handlers,
which can each respond differently to the event. For example, add this code to
the Module1:
Public WithEvents Job2 As New EventExample
Public Sub EventExample_Job2Done(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Job2.Job2Complete
Console.WriteLine("Job2 is done.")
End Sub
Public Sub EventExample_Job2DoneHandler2 _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Get Enterprise Integration Solutions now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.