Mark Rendle’s Simple.Data has quickly become a popular choice to retrieve data from various data sources. The number of Simple.Data providers includes today SQL Server, Oracle, Mysql, MongoDB and others. When I saw Simple.Data in action, I became enthusiastic about extending its list of providers with OData. I believe dynamic nature of Simple.Data matches very well RESTful nature of OData. Currently Visual Studio offers creation of a client context from an OData feed using “Add Service Reference” dialog which treats an OData service like it was a traditional SOAP service. This is unfortunate because OData communication does not need an interface proxy: communication is based on HTTP methods and can be implemented without context creation ceremony.
Welcome to Simple.Data OData provider! When I asked Mark Rendle on Twitter if he had thought about writing a Simple.Data OData provider, he replied that he was working on Windows Azure table provider that also uses OData protocol, and he offered me to join the project and focus on generic OData client implementation. This gave me an exciting opportunity, and today I am glad to announce that Simple.Data OData provider preview is available, and it’s Nuget feed is published.
Simple.Data OData provider is hosted at GitHub. The source code based is shared by OData and Azure provider implementation, but the Nuget packages are different.
Getting started with Simple.Data OData provider
The easiest way to start using Simple.Data OData provider is to install it’s Nuget package. In Visual Studio open Package Manager console and type the following:
You will see output similar to this:
Attempting to resolve dependency 'Simple.Data.Core (≥ 0.12.2.2)'. Successfully installed 'Simple.Data.OData 0.1.1'. Successfully added 'Simple.Data.OData 0.1.1' to SimpleODataTest.
In the source file where you will be using OData provider import namespaces:
using Simple.Data; using Simple.Data.OData;
Create an instance of dynamic database object that will be used to communication with OData feed:
dynamic db = Database.Opener.Open("http://packages.nuget.org/v1/FeedService.svc/");
Now you can start retrieving data from the OData server using Simple.Data syntax:
var package = db.Packages.FindByTitle("Simple.Data.OData"); Console.WriteLine(package.Title);
Reading data using Simple.Data OData provider
package = db.Packages.FindByTitleAndVersion("Simple.Data.OData", "0.1.1.2"); Console.WriteLine(package.Title);
Find multiple results:
IEnumerable<dynamic> packages = db.Packages.FindAllByTitle("Simple.Data.OData"); packages.ToList().ForEach(x => Console.WriteLine(x.Title + " " + x.Version)); packages = db.Packages.FindAll(db.Packages.Version == "126.96.36.199"); packages.ToList().ForEach(x => Console.WriteLine(x.Title + " " + x.Version));
Select column subset:
IEnumerable<dynamic> packages = db.Packages .FindAllByTitle("Simple.Data.OData") .Select(db.Packages.Version);
Limit number of results using Skip and Take:
IEnumerable<dynamic> packages = packages = db.Packages .FindAllByTitle("Simple.Data.OData") .Take(1); packages = db.Packages .FindAllByTitle("Simple.Data.OData") .Skip(1) .Take(1);
Retrieve results in specific order:
IEnumerable<dynamic> packages = db.Packages .FindAllByTitle("Simple.Data.OData") .OrderByDescending(db.Packages.Version);
OData provider already supports Insert,Update and Delete operations (more about it in a next blog post), but it does not support associations. The implementation of the new functionality requires some changes to Simple.Data.Core library that is on its way.