Esempio: CRUD operazioni che utilizzano il modello di persistenza degli oggetti AWS SDK for .NET - Amazon DynamoDB

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Esempio: CRUD operazioni che utilizzano il modello di persistenza degli oggetti AWS SDK for .NET

L'esempio di codice C# seguente dichiara una classe Book con le proprietà Id, Title, Isbn e BookAuthors. Nell'esempio vengono utilizzati gli attributi di persistenza degli oggetti per mappare queste proprietà alla tabella ProductCatalog in Amazon DynamoDB. L'esempio utilizza quindi la ynamoDBContext classe D per illustrare le tipiche operazioni di creazione, lettura, aggiornamento ed eliminazione ()CRUD. L'esempio crea un esempio Book instance e lo salva nella ProductCatalog tabella. Viene quindi recuperato l'elemento del libro e vengono aggiornate le relative proprietà Isbn e BookAuthors. Tenere presente che l'aggiornamento sostituisce l'elenco degli autori esistenti. Infine, viene eliminato l'elemento del libro.

Per ulteriori informazioni sulla tabella ProductCatalog utilizzata in questo esempio, consulta Creazione di tabelle e caricamento di dati per esempi di codice in DynamoDB. Per step-by-step istruzioni su come testare l'esempio seguente, vedere. NETesempi di codice.

Nota

L'esempio seguente non funziona con. NETcore perché non supporta i metodi sincroni. Per ulteriori informazioni, consulta AWS APIsasynchronous for. NET.

Esempio CRUDoperazioni che utilizzano la ynamoDBContext classe D
/// <summary> /// Shows how to perform high-level CRUD operations on an Amazon DynamoDB /// table. /// </summary> public class HighLevelItemCrud { public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await PerformCRUDOperations(context); } public static async Task PerformCRUDOperations(IDynamoDBContext context) { int bookId = 1001; // Some unique value. Book myBook = new Book { Id = bookId, Title = "object persistence-AWS SDK for.NET SDK-Book 1001", Isbn = "111-1111111001", BookAuthors = new List<string> { "Author 1", "Author 2" }, }; // Save the book to the ProductCatalog table. await context.SaveAsync(myBook); // Retrieve the book from the ProductCatalog table. Book bookRetrieved = await context.LoadAsync<Book>(bookId); // Update some properties. bookRetrieved.Isbn = "222-2222221001"; // Update existing authors list with the following values. bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; await context.SaveAsync(bookRetrieved); // Retrieve the updated book. This time, add the optional // ConsistentRead parameter using DynamoDBContextConfig object. await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); // Delete the book. await context.DeleteAsync<Book>(bookId); // Try to retrieve deleted book. It should return null. Book deletedBook = await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); if (deletedBook == null) { Console.WriteLine("Book is deleted"); } } }
Esempio
Prenota informazioni sulla classe da aggiungere alla ProductCatalog tabella
/// <summary> /// A class representing book information to be added to the Amazon DynamoDB /// ProductCatalog table. /// </summary> [DynamoDBTable("ProductCatalog")] public class Book { [DynamoDBHashKey] // Partition key public int Id { get; set; } [DynamoDBProperty] public string Title { get; set; } [DynamoDBProperty] public string Isbn { get; set; } [DynamoDBProperty("Authors")] // String Set datatype public List<string> BookAuthors { get; set; } }