From 678fa644834776bfdad6c0d6b302d11b53a52cde Mon Sep 17 00:00:00 2001 From: "Yaron Y. Goland" Date: Mon, 18 Dec 2017 17:30:14 -0800 Subject: [PATCH 1/3] Updating to latest DocDB SDK .gitignore - Ignore .vs files from VS 2017 DocumentDBRepository - Get rid of the two "IfNotExists" methods since they are now part of the SDK. Also moved the initialize code to an async block so it would be easier to see how the code normally should look in most programs. --- .gitignore | 1 + src/DocumentDBRepository.cs | 58 +++++++------------------------------ src/packages.config | 2 +- src/todo.csproj | 10 +++---- 4 files changed, 17 insertions(+), 54 deletions(-) diff --git a/.gitignore b/.gitignore index 2709d90..239ae5f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ ## files generated by popular Visual Studio add-ons. # User-specific files +.vs/ *.suo *.user *.sln.docstates diff --git a/src/DocumentDBRepository.cs b/src/DocumentDBRepository.cs index b5fb077..694ae9b 100644 --- a/src/DocumentDBRepository.cs +++ b/src/DocumentDBRepository.cs @@ -25,7 +25,7 @@ public static async Task GetItemAsync(string id, string category) { Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), - new RequestOptions() {PartitionKey = new PartitionKey(category)}); + new RequestOptions() { PartitionKey = new PartitionKey(category) }); return (T)(dynamic)document; } catch (DocumentClientException e) @@ -45,7 +45,7 @@ public static async Task> GetItemsAsync(Expression> { IDocumentQuery query = client.CreateDocumentQuery( UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), - new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true}) + new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true }) .Where(predicate) .AsDocumentQuery(); @@ -70,59 +70,21 @@ public static async Task UpdateItemAsync(string id, T item) public static async Task DeleteItemAsync(string id, string category) { - await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), new RequestOptions(){PartitionKey = new PartitionKey(category)}); + await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), new RequestOptions() { PartitionKey = new PartitionKey(category) }); } public static void Initialize() { - client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]); - CreateDatabaseIfNotExistsAsync().Wait(); - CreateCollectionIfNotExistsAsync().Wait(); + Init().Wait(); } - private static async Task CreateDatabaseIfNotExistsAsync() + private static async Task> Init() { - try - { - await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseId)); - } - catch (DocumentClientException e) - { - if (e.StatusCode == System.Net.HttpStatusCode.NotFound) - { - await client.CreateDatabaseAsync(new Database { Id = DatabaseId }); - } - else - { - throw; - } - } - } - - private static async Task CreateCollectionIfNotExistsAsync() - { - try - { - await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)); - } - catch (DocumentClientException e) - { - if (e.StatusCode == System.Net.HttpStatusCode.NotFound) - { - await client.CreateDocumentCollectionAsync( - UriFactory.CreateDatabaseUri(DatabaseId), - new DocumentCollection - { - Id = CollectionId, - PartitionKey = new PartitionKeyDefinition() { Paths = new Collection() { "/category" } } - }, - new RequestOptions { OfferThroughput = 1000 }); - } - else - { - throw; - } - } + client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]); + Database database = new Database { Id = DatabaseId }; + database = await client.CreateDatabaseIfNotExistsAsync(database); + DocumentCollection documentCollection = new DocumentCollection { Id = CollectionId }; + return await client.CreateDocumentCollectionIfNotExistsAsync(database.SelfLink, documentCollection); } } } \ No newline at end of file diff --git a/src/packages.config b/src/packages.config index c46df06..449fd0c 100644 --- a/src/packages.config +++ b/src/packages.config @@ -9,7 +9,7 @@ - + diff --git a/src/todo.csproj b/src/todo.csproj index 03d5192..d88f036 100644 --- a/src/todo.csproj +++ b/src/todo.csproj @@ -24,6 +24,7 @@ + true @@ -55,9 +56,8 @@ False ..\packages\EntityFramework.6.1.2-beta1\lib\net45\EntityFramework.SqlServer.dll - - ..\packages\Microsoft.Azure.DocumentDB.1.6.1\lib\net45\Microsoft.Azure.Documents.Client.dll - True + + ..\packages\Microsoft.Azure.DocumentDB.1.19.1\lib\net45\Microsoft.Azure.Documents.Client.dll @@ -222,12 +222,12 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - +