Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should my Azure DocumentDB document classes inherit from Microsoft.Azure.Documents.Document?

I'm seeing some weird behavior saving to DocumentDB. I started out saving documents using a plain old class that looked like this:

public class Person
{
    public string Name;
    public int Age;
}

I saved these documents like this:

var person = new Person { ... };
client.CreateDocumentAsync(myCollectionLink, person);

This worked fine. Properties were saved with exactly the names in the class. Then I realized I needed the document's SelfLink in order to perform updates and deletes. "Ah," I thought. "I'll just derive from Document, like so:

public class Person: Microsoft.Azure.Documents.Document
{
    public string Name;
    public int Age;
}

However, much to my surprise, when I made this change, new documents were created completely blank, except for the "id" property assigned by DocumentDB itself.

I double-checked multiple times. Deriving from Document prevents my custom properties in the document from being saved...

...unless I explicitly decorate each one with [JsonProperty], like so:

public class Person: Document
{
    [JsonProperty(PropertyName="name")]
    public string Name;

    [JsonProperty(PropertyName="age")]
    public int Age;
}

Then it works again (using, of course, the new more JSON-appropriate camelCase property names). And, upon retrieval, the objects get populated with the SelfLink property that I need for updates and deletes. All good.

By my questions are...Why did this happen? Am I doing something wrong by deriving from Document? Your feedback would be much appreciated.

like image 299
Brian Rak Avatar asked May 07 '15 03:05

Brian Rak


People also ask

Can Azure functions access DocumentDB?

There are two ways you can connect to documentDB from an Azure function. DocumentClient documentClient = new DocumentClient( "SERVICE_ENDPOINT", "MASTER_KEY", ConnectionPolicy. GetDefault(), ConsistencyLevel. Session);

Is Cosmosdb a document DB?

Azure Cosmos DB is Microsoft's globally-distributed, multi-model database service "for managing data at planet-scale." It builds upon and extends the earlier Azure DocumentDB, which was released in 2014. It is schema-less and generally classified as a NoSQL database.

What is Azure document?

Azure DocumentDB is a schema-free NoSQL document database service that delivers faster reads and writes time by indexing all documents without the need for schema and provides on demand database scalability.


1 Answers

This behavior is attributed to how JSON.NET deals with properties on dynamic objects. It effectively ignores them unless you decorate them with the JsonProperty attribute.

You can either work with plain POCO or you can extend from Resource (shown below), which is a static object that Document itself extends.

public class Person: Microsoft.Azure.Documents.Resource
{
    public string Name;
    public int Age;
}
like image 154
Ryan CrawCour Avatar answered Oct 19 '22 08:10

Ryan CrawCour