If you recall from my previous post Sync’ing OData to Local Storage in Windows Phone (Part 1), I had written a T4 template for my Windows Phone 7.5 (“Mango”) project to generate a proxy client needed to access both an OData service and local database on the device. My template was based on an existing T4 template,which was published in a blog post by Alexey Zakharov on Silverlight Show, that generated a generic OData proxy client.  I had promised to publish my first stab a T4 template to generate this hybrid proxy. However, because my original template was based on Alexey’s OSS sample, it was taking a long time to get the go ahead to post it.

A New T4 Template for OData Clients

Fortunately, the other day I heard about a new T4 template written by the OData team to generate an OData client proxy to access an OData v3 data service.

Perfect!

With this new Microsoft-developed template, I have been able to port my previous LINQ-to-SQL additions into a new template without too much work. And, I have now updated my previously published project Using Local Storage with OData on Windows Phone To Reduce Network Bandwidth to now include the actual T4 template. To use this project on your computer, follow the instructions in the main page.

Considerations for My New Hybrid T4 Template

Since I have posted this template to MSDN Samples Gallery Code under the Apache 2.0 license, I should probably mention a few caveats for your using this template:

  • This template requires the libraries that are part of WCF Data Services 1.0 (for OData v3), which you can install from the Microsoft WCF Data Services October 2011 CTP. In particular, it uses EDMLib to parse the .edmx metadata.
  • The original T4 template that I used as my starting point is a preview version that is published to Nuget.org. Since it’s a preview, I will need to port my updates into the final version, when it becomes available.
  • The original T4 template was designed to support the upcoming release of WCF Data Services 1.0, which includes new behaviors like collection properties. My template does not (yet) support collection properties because I have not yet figured out the best way to do this (I will probably have to end up serializing them to string values).
  • The original T4 template doesn’t yet include the data contract serialization attributes needed to support tombstoning on Window Phone, so I added those too in my version.
  • As before, my template supports complex type properties, but I’m not sure that it will handle nested complex types.
  • I’ve tested my template against the Netflix service (since that’s what my sample app consumes), which is the most complex public OData service that I have found. However, I haven’t tested it against a true OData v3 service.
  • You have to manually set the namespace and path variable to the generated .edmx file on your local machine (T4 doesn’t support Visual Studio macros).

Installing The Hybrid T4 Template into a New Project

In case you want to try out my T4 template in your own Windows Phone project, here’s how you would do it:

  1. Make sure that you have NuGet installed. You can install it from here: https://nuget.org/.
  2. If you haven’t already done so, use the Add Service Reference tool Visual Studio to add a reference to the OData service.
    (The template needs the service.edmx file generated by the tool).
  3. In your project, use the NuGet Package Manager Console to download and install the ODataT4-CS package:
    PM> Install-Package ODataT4-CS
  4. Remove the Reference.tt template and replace it with the ReferenceWithLocalDatabase.tt template from my sample.
  5. Open the ReferenceWithLocalDatabase.tt template file and change the value of the MetadataFilepath property in the TransformContext constructor to the location of the .edmx file generated by the service reference and update the Namespace property to a namespace that doesn’t collide with the one generated y the service reference.

Now, when you save the template file, VS should access the local .edmx file to generate a new proxy class in C#.

As I mentioned, I will post an update to my hybrid template after the final T4 template is released by the OData team.

Glenn Gailey