4
4
using Microsoft . Azure . Documents . Client ;
5
5
using Microsoft . Azure . Documents . Linq ;
6
6
using System ;
7
- using System . Collections . Generic ;
8
7
using System . Configuration ;
9
8
using System . Linq ;
10
9
using System . Threading . Tasks ;
@@ -31,7 +30,7 @@ public static void Main(string[] args)
31
30
//Setup a single instance of DocumentClient that is reused throughout the application
32
31
using ( client = new DocumentClient ( new Uri ( endpointUrl ) , authorizationKey ) )
33
32
{
34
- RunAsync ( ) . Wait ( ) ;
33
+ DatabaseManagementAsync ( ) . Wait ( ) ;
35
34
}
36
35
}
37
36
catch ( DocumentClientException de )
@@ -51,65 +50,39 @@ public static void Main(string[] args)
51
50
}
52
51
}
53
52
54
- private static async Task RunAsync ( )
53
+ private static async Task DatabaseManagementAsync ( )
55
54
{
56
55
//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
57
59
Database database = client . CreateDatabaseQuery ( ) . Where ( db => db . Id == databaseId ) . AsEnumerable ( ) . FirstOrDefault ( ) ;
58
60
59
- //Create database
60
- //First check if a database was returned, if not then create it
61
+ //First check if a database was returned
61
62
if ( database == null )
62
63
{
64
+ //Create database
63
65
database = await client . CreateDatabaseAsync ( new Database { Id = databaseId } ) ;
64
66
Console . WriteLine ( "Created Database: id - {0} and selfLink - {1}" , database . Id , database . SelfLink ) ;
65
67
}
66
68
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 ( ) ;
69
79
foreach ( var db in databases )
70
80
{
71
81
Console . WriteLine ( db ) ;
72
82
}
73
83
74
84
//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 ) ) ;
113
86
}
114
87
}
115
88
}
0 commit comments