In my previous post, I introduced the “Disconnected Application Block”, which is part of the Smart Client Software Factory. As I stated earlier, I think this provides a powerful way of implementing “Store and Forward” for both desktop and mobile applications.
In this post, I’ve provided a simple example example in VB of a form, service agent and dummy business component which is a bit simpler than the “Quickstarts.DisconnectedAgent” example it is based on.
The form “MainForm” has a command button for submitting requests and another command button for dispatching (running) the queued requests (depending on if the network is available or not).
Public Class MainForm Private moMyCalculator As New CalculatorServiceAgent(moManager.Instance.RequestQueue)
Private Sub cmdDispatchMessages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDispatchMessages.Click Private Sub cmdSubmitLocalRequest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmitLocalRequest.Click End Class
The Service Agent class contains the logic for queuing the request.
Private WithEvents moManager As RequestManager = DatabaseRequestManagerIntializer.Initialize("QueueDatabase")
moManager.DispatchAllPendingRequests()
End Sub
moMyCalculator.Add(1, 1)
End Sub
The code in the Add method creates a “behaviour” object to determine how the message should behaving in a queuing situation. It is then used to create a request, which is placed on the “Request Queue”.
This example differs from those provided in the QuickStart in that it uses the “ObjectProxyFactory”. This factory allows me to invoke a real object instead of a Web Service. In this case IntegerCalculatorOnlineServiceProxy.
Public Class CalculatorServiceAgent
Private moRequestQueue As IRequestQueue
Public Sub New(ByVal requestQueue As IRequestQueue) Dim behavior As New OfflineBehavior() Dim request As New Request() request.OnlineProxyType = GetType(IntegerCalculatorServiceOnlineProxy) moRequestQueue.Enqueue(request)
End Function
End Class
moRequestQueue = requestQueue
End Sub
''' <summary>
''' Enqueues a request to the <c>Add</c> web service method through the agent.
''' </summary>
''' <returns>The unique identifier associated with the request that was enqueued.</returns>
Public Function Add(ByVal a As Int32, ByVal b As Int32) As Guid
behavior.ProxyFactoryType = GetType(ObjectProxyFactory)
behavior.MaxRetries = 1
behavior.Stamps = 1
behavior.Expiration = DateTime.Now + New TimeSpan(0, 0, 30)
request.MethodName = "Add"
request.Behavior = behavior
request.CallParameters = New Object() {a, b}
request.Endpoint = "" ' Setting this to blank is important. If sent to nothing it wont work
And finally, the business logic:
''' <summary> ' This could be a call out to a 3rd party component End Function
This is nice, but what if I want my code to be informed of the result?
''' The proxy class is used to invoke the "real" business logic that does the work
''' </summary>
Public Class IntegerCalculatorServiceOnlineProxy
Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
Return a + b
End Class
This will be covered in the next post.
Pingback: Return Callbacks and the Disconnected Service Agent « An Original Idea