What to Read this Week?

Integrating Cosmos DB with OData (Part 1)

Azure Cosmos DB is great, but what if you need expose it as OData standard, including the query in URI convention? Well, you integrate it with OData. Hassan covers the basic of Cosmos DB, what is it and how to set it up, and how to integrate it with OData.

40 Visual Studio Code Plugins I Have

One can never has enough Visual Studio Code plugins. Here’s another 40 for you.

Open Neural Network Exchange (ONNX)

Machine learning is on the rise and everyone develops their own standard. ONNX (read `onix`) is an open standard model for machine learning. This explains what is it in more details as well as where to get the pre-build ONNX models (model zoo).

Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)

CSDojo explains Big O Notation in a simple, easy to understand way and how time and space complexity are calculated. There are some maths involved, but they are pretty basic.

C# Data Structures

And along with Big O Notation, these articles go over the data structures that are available in C#. Learning and knowing when to use these data structures are important in building the fast algorithm.
Advertisements

ASP.NET Web API and OData’s Verbose JSON

OData v3 and Verbose JSON

Whenever you work with OData in ASP.NET Web API, the return result will be JSON format. It’s noteworthy to know that by default Web API will return new OData JSON format specified in OData version 3. For older OData, the JSON format is different. It has more ‘unnecessary’ placeholders. This old OData JSON is also referred as ‘Verbose JSON’. In short, there are two types of OData JSON:

  • OData v3 has new JSON format
  • Verbose JSON is used in older OData versions.

ASP.NET Web API (.Net 4.5) and Verbose JSON

As mentioned before, by default Web API will return new OData JSON.

To change this behavior, you can request Web API to return old OData JSON format by specifying ‘odata=verbose’ in Accept header. Below is the sample of jQuery Ajax call specifying verbose JSON:

$.ajax({
    url: "/api/Products",
    dataType: "json",
    accepts: { json: "application/json;odata=verbose" }
}).done(function() {
    // do work ...
});

How it Looks Like?

For the curious:

Verbose JSON

{
    "d" : {
        "results": [
            {
                "__metadata": {
                    "uri": "http://demos/Orders(10248)",
                    "type": "SampleModel.Order"
                },
                "ProductId": 10248,
                "Name": "Vgnon"
            }
        ],
        "__count": "1"
    }
}

OData v3 (and above) JSON

{
    "odata.metadata":"http://localhost:43111/api/$metadata#Products",
    "odata.count":"1",
    "value":
    [
        {
            "ProductId":2,
            "CreatedDate":"2013-06-10T23:53:26",
            "CreatedBy":0,"Name":"Camera"
        }
    ]
}

The Complex Type Refers to Entity Type Through the Property

This applies to ASP.NET Web API under MVC 4 and .Net 4.5

Similar to Type Deriving From error, this error message comes from ASP.NET Web API when I implemented using ODataConventionModelBuilder.

the-complex-type-refers-to-entity-type-through-the-property

GetEdmModel() is my ODdataConventionModelBuilder and what causing this is that I didn’t expose EntitySet that my complex type refers to, Account refers to User.

To get around this, expose EntitySet in ODataConventionModelBuilder.

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

// Exposed enitites to Odata
modelBuilder.EntitySet<Account>("Accounts");
modelBuilder.EntitySet<User>("Users");

There issue tracked on AspNetWebStack CodePlex

Cannot Define Keys on Type Deriving From. Only Root Type in Entity Inheritance Hierarchy Can Contain Keys

This applies to ASP.NET Web API under MVC 4 and .Net 4.5

This error message is from ASP.NET Web API when I implemented using ODataConventionModelBuilder.

cannot-define-keys-on-type-deriving-from-only-root-type-in-entity-inheritance-hierarchy-can-contain-keys

GetEdmModel() is my ODdataConventionModelBuilder and what causing this is that I didn’t specify .DerivesFrom() on the type that inherit from others. In my case, Driver inherits from User.

To get around this, I have to include .DerivesFrom() in ODataConventionModelBuilder.

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();

// Derived class must be specified.
modelBuilder.Entity<Driver>().DerivesFrom<User>();
modelBuilder.Entity<Customer>().DerivesFrom<User>();

// Exposed enitites to Odata
modelBuilder.EntitySet<User>("Users");

More on OData in Web API alpha release, “ODataConventionModelBuilder and inheritance”

OData System Query Options

OData system queries are:

Source: OData