Thursday, February 12, 2009

Command Design Patterns


Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
The classes and/or objects participating in this pattern are:
Command (Command) :declares an interface for executing an operation
ConcreteCommand (CalculatorCommand) :defines a binding between a Receiver object and an action
implements Execute by invoking the corresponding operation(s) on Receiver
Client (CommandApp) :creates a ConcreteCommand object and sets its receiver
Invoker (User) :asks the command to carry out the request
Receiver (Calculator) :knows how to perform the operations associated with carrying out the request.
Sample Code:

#Region "Service Client Definition"
'''
''' Service Client is responsible for routing the request from UI to different services.
'''

''' Decorated with AspNetCompatibilityRequirements attribute to ALLOW the service to participate in HttpContext
_
Public Class ServiceClient
Implements IServiceClient

Public Sub New()
System.Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf TrustAllCertificatesCallback
End Sub



'''
''' This method is used to submit a command to the CommandManager from the UI.
'''

''' Name of the command that needs to executed
''' The session Id for the login
''' CommandResult which is result of the command execution
''' Used when there are no input parameters
Public Function SubmitRequest(ByVal commandName As String, ByVal sessionID As String) As CommandResult Implements IServiceClient.SubmitRequest
Return CommandManager.SubmitCommand(commandName, String.Empty, sessionID)
End Function
'''
''' This method is used to submit a command to the CommandManager from the UI.
'''

''' String: Name of the command that needs to executed
''' String: Input value for the command
''' The session Id for the login
''' CommandResult which is result of the command execution
''' Used when there are input parameters to be passed
Public Function SubmitRequestWithInput(ByVal commandName As String, ByVal commandInput As Object, ByVal sessionID As String) As CommandResult Implements IServiceClient.SubmitRequestWithInput
Return CommandManager.SubmitCommand(commandName, commandInput, sessionID)
End Function
'Public Function DelegateRequest(ByVal commandName As String, ByVal sessionID As String) As CommandResult Implements IServiceClient.DelegateRequest
' Dim CM As ContextMgrService = New ContextMgrService()
' Dim siteConfigContextValue As Object = CM.GetSessionData("SITECONFIGINFO")
' Dim siteConfigList() As AdminService.SiteConfigEntity = CType(siteConfigContextValue, AdminService.SiteConfigEntity())
' Dim cmdResult As CommandResult = New CommandResult()
' Dim remoteServiceClientURL As String = String.Empty
' Dim userID As String
' Dim password As String
' For Each siteConfig As AdminService.SiteConfigEntity In siteConfigList
' If siteConfig.linkType = "SVCCLIENT" And siteConfig.siteID = "UKIDER01" Then
' remoteServiceClientURL = siteConfig.linkURI
' userID = siteConfig.userID
' password = siteConfig.password
' Exit For
' End If
' Next
' Dim baseAddress As Uri = New Uri(remoteServiceClientURL)
' Dim bind As BasicHttpBinding = New BasicHttpBinding()
' Dim endPoint As EndpointAddress = New EndpointAddress(baseAddress)
' Dim channel As ChannelFactory(Of IServiceClient) = New ChannelFactory(Of IServiceClient)
' Dim remoteClient As IServiceClient = channel.CreateChannel(bind, endPoint)
' cmdResult = remoteClient.HandleRemoteRequest(commandName, "1", userID, password)
' Return cmdResult
'End Function
'''
''' This method is used to handle a remote request from another instance of ServiceClient
'''

''' String: Name of the command that needs to executed
''' String: Input value for the command
''' String: UserId provided to the remote client to connect
''' String: Password provided to the remote client to connect
''' CommandResult which is result of the command execution
''' Used when there are input parameters to be passed
Public Function HandleRemoteRequest(ByVal commandName As String, ByVal commandInput As String, ByVal userID As String, ByVal password As String) As CommandResult Implements IServiceClient.HandleRemoteRequest
Dim sessionID As String
Dim input As String = userID + "@" + password
Dim cmdResult As CommandResult = New CommandResult()
cmdResult = CommandManager.SubmitCommand("LoginCmd", input, String.Empty)
If cmdResult.ErrorCode = String.Empty Then
sessionID = cmdResult.OutputList(0)
Return CommandManager.SubmitCommand(commandName, commandInput, sessionID)
Else
Return cmdResult
End If
End Function


'''
''' Requried to handle any errors raising out of trust certification in SSL.
'''

'''
'''
'''
'''
'''
'''
Private Function TrustAllCertificatesCallback(ByVal sender As Object, ByVal cert As X509Certificate, ByVal chain As X509Chain, ByVal errors As SslPolicyErrors) As Boolean
Return True
End Function
End Class
#End Region
#Region "Command Manager Definiton"
'''
''' This class represent the invoker in the command design pattern.
''' Responsible for associating concrete commands with different requests.
'''

'''
Public Class CommandManager
'''
''' This method enables the Service Client class to submit the request to the Command manager.
'''

''' String: Name of the command to be executed.
''' Object: Input value to be processed
''' The session Id for the login
''' Returns the object value after executing the command
'''
Public Shared Function SubmitCommand(ByVal commandName As String, ByVal inputValue As Object, ByVal sessionID As String) As Object
Dim submitCommandResult As Object = New Object()
Dim cmd As Command
'Dim inputString As Object = inputValue.ToString()
Dim currAssembly As System.Reflection.Assembly
currAssembly = System.Reflection.Assembly.GetExecutingAssembly()
cmd = currAssembly.CreateInstance(commandName)
If (sessionID <> String.Empty) Then
Dim contextManagerService As ContextMgrService = New ContextMgrService()
Dim currentUser As Object = contextManagerService.GetCurrentUser(sessionID)
Dim loginDetail As UserService.UserEntity = CType(currentUser, UserService.UserEntity)
'cmd.LoginUserID = loginDetail.LoginID
'cmd.LoginPassword = loginDetail.Password
cmd.LoginUserID = "Site01@Admin"
cmd.LoginPassword = 1
End If
cmd.LoginUserID = "Site01@Admin"
cmd.LoginPassword = 1

submitCommandResult = ExecuteCommand(cmd, inputValue, sessionID)
Return submitCommandResult
End Function
'''
''' This method is responsible for executing the concrete command and serializing the result to be returned.
'''

''' Command: Instance of the concrete command object to be executed
''' String: Input value to be processed by the command
''' The session Id for the login
''' Object value which is the result of command execution
''' inputValue would be 'Nothing' when SubmitRequest() is used to submit the command from UI
Private Shared Function ExecuteCommand(ByVal commandObject As Command, ByVal inputValue As Object, ByVal sessionID As String) As Object
Dim executeCommandResult As CommandResult = New CommandResult()
Dim commandResultForReturn As CommandResultList = New CommandResultList()
executeCommandResult = commandObject.Execute(inputValue)
Return executeCommandResult
End Function
End Class
#End Region
#Region "Command Definitions"
'''
''' Base command class which must be inherited by all the concrete command classes.
'''

''' All distinct requests are represented by a command
Public MustInherit Class Command
Public Sub New()
loginDetail = New UserService.UserEntity()
End Sub
Public MustOverride Function Execute(ByVal input As Object) As Object
Private loginDetail As UserService.UserEntity
Public Property LoginUserID() As String
Get
Return loginDetail.LoginID
End Get
Set(ByVal value As String)
loginDetail.LoginID = value
End Set
End Property
Public Property LoginPassword() As String
Get
Return loginDetail.Password
End Get
Set(ByVal value As String)
loginDetail.Password = value
End Set
End Property
End Class






#End Region
CLASS CODE:

Public Class DeleteTeamDiaryEntryCmd
Inherits Command
Public Overrides Function Execute(ByVal input As Object) As Object
End Function
End Class
PROXY CODE:

_ServiceClientProxy = New ServiceClient.ServiceClientClient()
AddHandler _ServiceClientProxy.SubmitRequestWithInputCompleted, AddressOf Localization
_ServiceClientProxy.SubmitRequestWithInputAsync(" PPG.Leapfrog.Commands.CommonCommands.GetLocalizedControlValueCmd", "VehicleAvailabilityForecast", _SessionID)

No comments: