Home > Uncategorized > CRUD with #AWS #DynamoDB in .NET Core

CRUD with #AWS #DynamoDB in .NET Core

One of the true benefits of DynamoDB is that it’s great for mini-projects, where you have to run on virtually zero budget. It doesn’t charge per-hour like the amazon RDS options, or Document DB options. It charges per storage >25GB and per million requests.

So, I set up a DynamoDB via the UI, and named it “test”. It’s much better if you name this something more meaningful, but I was just messing. I added a primary key of ID which is a number.

Then, as you create a new .NET Core project, you’ll need a Nuget package as follows;

Install-Package AWSSDK.DynamoDBv2

So with any Database, the first thing is to connect to it, which you’ll need to set up your IAM credentials, and then write some code like;

var credentials = new BasicAWSCredentials(AWS_USER, AWS_KEY);
 var client = new AmazonDynamoDBClient(credentials, RegionEndpoint.EUWest1);
 var tableResponse = client.ListTablesAsync().Result;
 var context = new DynamoDBContext(client);

I’ve written everything synchronously here, not best design, but it makes it easy to follow the flow. The ListTablesAsync is optional, but helps verify that it’s working.

The Object I defined, named “test” is as follows;

class test
 {
     public int id { get; set; }
     public string animal { get; set; }
 }

CREATE

context.SaveAsync(new test {id = 4, animal = "Frog"}).Wait();

READ

var conditions = new List
 {
     new ScanCondition("id", ScanOperator.Equal, 4)
 };
 var allDocs = context.ScanAsync(conditions).GetRemainingAsync().Result;
 allDocs.ForEach(t => Console.WriteLine(t.animal));

UPDATE

context.SaveAsync(new test { id = 4, animal = "Lion" }).Wait();

DELETE

context.DeleteAsync(new test {id = 4, animal = "Lion"}).Wait();

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment