Exemplo n.º 1
0
        private static async Task UseDynamics(string colSelfLink)
        {
            //Create a dynamic object,
            dynamic dynamicOrder = new
            {
                id = "DYN01",
                purchaseOrderNumber = "PO18009186470",
                orderDate = DateTime.UtcNow,
                total = 5.95,
            };

            Document createdDocument = await client.CreateDocumentAsync(colSelfLink, dynamicOrder);

            //get a dynamic object
            dynamic readDynOrder = (await client.ReadDocumentAsync(createdDocument.SelfLink)).Resource;

            //update a dynamic object
            readDynOrder.ShippedDate = DateTime.UtcNow;
            await client.ReplaceDocumentAsync(readDynOrder);
        }
Exemplo n.º 2
0
        private static async Task UseAttachments(string colSelfLink)
        {
            dynamic documentWithAttachment = new 
            {
                id = "PO1800243243470",
                CustomerId = 1092,
                TotalDue = 985.018m,
            };

            Document doc = await client.CreateDocumentAsync(colSelfLink, documentWithAttachment);

            //This attachment could be any binary you want to attach. Like images, videos, word documents, pdfs etc. it doesn't matter
            using (FileStream fileStream = new FileStream(@".\Attachments\text.txt", FileMode.Open))
            {
                //Create the attachment
                await client.CreateAttachmentAsync(doc.AttachmentsLink, fileStream, new MediaOptions { ContentType = "text/plain", Slug = "text.txt" });
            }

            //Query for document for attachment for attachments
            Attachment attachment = client.CreateAttachmentQuery(doc.SelfLink).AsEnumerable().FirstOrDefault();
            
            //Use DocumentClient to read the Media content
            MediaResponse content = await client.ReadMediaAsync(attachment.MediaLink);

            byte[] bytes = new byte[content.ContentLength];
            await content.Media.ReadAsync(bytes, 0, (int)content.ContentLength);
            string result = Encoding.UTF8.GetString(bytes);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 2. Basic CRUD operations using dynamics instead of POCOs
        /// </summary>
        private static async Task UseDynamics()
        {
            Console.WriteLine("\n2. Use Dynamics");

            var collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

            //Create a dynamic object,
            //Notice the case here. The properties will be created in the database
            //with whatever case you give the property
            //If you read this back in to a Document object id will get mapped to Id
            dynamic dynamicOrder = new
            {
                id = "DYN01",
                purchaseOrderNumber = "PO18009186470",
                orderDate = DateTime.UtcNow,
                total = 5.95,
            };

            Console.WriteLine("\nCreating document");

            ResourceResponse<Document> response = await client.CreateDocumentAsync(collectionLink, dynamicOrder);
            var createdDocument = response.Resource;

            Console.WriteLine("Document with id {0} created", createdDocument.Id);
            Console.WriteLine("Request charge of operation: {0}", response.RequestCharge);

            response = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, "DYN01"));
            var readDocument = response.Resource;

            //update a dynamic object by just creating a new Property on the fly
            //Document is itself a dynamic object, so you can just use this directly too if you prefer
            readDocument.SetPropertyValue("shippedDate",DateTime.UtcNow);

            //if you wish to work with a dynamic object so you don't need to use SetPropertyValue() or GetPropertyValue<T>()
            //then you can cast to a dynamic
            dynamicOrder = (dynamic)readDocument;
            dynamicOrder.foo = "bar";

            //now do a replace using this dynamic document
            //notice here you don't have to set collectionLink, or documentSelfLink, 
            //everything that is needed is contained in the readDynOrder object 
            //it has a .self Property
            Console.WriteLine("\nReplacing document");

            response = await client.ReplaceDocumentAsync(dynamicOrder);
            var replaced = response.Resource;

            Console.WriteLine("Request charge of operation: {0}", response.RequestCharge);
            Console.WriteLine("shippedDate: {0} and foo: {1} of replaced document", replaced.GetPropertyValue<DateTime>("shippedDate"), replaced.GetPropertyValue<string>("foo"));
        }