Sunday, July 27, 2008

ADO.NET Data Services

In Traditional web applications, you request a page and the the server, process the request, makes some database calls if needed and put your data and HTML together and pass on to the client.This model has now changed with RIA, using AJAX, Silverlight and others

Take for an example a hotel reservation page that uses AJAX for user interactions. Maybe the intital page loads with all available countries in a combo box. and when the user selects a country, all the hotels for that country is retrived through a AJAX call.

Now, this is different form the typical web model, initially by requesting this page, the server send out HTML and presentation logic (javascript in this case for AJAX) to the client. The request for the hotels for a particular country is a pure data call, where only data flows in the form of XML.

This example was just used to demonstrate the change from the traditionl web model, but you would find a lot more pure data calls then just one (like in this case) in real world web applications.

The ADO.NET Data Serveice (formally called Astoria), helps developers in this area, providing a simple but yet a powerful way to expose data centric operations.
Data services, exposes your data as a RESTful services, so that your data could be accessed as a web resource, i.e through URLs.
As RESTful services support the CRUD operations, you can use HTTP verbs like POST to insert data, PUT to update data, GET to retrive data and DELETE to delete data.

for an example, you can access all the hotels of a specific country like this,

Tthis would retrive all the hotels in Sri Lanka. The Country and Hotel in the above URL are conceptual entites in your data source of your domain.

The data from the data services can be retrived in multiple represnetation,presently the data service framework exposes data in JSON, ATOM and in POX (plain old XML).

Building a ADO.NET data services is simple. Listed below are some of the common steps when creating a ADO .NET data service.

1) Create a Entity Data Model(EDM) representing your data. The EDM was introduced part of the ADO.NET Entity Framework.
2) Create a data service class representing your entites created in the EDM.

So, Lets make this concept more concrete, lets say my database contains two table, Country and Hotel. I can create an EDM model pointing to my database.
Now, my generated model would contain 2 conceptual entities called Country and Hotel.
Next step, I create a data service (called hotelservice.svc ) based on the generated EDM model.
Now I can access all the Countries using the URL
You can see that I am actually pointing to the entity that is in my EDM model, which in turns points to the Country table.
I can access the Country with the name India like this (country name is my primary key)

I can list all the Hotels for the country India like this
and so on..

What makes the ADO.NET data services very simple is that, you dont need to write code for each and every operations you provide, think about it, you would have to write a service opeartion, write your SQL query, connect to a database, execute it, get the result set, convert it into a format your client understands and send it back to the client, but with data services, all this is done for you, only things
you might have to do when exposing data in a database would be to create an EDM model and create a data service (just a few line of code, minimum of 2 lines) and the rest is done by the framework for you.

You can expose data that does not inherantly come from databases, by exposing the data to the data service framework through the IQueryable interface (introduced with LINQ).
The point of this artical is just to introduce ADO.NET data services and not to give you an example of a working sample, you can find more about how to create data services here.

What makes data services easy to use is there capabilty to provide common functionalites like filtering,sorting, ordering of data and paging, through request parameters in the url, for an example, If I want all the 3 star hotels in Sri Lanka, I can send out a request like this,
http://www.hotelservice.svc/Country(SriLanka)/Hotels?$filter=Type eq '3star'

The other operations are as easy as this, the framework even provides a parameter called $value, that allows you to retrive only data without the surrounding XML markup.

You can also restrict the types of operations that users can do on the service, like providing user only with read only access to your data.
You can download the CTP version of the ADO.NET here...try it out.

Monday, July 21, 2008

Making Applications Offline With Microsoft Sync Framework

Computers are not just on dektops anymore, we see mobile computing theses days. With the heavy use of laptops and smart phone.
When data is on the move, there are advantages of taking your data offline, this would typically mean that your client would have a data cache locally.

Having data locally has its own advantages, for example, your data is highly available, you can work offline even without connecting to your network (maybe while your flying). This model also reduces the network congestion, as users have there own copy of the data, they do not need to access the data via a network (this would be mostly true, when your applications is a few updates to the data but requires a large number of reads).

