Exemple : opérations CRUD utilisant le modèle de persistance des objets AWS SDK for .NET - Amazon DynamoDB

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Exemple : opérations CRUD utilisant le modèle de persistance des objets AWS SDK for .NET

L'exemple de code C# suivant déclare une classe Book avec les propriétés Id, Title, ISBN et Authors. L'exemple utilise des attributs de persistance des objets pour mapper ces propriétés à la table ProductCatalog dans Amazon DynamoDB. L'exemple utilise ensuite le DynamoDBContext pour illustrer les opérations de création, lecture, mise à jour et suppression (CRUD) typiques. L'exemple crée un exemple d'instance Book et l'enregistre dans la table ProductCatalog. Il extrait ensuite l'élément livre et met à jour ses propriétés ISBN et Authors. Notez que la mise à jour remplace la liste des auteurs existants. Enfin, l'exemple supprime l'élément livre.

Pour plus d'informations sur la table ProductCatalog utilisée dans cet exemple, consultez Création de tables et chargement de données pour des exemples de code dans DynamoDB. Pour step-by-step obtenir des instructions permettant de tester l'exemple suivant, reportez-vous àExemples de code .NET.

Note

L'exemple suivant ne fonctionne pas avec .NET Core qui ne prend pas en charge les méthodes synchrones. Pour plus d'informations, consultez API asynchrones AWS pour .NET.

Exemple
using System; using System.Collections.Generic; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DataModel; using Amazon.Runtime; namespace com.amazonaws.codesamples { class HighLevelItemCRUD { private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { try { DynamoDBContext context = new DynamoDBContext(client); TestCRUDOperations(context); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); } catch (AmazonServiceException e) { Console.WriteLine(e.Message); } catch (Exception e) { Console.WriteLine(e.Message); } } private static void TestCRUDOperations(DynamoDBContext 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. context.Save(myBook); // Retrieve the book. Book bookRetrieved = context.Load<Book>(bookID); // Update few properties. bookRetrieved.ISBN = "222-2222221001"; bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; // Replace existing authors list with this. context.Save(bookRetrieved); // Retrieve the updated book. This time add the optional ConsistentRead parameter using DynamoDBContextConfig object. Book updatedBook = context.Load<Book>(bookID, new DynamoDBContextConfig { ConsistentRead = true }); // Delete the book. context.Delete<Book>(bookID); // Try to retrieve deleted book. It should return null. Book deletedBook = context.Load<Book>(bookID, new DynamoDBContextConfig { ConsistentRead = true }); if (deletedBook == null) Console.WriteLine("Book is deleted"); } } [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; } } }