Date:

Share:

Understanding Table Name Conventions and DynamoDBTable Attribute

Related Articles

God .NET DynamoDB SDK By default uses conventions to resolve table names.

The class name used in DynamoDB SDK operations is used as the table name by default. For example, if the class is WeatherForecastThe DynamoDB SDK by default looks for a table named ‘WeatherForecast’.

However, there are ways you can customize this name and override it based on your application and the environment it’s running.

This article is sponsored by AWS and is part of my AWS series.

In this blog post, let’s explore

  • The default table name convention used by DynamoDB .NET
  • Configuring DynamoDB table naming conventions with .NET
  • DynamoDBTableAttribute

If you’re new to DynamoDB, I highly recommend checking out my Getting Started with AWS DynamoDB For the .NET Developer post below, where I also show you how to set up the table used in this blog post.

AWS DynamoDB for .NET Developer: How to get started easily

Learn how to get started with AWS DynamoDB with .NET Core by updating the default ASP NET Web API template to use DynamoDB as its data store. We will learn to perform basic create, read, update and delete operations from the API.

The default table name convention used by DynamoDB .NET

When using either method in DynamoDBContext object in the .NET DynamoDB SDK, you must pass in a .NET type, as shown below.

return await _dynamoDbContext.LoadAsync<WeatherForecast>(cityName, date);

await _dynamoDbContext
     .QueryAsync<WeatherForecast>(cityName).GetRemainingAsync();
     
 await _dynamoDbContext
     .QueryAsync<WeatherForecast>(
     	cityName,
     	QueryOperator.GreaterThan, new object[] { startDate })
     .GetRemainingAsync();
     
await _dynamoDbContext.SaveAsync<WeatherForecast>(data);
               

By default, the SDK uses nameof(WeatherForecast) to automatically determine the DynamoDB table name.

Configuring DynamoDB table naming conventions with .NET

Based on your team, application, and AWS account setup, you may sometimes need to override the table names.

One of the common reasons I’ve come across is when you have multiple application environments (developer, test), etc. deployed on the same AWS account. You cannot have multiple tables with the same name in the same AWS account.

In these scenarios, it’s great to be able to override/customize the DynamoDB table names based on the application environment/configuration.

For example, we can customize our table names with the name of the environment they are running on (‘dev_WeatherForecast‘,’test_Weather Forecast‘).

You can set this at the application/global level and also at the individual action level of the DynamoDBContext for example.

Application/global level

To set all actions in DynamoDBContext For an application, you can specify a DynamoDBContextConfig When creating the DynamoDBContext.

You can set this when setting up the DynamoDBContext in the app’s DI container.

We can use TableNamePrefix property in the configuration, to add the defined text to all table names.

The code below sets it toTest_‘ when the application is running in the test environment.

var dynamoDbClient = new AmazonDynamoDBClient(
	FallbackCredentialsFactory.GetCredentials(), RegionEndpoint.APSoutheast2);

var dynamoDBContext = new DynamoDBContext(
    dynamoDbClient, new DynamoDBContextConfig() {TableNamePrefix = "test_"});

builder.Services.AddSingleton<IAmazonDynamoDB>(dynamoDbClient);
builder.Services.AddSingleton<IDynamoDBContext>(dynamoDBContext);

The prefix value itself can come from the Application configuration file.

Personal action level

Table naming conventions can be overridden at individual operation levels also when using DynamoDBContext.

The code below specifies the TableNamePrefix you too OverrideTableName Assets on DynamoDBOperationConfig resist.

return await _dynamoDbContext
    .QueryAsync<WeatherForecastTable>(
        cityName,
        new DynamoDBOperationConfig()
        {
            TableNamePrefix = "test_", 
            OverrideTableName = "WeatherForecastTable"
        })
    .GetRemainingAsync();

This overrides the table name based on type conventions and uses the value specified in OverrideTableName attribute and also prefixes the value with TableNamePrefix.

If there is a prefix specified at the global level it will be replaced in this case by the one at the operation level. However, you cannot override it to be null or empty at the action level, if you have already specified one at the global level.

DynamoDBTableAttribute

In cases where you want to use a different name for the table outside of the default convention (based on the type name), in all operations on that type, you can use DynamoDBTableAttribute.

God DynamoDBTable property Designates a class and specifies the name to be used as the default instead of the name using the default convention.

[DynamoDBTable("WeatherForecastTable")]
public class WeatherForecastTable
{
   public string CityName { get; set; }
   public DateTime Date { get; set; }
   ...
}

Whenever you use this type with DynamoDBContexthe uses the name what- DynamoDBTable attribute if specified in the type.

However, note that any action level overrides will still take precedence if one is specified.

Source

Popular Articles