Skip to content

Commit 0314fd6

Browse files
author
ryancrawcour
committed
simplified & updated database samples
1 parent 382df88 commit 0314fd6

File tree

1 file changed

+18
-45
lines changed

1 file changed

+18
-45
lines changed

samples/code-samples/DatabaseManagement/Program.cs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Azure.Documents.Client;
55
using Microsoft.Azure.Documents.Linq;
66
using System;
7-
using System.Collections.Generic;
87
using System.Configuration;
98
using System.Linq;
109
using System.Threading.Tasks;
@@ -31,7 +30,7 @@ public static void Main(string[] args)
3130
//Setup a single instance of DocumentClient that is reused throughout the application
3231
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
3332
{
34-
RunAsync().Wait();
33+
DatabaseManagementAsync().Wait();
3534
}
3635
}
3736
catch (DocumentClientException de)
@@ -51,65 +50,39 @@ public static void Main(string[] args)
5150
}
5251
}
5352

54-
private static async Task RunAsync()
53+
private static async Task DatabaseManagementAsync()
5554
{
5655
//Try to get a database
56+
//Note: we are using query here instead of ReadDatabaseAsync because we're checking if something exists
57+
// the ReadDatabaseAsync method expects the resource to be there, if its not we will get an error
58+
// instead of an empty
5759
Database database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).AsEnumerable().FirstOrDefault();
5860

59-
//Create database
60-
//First check if a database was returned, if not then create it
61+
//First check if a database was returned
6162
if (database==null)
6263
{
64+
//Create database
6365
database = await client.CreateDatabaseAsync(new Database { Id = databaseId });
6466
Console.WriteLine("Created Database: id - {0} and selfLink - {1}", database.Id, database.SelfLink);
6567
}
6668

67-
//List databases for an account
68-
var databases = await ListDatabasesAsync();
69+
//Get a single database
70+
//Note: that we don't need to use the SelfLink of a Database anymore
71+
// the links for a resource are now comprised of their ID properties
72+
// using UriFactory will give you the correct URI for a resource
73+
//
74+
// SelfLink will still work if you're already using this
75+
database = await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
76+
77+
//List all databases for an account
78+
var databases = await client.ReadDatabaseFeedAsync();
6979
foreach (var db in databases)
7080
{
7181
Console.WriteLine(db);
7282
}
7383

7484
//Delete a database
75-
//Cleanup using the SelfLink property of the Database which we either retrieved or created
76-
//If you do not have this SelfLink property accessible and populated you would need to get the Database using the id,
77-
//then read the SelfLink property from that. This SelfLink value never changes for a Database once created;
78-
//so it would be perfectly acceptable practice to cache the value or store this in your configuratiom files
79-
await client.DeleteDatabaseAsync(database.SelfLink);
80-
}
81-
82-
/// <summary>
83-
/// This method uses a ReadDatabaseFeedAsync method to read a list of all databases on the account.
84-
/// It demonstrates a pattern for how to control paging and deal with continuations
85-
/// This should not be needed for reading a list of databases as there are unlikely to be many hundred,
86-
/// but this same pattern is introduced here and can be used on other ReadFeed methods.
87-
/// </summary>
88-
/// <returns>A List of Database entities</returns>
89-
private static async Task<List<Database>> ListDatabasesAsync()
90-
{
91-
string continuation = null;
92-
List<Database> databases = new List<Database>();
93-
94-
do
95-
{
96-
FeedOptions options = new FeedOptions
97-
{
98-
RequestContinuation = continuation,
99-
MaxItemCount = 50
100-
};
101-
102-
FeedResponse<Database> response = await client.ReadDatabaseFeedAsync(options);
103-
foreach (Database db in response)
104-
{
105-
databases.Add(db);
106-
}
107-
108-
continuation = response.ResponseContinuation;
109-
}
110-
while (!String.IsNullOrEmpty(continuation));
111-
112-
return databases;
85+
await client.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseId));
11386
}
11487
}
11588
}

0 commit comments

Comments
 (0)