Talking to the Back End
The last component to analyze in our sample application is the online proxy, which communicates directly with the back end Web service. The following code shows the
GetCustomers function specified in the OnlineProxyContext.
public Payload GetCustomers(Payload referencePayload)
{
ReferenceCacheDataPayload refCacheDataPayload =
referencePayload as ReferenceCacheDataPayload;
NorthwindService svc = new NorthwindService();
GetCustomersResponse response = svc.GetCustomers(
(GetCustomersRequest)refCacheDataPayload.
DataDefinition.AssociatedRequestDatas);
offlineBlockBuilderInstance.ReferenceDataCache.Store(
refCacheDataPayload.DataDefinition, response);
refCacheDataPayload.UpdateDataToReturn(response);
return refCacheDataPayload;
}
The preceding code first creates a new instance of the proxy class NorthwindService. The request message is accessed through the property
refCacheDataPayload.DataDefinition.AssociatedRequestData. The request message is then passed as a parameter to
GetCustomers, which returns the corresponding response message
GetCustomersResponse. The
Store function of the class ReferenceDataCache stores the returned reference data in the local cache storage provider. Furthermore the function
UpdateDataToReturn is called to store the result directly in the variable
refCacheDataPayload, which is then returned as a function result. Therefore the requested reference data can be used directly from the smart client.
After all the reference data is available to the smart client the application can go to offline mode if necessary.
Now take a look at the
PlaceOrder function in the class NorthwindServiceAgent, which is responsible for executing a new order against the back end Web service:
public void PlaceOrder(PlaceOrderRequest request)
{
string onlineProxyAssemblyName = "SmartClient";
string onlineProxyClassName =
"SmartClient.NorthwindOnlineProxy";
string onlineProxyMethodName = "PlaceOrder";
string specificServiceAgentMethodToInvoke =
"OrderPlacedCallback";
OnlineProxyContext onlineProxyMethodContext =
new OnlineProxyContext(
onlineProxyAssemblyName,
onlineProxyClassName,
onlineProxyMethodName);
ServiceAgentContext uploadServiceAgentContext =
new ServiceAgentContext(
specificServiceAgentMethodToInvoke);
Payload messageToEnqueue = new Payload(
onlineProxyMethodContext, this.Guid,
uploadServiceAgentContext, request);
OfflineBlockBuilder.Instance.PayloadConsumer.Enqueue(
messageToEnqueue);
}
As you can see, the code here is also an
OnlineProxyContext instance that indicates which function of the online proxy should be called. The same applies to the ServiceAgentContext instance. The code creates a new Payload class instance, which is then enqueued in the Queue Storage Provider via the
PayloadConsumer.Enqueue call. As soon as the smart client is back to the online mode the Service Request is taken from the Executor and executed against the specified online proxy.
The Occasionally Connected Future
It's essential to know how to use the Smart Client Offline Application Blocks to take full advantage of the occasionally-connected capabilities of .NET smart clients. Not only will your end users value these features highly and increase your organization's efficiency, but knowing how to deal with occasionally-connected clients is a valuable skill that you'll need in the future, as new software appears that supports this paradigm. It's also a great way:via the fiefdoms and emissaries metaphorto get functional exposure to today's service-oriented architectures.