Pages

Saturday, January 14, 2012

Using third party web services in your applications

Reading a question in StackOverflow website I thought I have to write about my suggestion to Windows Phone developers regarding the way you should use third party web services in your applications:


Adding a layer of indirection is considerable
It is very common that you need to consume a web service from a third party to show some information to your users or provide some functionality for them. So the easiest way is to add a reference to their web service in your application and call methods.
In certain scenarios you have to employ this method. But I am thinking of not adding a direct dependency to a third party web services. Ans I suggest to add a layer of indirection by implementing your own web service by dispatching the request from your applications to the third party web service.
Application Vulnerability to changes in web service definition
By adding a direct dependency to a third party web service and considering the fact that the web service owners may change the web service definition, all the installed instance of your application may fail to work if such a change happens. Then you have to provide an updated version of your application and wait users to install the update. Some users may not be happy with the situation and decide to uninstall the application instead of updating.
The chance of adding your own functionality
If you directly call the third party web service you are limited to functionalities provided by that web service, for example imagine that you call web service to receive a list of addresses. Now if you want to pin them on a map, you need the latitude and longitude of the addresses which is not provided by the web service. I believe in most of the cases you will be much more productive if you have some power to do the things in your own way.
Performance and efficiency considerations
As the designers of third party web service have thought about some specific scenarios the set of methods they provided may not match your exact needs. For example you may have to make two or three calls to receive all the required data and no single method gives all the response you needed. And the result from the first calls is not enough to provide any feedback to user . Or as an another example you may only need to show a part of received data and have nothing to do with the whole given data. As can be clearly seen by this examples there could be two kind of problems with third party web services, extra round-trips and extra bandwidth consumption, which avoiding them can increase your application performance.
Logging and tracking
Generally a good method for logging and other cross cutting concerns is to have an indirection layer, which receives requests and dispatches them. And here in our topic about third party web services, if we do not sit in user's way to access to data and services we have no chance to track them and have a report of it. So create a web service and dispatch the request to third party web service and meanwhile collect some useful data like error exceptions, calls count, performance logging, caching. And do not forget to be respectful to users privacy. If your application is mostly calling the web service then you could have a nice report your application usage.

Some disadvantages
No need to say, writing your own web service has its own disadvantages. First it is costly, you have to develop, test, maintain the web service. Also you have to pay for hosting, if you do not have a spare web space. And your hosting may not be as powerful and fast as the third party web service is, so your app would not be as fast as it could be. Also there are legal issues about using third party web services, some web services specifically forbidden not using their services directly. I am not very into security, but I have a vague idea about there may be some security issues.

What do you think?
Also there may be some other advantages and disadvantages, I would like to hear about them, so do not hesitate to leave a comment.



Wednesday, January 11, 2012

Assigning null to anonymouse type properties

Just a quick note about handling an issue for anonymous types, if you wanted to assign a null value to a property you will face a compile error which says Cannot assign <null> to anonymous type property, and the easy solution for this is to write to cast the null to desired type, this is important specially if you are creating an array of anonymouse typed objects.

Here is a sample code:

return new[] { 
    new { Name = "Fixed Value", ViewModel = (objectnew B_FixedValue_EditViewModel() } ,
    new { Name = "Fixed List", ViewModel =  null as object} ,
    new { Name = "Calculation on one value", ViewModel = null as object } ,
    new { Name = "Calculation on two values", ViewModel = null as object } ,
    new { Name = "Loading a value from database", ViewModel = null as object } ,
    new { Name = "Conditional Value Provision", ViewModel = null as object} ,
};