Thursday, September 15, 2011

F# type providers: oData from NuGet

Yesterday F# 3.0 Developer Preview  became available at the MSDN Site. It will be general available on Friday.

One of the new features is the type provider.

This little snippet shows how easy it is to query a site that provides oData. NuGet is an example of a site that provides oData.

Code Snippet
  1. #r "FSharp.Data.TypeProviders.dll"
  2. #r "System.Data.Services.Client.dll"
  3.  
  4. open Microsoft.FSharp.Data.TypeProviders
  5.  
  6. [<Generate>]
  7. type NuGet = ODataService<"http://packages.nuget.org/v1/FeedService.svc/">
  8.  
  9. let dbNuGet = NuGet.GetDataContext()
  10. let packages = dbNuGet.Packages
  11.  
  12. let nuGetQuery =
  13.     query { for package in packages do
  14.             where (package.Tags.Contains("fsharp") && package.IsLatestVersion)
  15.             select package }
  16.  
  17. let printPackage (package:NuGet.ServiceTypes.PublishedPackage) =
  18.     printfn "- - - - - - - - - - - - - - - - - - - -"
  19.     printfn "Title: %s" package.Title
  20.     printfn "Authors: %s" package.Authors
  21.     printfn "Description: %s" package.Description
  22.  
  23.     
  24. nuGetQuery |> Seq.iter printPackage

This is the result:

result

One of the nice features of type providers is intellisense for the available types.

intellisense

It is possible to create your own type provider.

More information will be available on Friday. Don Syme will present all the details at build: http://channel9.msdn.com/events/BUILD/BUILD2011/SAC-904T

update: Writing F# Type Providers with the F# 3.0 Developer Preview - An Introductory Guide and Samples

No comments:

Total Pageviews