Last active
April 13, 2018 19:32
-
-
Save Jacob-McKay/cd44a7339a329d763728348f43917db0 to your computer and use it in GitHub Desktop.
Don't extend Document... Just don't.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Documents; | |
using Microsoft.Azure.Documents.Client; | |
using Microsoft.Azure.Documents.Linq; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
DocDbDocumentProof.Run().Wait(); | |
} | |
} | |
public class ExtendsDocument : Document | |
{ | |
[JsonProperty("something")] | |
public int Something { get; set; } | |
[JsonProperty("partitionKey")] | |
public string PartitionKey { get; set; } | |
} | |
public static class DocDbDocumentProof | |
{ | |
public static async Task Run() | |
{ | |
const string db = "<database-name>"; | |
const string collection = "<collection-name>"; | |
const string databaseUri = "<database-uri>"; | |
const string authKey = "<database-key>"; | |
var collectionUri = UriFactory.CreateDocumentCollectionUri(db, collection); | |
var client = new DocumentClient( | |
new Uri(databaseUri), | |
authKey, | |
new ConnectionPolicy | |
{ | |
ConnectionMode = ConnectionMode.Direct, | |
ConnectionProtocol = Protocol.Tcp, | |
RetryOptions = new RetryOptions | |
{ | |
MaxRetryAttemptsOnThrottledRequests = 10, | |
MaxRetryWaitTimeInSeconds = 10 | |
} | |
}); | |
var doc = new ExtendsDocument | |
{ | |
Id = "My unique Guid", | |
Something = 10, | |
PartitionKey = "My unique partition key" | |
}; | |
await client.CreateDocumentAsync(collectionUri, doc); | |
var results = client.CreateDocumentQuery<ExtendsDocument>(collectionUri) | |
.Where(x => x.PartitionKey == doc.PartitionKey && x.Id == doc.Id) | |
.AsDocumentQuery(); | |
IEnumerable<ExtendsDocument> allResults = new List<ExtendsDocument>(); | |
do | |
{ | |
allResults = allResults.Concat(await results.ExecuteNextAsync<ExtendsDocument>()); | |
} while (results.HasMoreResults); | |
var myDoc = allResults.FirstOrDefault(); | |
if (myDoc == null) return; | |
Console.WriteLine(JsonConvert.SerializeObject(myDoc)); | |
myDoc.Something = 11; | |
Console.WriteLine(JsonConvert.SerializeObject(myDoc)); | |
await client.DeleteDocumentAsync(myDoc.SelfLink, new RequestOptions | |
{ | |
PartitionKey = new PartitionKey(myDoc.PartitionKey) | |
}); // Clean up the mess | |
Console.ReadLine(); | |
// You will see in console something like | |
/* | |
{"something":10,"partitionKey":"My unique partition key","id":"My unique Guid","_rid":"...","_self":"...","_ts":0,"_etag":"...","something":10,"partitionKey":"My unique partition key"} | |
{"something":11,"partitionKey":"My unique partition key","id":"My unique Guid","_rid":"...","_self":"...","_ts":0,"_etag":"...","something":10,"partitionKey":"My unique partition key"} | |
*/ | |
// You may notice that the serialization of the object duplicates the 'something' and 'companyId' properties | |
// And that the 2nd line changes the something to 11 successfully, but the 2nd occurance of it has the orignal 10 in it | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment