Skip to content

Creating DataContext instances

scale-tone edited this page Apr 16, 2017 · 3 revisions

You need to pass a pre-configured AmazonDynamoDBClient to DataContext's constructor:

var client = new AmazonDynamoDBClient(yourAccessKey, yourSecretKey);

var ctx = new DataContext(client, string.Empty);

This allows you to feed AmazonDynamoDBClient with AWS credentials and other important parameters (region, proxy credentials etc.) before. Think of AmazonDynamoDBClient as a connection object for DataContext, just like SqlConnection for System.Data.Linq.DataContext.

The second constructor parameter is a prefix, that will be added to all table names when communicating with DynamoDB. So you can have multiple (production, test, etc.) environments in the same AWS account and switch between them via a config file.

NOTE: DataContext object is lightweight yet not thread-safe. So, feel free to (re)create it's instances anytime you want, but avoid sharing them between different threads. Yes, this principle differs much from the rules of AWS SDK DynamoDBContext's usage: that one is supposed to be created once and shared as widely as possible.