protected virtual void Dispose(bool disposing) { if (disposing && _database != null) { _database.Dispose(); _database = null; } }
private static async Task GetStartedDemo() { // Create a new instance of the DocumentClient client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey); database = await CreateDatabaseAsync(); await CreateCollectionAsync(); await SeedDocuments(); await SampleSqlQuery(); await SampleLinqQuery(); await SampleLambdaQuery(); Console.WriteLine("Press any key to delete database..."); Console.ReadKey(); await client.DeleteDatabaseAsync("dbs/" + database.Id); client.Dispose(); }
private static async Task GetStartedDemo() { // Create a new instance of the DocumentClient var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey); // Check to verify a database with the id=FamilyRegistry does not exist Database database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault(); // If the database does not exist, create a new database if (database == null) { database = await client.CreateDatabaseAsync( new Database { Id = "FamilyRegistry" }); WriteMessage("Created dbs/FamilyRegistry"); } // Check to verify a document collection with the id=FamilyCollection does not exist DocumentCollection documentCollection = client.CreateDocumentCollectionQuery("dbs/" + database.Id).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault(); // If the document collection does not exist, create a new collection if (documentCollection == null) { documentCollection = await client.CreateDocumentCollectionAsync("dbs/" + database.Id, new DocumentCollection { Id = "FamilyCollection" }); WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection"); } // Check to verify a document with the id=AndersenFamily does not exist Document document = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id).Where(d => d.Id == "AndersenFamily").AsEnumerable().FirstOrDefault(); // If the document does not exist, create a new document if (document == null) { // Create the Andersen Family document Family andersonFamily = new Family { Id = "AndersenFamily", LastName = "Andersen", Parents = new Parent[] { new Parent { FirstName = "Thomas" }, new Parent { FirstName = "Mary Kay"} }, Children = new Child[] { new Child { FirstName = "Henriette Thaulow", Gender = "female", Grade = 5, Pets = new Pet[] { new Pet { GivenName = "Fluffy" } } } }, Address = new Address { State = "WA", County = "King", City = "Seattle" }, IsRegistered = true }; // id based routing for the first argument, "dbs/FamilyRegistry/colls/FamilyCollection" await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, andersonFamily); WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection/docs/AndersenFamily"); } // Check to verify a document with the id=AndersenFamily does not exist document = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id).Where(d => d.Id == "WakefieldFamily").AsEnumerable().FirstOrDefault(); if (document == null) { // Create the WakeField document Family wakefieldFamily = new Family { Id = "WakefieldFamily", Parents = new Parent[] { new Parent { FamilyName= "Wakefield", FirstName= "Robin" }, new Parent { FamilyName= "Miller", FirstName= "Ben" } }, Children = new Child[] { new Child { FamilyName= "Merriam", FirstName= "Jesse", Gender= "female", Grade= 8, Pets= new Pet[] { new Pet { GivenName= "Goofy" }, new Pet { GivenName= "Shadow" } } }, new Child { FamilyName= "Miller", FirstName= "Lisa", Gender= "female", Grade= 1 } }, Address = new Address { State = "NY", County = "Manhattan", City = "NY" }, IsRegistered = false }; // id based routing for the first argument, "dbs/FamilyRegistry/colls/FamilyCollection" await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, wakefieldFamily); WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection/docs/WakefieldFamily"); } // Query the documents using DocumentDB SQL for the Andersen family var families = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id, "SELECT * " + "FROM Families f " + "WHERE f.id = \"AndersenFamily\""); foreach (var family in families) { Console.WriteLine("\tRead {0} from SQL", family); } // Query the documents using LINQ for the Andersen family families = from f in client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id) where f.Id == "AndersenFamily" select f; foreach (var family in families) { Console.WriteLine("Read {0} from LINQ", family); } // Query the documents using LINQ lambdas for the Andersen family families = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id) .Where(f => f.Id == "AndersenFamily") .Select(f => f); foreach (var family in families) { Console.WriteLine("\tRead {0} from LINQ query", family); } // Clean up/delete the database and client await client.DeleteDatabaseAsync("dbs/" + database.Id); client.Dispose(); }
public void Dispose() { _cosmosDBClient.Dispose(); }
private static async Task GetStartedDemo() { // Create a new instance of the DocumentClient var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey); // Check to verify a database with the id=FamilyRegistry does not exist Database database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault(); // If the database does not exist, create a new database if (database == null) { database = await client.CreateDatabaseAsync( new Database { Id = "FamilyRegistry" }); WriteMessage("Created dbs/FamilyRegistry"); } // Check to verify a document collection with the id=FamilyCollection does not exist DocumentCollection documentCollection = client.CreateDocumentCollectionQuery("dbs/" + database.Id).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault(); // If the document collection does not exist, create a new collection if (documentCollection == null) { documentCollection = await client.CreateDocumentCollectionAsync("dbs/" + database.Id, new DocumentCollection { Id = "FamilyCollection" }); WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection"); } // Check to verify a document with the id=AndersenFamily does not exist Document document = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id).Where(d => d.Id == "AndersenFamily").AsEnumerable().FirstOrDefault(); // If the document does not exist, create a new document if (document == null) { // Create the Andersen Family document Family andersonFamily = new Family { Id = "AndersenFamily", LastName = "Andersen", Parents = new Parent[] { new Parent { FirstName = "Thomas" , SSN = "111-11-1111"}.Seal(), new Parent { FirstName = "Mary Kay", SSN = "222-22-2222"}.Seal() }, Children = new Child[] { new Child { FirstName = "Henriette Thaulow", Gender = "female", Grade = 5, Pets = new Pet[] { new Pet { GivenName = "Fluffy" } } } }, Address = new Address { State = "WA", County = "King", City = "Seattle" }, IsRegistered = true, GeneticCondition = "diabetes" }; Crypteron.CipherObject.Seal(andersonFamily); // or andersonFamily.Seal() // id based routing for the first argument, "dbs/FamilyRegistry/colls/FamilyCollection" await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, andersonFamily); WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection/docs/AndersenFamily"); } // Check to verify a document with the id=AndersenFamily does not exist document = client.CreateDocumentQuery("dbs/" + database.Id + "/colls/" + documentCollection.Id).Where(d => d.Id == "WakefieldFamily").AsEnumerable().FirstOrDefault(); if (document == null) { // Create the WakeField document Family wakefieldFamily = new Family { Id = "WakefieldFamily", Parents = new Parent[] { new Parent { FamilyName= "Wakefield", FirstName= "Robin" }, new Parent { FamilyName= "Miller", FirstName= "Ben" } }, Children = new Child[] { new Child { FamilyName= "Merriam", FirstName= "Jesse", Gender= "female", Grade= 8, Pets= new Pet[] { new Pet { GivenName= "Goofy" }, new Pet { GivenName= "Shadow" } } }, new Child { FamilyName= "Miller", FirstName= "Lisa", Gender= "female", Grade= 1 } }, Address = new Address { State = "NY", County = "Manhattan", City = "NY" }, IsRegistered = false, }; Crypteron.CipherObject.Seal(wakefieldFamily); // or wakefieldFamily.Seal() // id based routing for the first argument, "dbs/FamilyRegistry/colls/FamilyCollection" await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, wakefieldFamily); WriteMessage("Created dbs/FamilyRegistry/colls/FamilyCollection/docs/WakefieldFamily"); } // Query the documents using DocumentDB SQL for the Andersen family var families = client.CreateDocumentQuery<Family>("dbs/" + database.Id + "/colls/" + documentCollection.Id, "SELECT * " + "FROM Families f " + "WHERE f.id = \"AndersenFamily\""); foreach (var family in families) { Console.WriteLine("\tRead {0} from SQL", JsonConvert.SerializeObject(family.Unseal(), Formatting.Indented)); } // Query the documents using LINQ for the Andersen family families = from f in client.CreateDocumentQuery<Family>("dbs/" + database.Id + "/colls/" + documentCollection.Id) where f.Id == "AndersenFamily" select f; foreach (var family in families) { Console.WriteLine("Read {0} from LINQ", JsonConvert.SerializeObject(family.Unseal(), Formatting.Indented)); } // Query the documents using LINQ lambdas for the Andersen family // Need to get results back in original class with Secure attributes // Dynamic JObject is not currently supported families = client.CreateDocumentQuery<Family>("dbs/" + database.Id + "/colls/" + documentCollection.Id) .Where(f => f.Id == "AndersenFamily") .Select(f => f); foreach (var family in families) { Console.WriteLine("\tRead {0} from LINQ query", JsonConvert.SerializeObject(family.Unseal(), Formatting.Indented)); } // Clean up/delete the database and client Console.WriteLine("Done. Press 'd' to delete database prior to exiting"); var choice = Console.ReadKey().KeyChar; if (choice == 'd' || choice == 'D') { Console.WriteLine("\nDeleting database ..."); await client.DeleteDatabaseAsync("dbs/" + database.Id); } client.Dispose(); }
private static async Task CreateDatabase() { var client = new DocumentClient(new Uri(url), auth); // Create the database if it does not exist. Database database = client.CreateDatabaseQuery().Where(db => db.Id == "fProj") .AsEnumerable().FirstOrDefault(); if (database == null) { database = await client.CreateDatabaseAsync(new Database { Id = "fProj" }); } else { Console.WriteLine("Your Database existed previously"); } // Create the Document Collection if it does not exist. DocumentCollection col = client.CreateDocumentCollectionQuery (database.CollectionsLink) .Where(c => c.Id == "fpDocs") .AsEnumerable().FirstOrDefault(); if (col == null) { col = await client.CreateDocumentCollectionAsync (database.CollectionsLink, new DocumentCollection { Id = "fpDocs" }, new RequestOptions { OfferType = "S1" }); } else { Console.WriteLine("Yo, you already had that collection"); } // Create the first document Document doc = client.CreateDocumentQuery(col.DocumentsLink) .Where(d => d.Id == "1").AsEnumerable().FirstOrDefault(); if (doc == null) { Restaurant doc1 = new Restaurant { Id = "1", RName = "Duck's Country Bunker", UserId = "user1", FName = "Phil", LName = "McLovin", MenuItems = new MenuItem[] { new MenuItem { Id = "1", ItemName = "Small Fries", Calories = 333, Price = .99M }, new MenuItem { Id = "2", ItemName = "Medium Fries", Calories = 444, Price = 1.99M }, new MenuItem { Id = "3", ItemName = "Large Fries", Calories = 555, Price = 2.99M }, new MenuItem { Id = "4", ItemName = "Small Beer", Calories = 5, Price = .99M }, new MenuItem { Id = "5", ItemName = "Medium Beer", Calories = 10, Price = 2.00M }, new MenuItem { Id = "6", ItemName = "Normal Sized Beer", Calories = 300, Price = 3.00M }, new MenuItem { Id = "7", ItemName = "Coffee", Calories = 50, Price = 1.00M } }, Address = new Address { Id = "1", StreetNumber = "1235 SE Brooklyn St.", City = "Corvallis", State = "Oregon", ZipCode = "99775" } }; await client.CreateDocumentAsync(col.DocumentsLink, doc1); } // Create the second document doc = client.CreateDocumentQuery(col.DocumentsLink) .Where(d => d.Id == "2").AsEnumerable().FirstOrDefault(); if (doc == null) { Restaurant doc2 = new Restaurant { Id = "2", RName = "Obi One", UserId = "user1", FName = "Allen", LName = "Jones", MenuItems = new MenuItem[] { new MenuItem { Id = "1", ItemName = "Cheeseburger", Calories = 75, Price = .99M }, new MenuItem { Id = "2", ItemName = "Hamburger", Calories = 50, Price = 1M }, new MenuItem { Id = "3", ItemName = "Regular Onion Rings", Calories = 100, Price = 1.50M }, new MenuItem { Id = "4", ItemName = "Large Onion Rings", Calories = 300, Price = 2M } }, Address = new Address { Id = "2", StreetNumber = "4949 N Broadway Ave", City = "Eugene", State = "Oregon", ZipCode = "96709" } }; await client.CreateDocumentAsync(col.DocumentsLink, doc2); } doc = client.CreateDocumentQuery(col.DocumentsLink) .Where(d => d.Id == "3").AsEnumerable().FirstOrDefault(); if (doc == null) { Restaurant doc3 = new Restaurant { Id = "3", RName = "Classy Chicken", UserId = "user2", FName = "Ash", LName = "Awesome", MenuItems = new MenuItem[] { new MenuItem { Id = "1", ItemName = "Small Chicken Dinner", Calories = 1111, Price = 11M }, new MenuItem { Id = "2", ItemName = "Medium Chicken Dinner", Calories = 2222, Price = 22M }, new MenuItem { Id = "3", ItemName = "Large Chicken Dinner", Calories = 3333, Price = 33M }, new MenuItem { Id = "4", ItemName = "Freedom Sized Chicken Dinner", Calories = 4444, Price = 44M }, new MenuItem { Id = "5", ItemName = "Small Chicken Wings", Calories = 500, Price = 5M }, new MenuItem { Id = "6", ItemName = "Medium Chicken Wings", Calories = 1000, Price = 7M }, new MenuItem { Id = "7", ItemName = "Large Chicken Wings", Calories = 1500, Price = 10M } }, Address = new Address { Id = "3", StreetNumber = "500 Midtown Ave", City = "Los Angeles", State = "California", ZipCode = "90210" } }; await client.CreateDocumentAsync(col.DocumentsLink, doc3); } doc = client.CreateDocumentQuery(col.DocumentsLink) .Where(d => d.Id == "4").AsEnumerable().FirstOrDefault(); if (doc == null) { Restaurant doc4 = new Restaurant { Id = "4", RName = "Four Stars", UserId = "user2", FName = "Ash", LName = "Awesome", MenuItems = new MenuItem[] { new MenuItem { Id = "1", ItemName = "Pretentious Meal #1", Calories = 1, Price = 100M }, new MenuItem { Id = "2", ItemName = "Pretentious Meal #2", Calories = 2, Price = 100M }, new MenuItem { Id = "3", ItemName = "Pretentious Meal #3", Calories = 3, Price = 300M }, new MenuItem { Id = "4", ItemName = "Small Gerbil Food", Calories = 4, Price = 400M }, new MenuItem { Id = "5", ItemName = "Medium Gerbil Food", Calories = 5, Price = 50M }, new MenuItem { Id = "6", ItemName = "Large Gerbil Food", Calories = 6, Price = 60M } }, Address = new Address { Id = "4", StreetNumber = "3001 SE Belmont St.", City = "Portland", State = "Oregon", ZipCode = "97214" } }; await client.CreateDocumentAsync(col.DocumentsLink, doc4); } var items = client.CreateDocumentQuery(col.DocumentsLink, "SELECT r.id, r.rName FROM Restaurant r"); // Query and check what I have entered is there. foreach(var rest in items) { Console.WriteLine(rest); } // Begin seeding of the collection with users Document user = client.CreateDocumentQuery(col.DocumentsLink).Where(d => d.Id == "user1") .AsEnumerable().FirstOrDefault(); if (user == null) { Users user1 = new Users { Id = "user1", UserName = "******", Password = "******" }; await client.CreateDocumentAsync(col.DocumentsLink, user1); } user = client.CreateDocumentQuery(col.DocumentsLink).Where(d => d.Id == "user2") .AsEnumerable().FirstOrDefault(); if (user == null) { Users user2 = new Users { Id = "user2", UserName = "******", Password = "******" }; await client.CreateDocumentAsync(col.DocumentsLink, user2); } // TODO: Try to query a user assosciated with a restaurant as a separate document. /* */ //await client.DeleteDatabaseAsync(database.SelfLink); // Dispose of the client connection. client.Dispose(); }
private static async Task GetStartedDemo() { // Make sure to call client.Dispose() once you've finished all DocumentDB interactions // Create a new instance of the DocumentClient var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey); // Check to verify a database with the id=FamilyRegistry does not exist Database database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault(); if (database == null) { // Create a database database = await client.CreateDatabaseAsync( new Database { Id = "FamilyRegistry" }); } else { Warn("database"); } // Check to verify a document collection with the id=FamilyCollection does not exist DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault(); if (documentCollection == null) { // Create a document collection using the lowest performance tier available (currently, S1) documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection { Id = "FamilyCollection" }, new RequestOptions { OfferType = "S1" }); } else { Warn("document collection"); } // Check to verify a document with the id=AndersenFamily does not exist Document document = client.CreateDocumentQuery(documentCollection.DocumentsLink).Where(d => d.Id == "AndersenFamily").AsEnumerable().FirstOrDefault(); if (document == null) { // Create the Andersen Family document Family AndersonFamily = new Family { Id = "AndersenFamily", LastName = "Andersen", Parents = new Parent[] { new Parent { FirstName = "Thomas" }, new Parent { FirstName = "Mary Kay"} }, Children = new Child[] { new Child { FirstName = "Henriette Thaulow", Gender = "female", Grade = 5, Pets = new Pet[] { new Pet { GivenName = "Fluffy" } } } }, Address = new Address { State = "WA", County = "King", City = "Seattle" }, IsRegistered = true }; await client.CreateDocumentAsync(documentCollection.DocumentsLink, AndersonFamily); } else { Warn("document"); } // Check to verify a document with the id=AndersenFamily does not exist document = client.CreateDocumentQuery(documentCollection.DocumentsLink).Where(d => d.Id == "WakefieldFamily").AsEnumerable().FirstOrDefault(); if (document == null) { // Create the WakeField document Family WakefieldFamily = new Family { Id = "WakefieldFamily", Parents = new Parent[] { new Parent { FamilyName= "Wakefield", FirstName= "Robin" }, new Parent { FamilyName= "Miller", FirstName= "Ben" } }, Children = new Child[] { new Child { FamilyName= "Merriam", FirstName= "Jesse", Gender= "female", Grade= 8, Pets= new Pet[] { new Pet { GivenName= "Goofy" }, new Pet { GivenName= "Shadow" } } }, new Child { FamilyName= "Miller", FirstName= "Lisa", Gender= "female", Grade= 1 } }, Address = new Address { State = "NY", County = "Manhattan", City = "NY" }, IsRegistered = false }; await client.CreateDocumentAsync(documentCollection.DocumentsLink, WakefieldFamily); } else { Warn("document"); } // Query the documents using DocumentDB SQL for the Andersen family var families = client.CreateDocumentQuery(documentCollection.DocumentsLink, "SELECT * " + "FROM Families f " + "WHERE f.id = \"AndersenFamily\""); foreach (var family in families) { Console.WriteLine("\tRead {0} from SQL", family); } // Query the documents using LINQ for the Andersen family families = from f in client.CreateDocumentQuery(documentCollection.DocumentsLink) where f.Id == "AndersenFamily" select f; foreach (var family in families) { Console.WriteLine("\tRead {0} from LINQ", family); } // Query the documents using LINQ lambdas for the Andersen family families = client.CreateDocumentQuery(documentCollection.DocumentsLink) .Where(f => f.Id == "AndersenFamily") .Select(f => f); foreach (var family in families) { Console.WriteLine("\tRead {0} from LINQ query", family); } // Query the documents using DocumentSQl with one join var items = client.CreateDocumentQuery<dynamic>(documentCollection.DocumentsLink, "SELECT f.id, c.FirstName AS child " + "FROM Families f " + "JOIN c IN f.Children"); foreach (var item in items.ToList()) { Console.WriteLine(item); } // Query the documents using LINQ with one join items = client.CreateDocumentQuery<Family>(documentCollection.DocumentsLink) .SelectMany(family => family.Children .Select(children => new { family = family.Id, child = children.FirstName })); foreach (var item in items.ToList()) { Console.WriteLine(item); } // Clean up/delete the database and client await client.DeleteDatabaseAsync(database.SelfLink); client.Dispose(); }