The service client AmazonDynamoDBClient provides a 1 to 1 mapping of the DynamoDB service API. Using the service client gives access to the full API but using the API is more verbose and low level. You will have to implement your own custom logic for transforming your own data structures into the request objects used for DynamoDB.
At the service client level an item is a Dictionary<string, AttributeValue> where the string is an attribute name like Name and AttributeValue contains the value. AttributeValue has different property for each data type that DynamoDB supports. Only one property should be set.
| Property | Type |
|---|---|
| S | String |
| SS | Set of unique strings |
| N | Numeric (Note: the numeric is converted to a string on the AttributeValue) |
| NS | Set of unique numerics |
| B | Binary |
| BS | Set of unique binaries |
| BOOL | Boolean |
| L | List of AttributeValues |
| M | Map of string to AttributeValues |
The PutItem operation either creates a new item or replaces an entire existing item.
Using the Hash and range key, if a range key was set for the table, the item is returned as a Dictionary<string, AttributeValue>.
If your use case only needs a limited set of attributes then by using the AttributesToGet property the amount of read units consumed can be reduced for just the attributes returned.
DynamoDB is an eventually consistent system. Which means after a write operation an immediate read might not return the same data as was just written. If your application can't handle eventually consistent read then set the ConsistentRead property on the GetItemRequest to true. Using ConsistentRead does consume additional read unit from the provision capacity.
The UpdateItem operation can be used to do a partial update of the item in DynamoDB. This is really useful when you are only changing a few fields and can save you from consuming write units for attributes that you are not changing. Items are updated using expressions that define how to update attributes. Expressions can also be used to do conditional updates.
| Name | Description |
|---|---|
| SET | Sets an attribute. |
| REMOVE | Remove an attribute from an item |
| ADD | Adds the value to an item. If the type is 'number' then adds the value to existing value. If the type is a set then adds to the collection. |
| DELETE | deletes an element from a set |
For more information about DynamoDB expressions check out the Developer Guide
Query allows you to search the range key within the hash key. In this example the expression set for KeyConditionExpression only includes the hash key so all TODO lists will be returned for the user.
Many operations in AWS like the query operation use paging to support large return payloads. Operations that support paging will have a property marking the last point searched on the response object. To continue the query to the next page, set the marker property from the response object equal to the request object and call the operation again.
For the query operation the LastEvaluatedKey property on the response needs to be set to the ExclusiveStartKey on the request.
request.ExclusiveStartKey = response?.LastEvaluatedKey;To delete the item, pass in the table name and the key information.
Allows fast queries on other attributes besides hash key.
Search for items based on any attributes. This will cause a full table scan so use sparingly.
- Getting Started
- What is a serverless application?
- Common AWS Serverless Services
- What are we going to build in this tutorial?
- TODO List AWS Services Used
- Using DynamoDB to store TODO Lists
- Creating DynamoDB table
- Accessing DynamoDB items with the AWS SDK for .NET
- CRUD operations with Amazon DynamoDB Service Client
- CRUD operations with Document Model API
- CRUD operations with Data Model API
- Amazon DynamoDB wrap up
- Handling service events with Lambda
- Getting ASP.NET Core ready for Serverless
- Deploying ASP.NET Core as a Serverless Application
- Tear Down
- Final Wrap Up
Continue on to next page: CRUD operations with Document Model API