So, then again, you can work offline but you would also want to persist your changes or use the upto date date.This is where sycronization comes in, and so does Microsoft Sync Framework.
The Microsoft Sync Framework, is a set of libraries that can be used to develop offline applications, The framework will takecare of all the syncronization that your application needs, whether it be syncronizing your changes to the central data store or syncronizing your local data cache with upto date from the central store.

Sync Framew0ork, can be used with any data source as long as you have a provider for it, the provider would be responisble for making the actual data source specific call to syncronize data.

The Sync Framework also provides implementatins for resolving conflicts within data according to defined policies.
You can extend the Sync Framework for different data sources by simply creating your own provider, the framework also has a few default provider implementations like providers for ADO.Net data sources, file providers and also a SyncFeed provider for rss/atom feeds.
MIX has a nice video on ths framework on action and you can learn more and download the Sync Framework here.

Tuesday, July 8, 2008

PEX - Automated Exploratory Program Testing

One of the prodcuts that is being reserched at Microsoft is PEX (program exploration).The team released a 0.5 version product last month and you can download it here.So what is PEX anyway, what does it try to solve?, Remeber how you wrote a method with "amazing" logic and you also wrote nice test cases to test this function, if the function is not very complex and have only a few paths it can take, you would end up with maybe a test case or two and some set of input data.But if your function is very complex and has a complex , you have to go through each and every path the program would take and generate you test data (and not to metion the test cases).
What PEX does is, it "scans" your program (the method you want to test) and comes with a set of test data for each and every path. Lets take an example.

[TestMethod] // mark this as a test case
[PexMethod] //PEX attribute
public void TestMethod1(int i, int y)
GetNumber(i, y);

//The method you want to test
private int GetNumber(int i, int y)
int j = i / y;
if (j > 0)

return 0;
return 1;

The example is explantory (I have used MS Test), but with a few exceptions that the test case is now parameterized, to take in the parameters needed by the function that is being tested. And also you can see that a [PexMethod] attribute is added on top of the test case for PEX to recognize the test case.

You can see, that PEX created data for all possible paths of the function. Running PEX on your methods would also ensure that you delt with the most unexpected inputs as well, for an example an exception that might turn out for a value that the user passed into method that you never thought about in your test cases, making your program stable (in this example a DivideByZeroException, and if we had more paths our function could have taken, PEX would generate more test cases).

PEX work with .NET 2.0 and higher, there is also a VS 2008 add-in that you can use. Currently PEX is supports MS TEST, but they are planning to write extensions for other test tools as well.

A cool thing about PEX, is that it creates code for each and every test case it creates, so that also means that you can debug the generated test cases.

Friday, July 4, 2008

ATOM and WCF Syndication

Something that got me intrested is, syndication feeds, more particularly on ATOM 1.0.This might not be new news for most of us, but for the sake of a post after a long time, I had to do this !.
Most of us might use feed readers, I use Feedreader, and for the people who dont know what this is all about, let me give you an intro.
Think of you visiting my blog, every day to see if I have posted something new, well thats hactic, but instead the feed reader can do this for you.
You simply subscribe to the Atom feed in the blog using your feed reader and your feed reader would periodically check if the blog has been updated, if so it will retrive a summary of the recently updated posts in the blog, and if you want to view the details, you can just click on a link and go there.
ATOM is the standard that these blogs expose there post summaries, so the feed reader knows exactly how to display the can read more about ATOM here..
ATOM can also be used in business applications, for an example, say my defect tracking system exposes the defects assigned to a person as an ATOM feed, I can subscribe to this feed through a feed reader and get a complete summmary of the defects that are assigned to me.OR for another example, say my HR department exposes the new joinees service as an ATOM feed, well I could just subscribe to that feed and get updated with all the new joinees of the company.
The new .NET 3.5 Web Programming Model (on that we used to create RESTFul services), particularly WCF Syndication, allows us to create ATOM feeds easily.Not only this you can expose you feed in RSS and ATOM at the same time (RSS, for your information, partially replaced RSS).
It would be great if I could give you an example on "how" easy it is...but times to go..