using System.Collections.Generic;
private static string tableName = "P";
private static int sampleBookId = 555;
static void Main(string[] args)
CATEGORIA_PRODUCTOS = Table.LoadTable(client, tableName);
CreateBookItem(productCatalog);
RetrieveBook(productCatalog);
UpdateMultipleAttributes(productCatalog);
UpdateBookPriceConditionally(productCatalog);
DeleteBook(productCatalog);
Console.WriteLine("To continue, press Enter");
catch (Exception e) { Console.WriteLine(e.Message); }
private static void CreateBookItem(Table productCatalog)
Console.WriteLine("\n*** Executing CreateBookItem() ***");
var book = new Document();
book["Id"] = sampleBookId;
book["Title"] = "Book " + sampleBookId;
book["ISBN"] = "111-1111111111";
book["Authors"] = new List<string> { "Author 1", "Author 2", "Author 3" };
book["Dimensions"] = "8.5x11x.5";
book["InPublication"] = new DynamoDBBool(true);
book["InStock"] = new DynamoDBBool(false);
book["QuantityOnHand"] = 0;
productCatalog.PutItem(book);
private static void RetrieveBook(Table productCatalog)
Console.WriteLine("\n*** Executing RetrieveBook() ***");
GetItemOperationConfig config = new GetItemOperationConfig
AttributesToGet = new List<string> { "Id", "ISBN", "Title", "Authors", "Price" },
Document document = productCatalog.GetItem(sampleBookId, config);
Console.WriteLine("RetrieveBook: Printing book retrieved...");
private static void UpdateMultipleAttributes(Table productCatalog)
Console.WriteLine("\n*** Executing UpdateMultipleAttributes() ***");
Console.WriteLine("\nUpdating multiple attributes....");
int partitionKey = sampleBookId;
var book = new Document();
book["Id"] = partitionKey;
book["Authors"] = new List<string> { "Author x", "Author y" };
book["newAttribute"] = "New Value";
UpdateItemOperationConfig config = new UpdateItemOperationConfig
ReturnValues = ReturnValues.AllNewAttributes
Document updatedBook = productCatalog.UpdateItem(book, config);
Console.WriteLine("UpdateMultipleAttributes: Printing item after updates ...");
PrintDocument(updatedBook);
private static void UpdateBookPriceConditionally(Table productCatalog)
Console.WriteLine("\n*** Executing UpdateBookPriceConditionally() ***");
int partitionKey = sampleBookId;
var book = new Document();
book["Id"] = partitionKey;
Expression expr = new Expression();
expr.ExpressionStatement = "Price = :val";
expr.ExpressionAttributeValues[":val"] = 19.00;
UpdateItemOperationConfig config = new UpdateItemOperationConfig
ConditionalExpression = expr,
ReturnValues = ReturnValues.AllNewAttributes
Document updatedBook = productCatalog.UpdateItem(book, config);
Console.WriteLine("UpdateBookPriceConditionally: Printing item whose price was conditionally updated");
PrintDocument(updatedBook);
private static void DeleteBook(Table productCatalog)
Console.WriteLine("\n*** Executing DeleteBook() ***");
DeleteItemOperationConfig config = new DeleteItemOperationConfig
ReturnValues = ReturnValues.AllOldAttributes
Document document = productCatalog.DeleteItem(sampleBookId, config);
Console.WriteLine("DeleteBook: Printing deleted just deleted...");
private static void PrintDocument(Document updatedDocument)
foreach (var attribute in updatedDocument.GetAttributeNames())
string stringValue = null;
var value = updatedDocument[attribute];
stringValue = value.AsPrimitive().Value.ToString();
else if (value is PrimitiveList)
stringValue = string.Join(",", (from primitive
in value.AsPrimitiveList().Entries
select primitive.Value).ToArray());
Console.WriteLine("{0} - {1}", attribute, stringValue);