DynamoDb is a popular No SQL Database Services offered by AWS. It's mainly used in Serverless architecture where DynamoDb is mainly used as a persistence object.
Accessing Data in DynamoDb in a Middle Tier like REST API Backend Services is a common requirement of the Web Application. In this article, I will be giving code examples to access DynamoDb in ASP.Net Core Web API using C# programming language.
To Access DynamoDb from ASP.NET Core Web API, you need AWS access key id and AWS Secret Access Key which you can find in the My Security Credentials page in AWS Browser Console by clicking on your name dropdown in the upper right corner.
Let's assume we have a Student table in Dynamodb which has schema has below and we want the normal CRUD( Create, Read, Update and Delete) operation with the Student Data.
Let's Create an Interface in C# like below to implement the CRUD operation.
Let's implement this interface in a class named "StudentService" so to access DynamoDb from your application we need to install a package named AWSSDK.DynamoDBv2 from Nuget Package Manager. I am using version 18.104.22.168 but you can higher version if my code example is not giving any error.
First, we need to inject DynamoDb Context using Constructor Injection like below
Let's implement the GetAllStudent() method of the interface like below
In the above example, you can see that we are using DynamoDb Context to get the entire data Student Table.
Also, Please note we have to create a Student Model in your application exactly the same as the Schema of the Student table of DynamoDb like below
If you are having the model property name different compared to the Database column name then first you need to retrieve the data and then do the mapping to the object of your backend Student class.
In the above example, we can see we have ScanCondition through which we can apply multiple conditions and get the final result.
Now we can inject the above Student Service in our controller class as below to perform the CRUD operation.
Hope this article will help you to normal CRUD operation for DynamoDb tables in ASP.Net Core.