OData Client Code Generator

Leave a comment

The OData v4 Client Code Generator is an extension that can be used to generate OData code for use in applications.

To install it, open Visual Studio 2015 and under Visual Studio Gallery, search for OData Client Code Generator:

Click to Install:

The extension is now installed:

To use the extension, we will create a new Windows Console project:

Add a new item and under Code select OData Client:

Files are created in the project – a .tt file and a .ttinclude file:

Note the MetadataDocumentUri is empty:

Set this to an OData URI, for example:

public const string MetadataDocumentUri = "http://services.odata.org/V4/Northwind/Northwind.svc/";

Now, right click on ODataClient.tt and select Run Custom Tool:

You should see no errors. A cs file is generated, in this case ODataClient1.cs:

Now you can use the code that has been generated. We will call the code by creating an instance of the NorthwindEntities class pointing to our URI and then querying all products:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ODataConsoleApp.NorthwindModel;
 
namespace ODataConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ODataWebExperimental.Northwind.Model.NorthwindEntities nw = 
                        new ODataWebExperimental.Northwind.Model.NorthwindEntities(new Uri("http://services.odata.org/V4/Northwind/Northwind.svc/"));
            var products = nw.Products.ToList();
            foreach (var product in products)
            {
                Console.WriteLine("Product Name: {0}", product.ProductName);
            }
            Console.ReadLine();
        }
    }
}

We get the following output:

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

four × five =