Skip to content

Separate DynamoDBOperationConfig Into Operation-Specific Configs #3421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 9, 2024
101 changes: 101 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/BaseOperationConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

namespace Amazon.DynamoDBv2.DataModel
{
/// <summary>
/// Base class for operation-specific configurations for DynamoDB object persistence operations.
/// </summary>
/// <remarks>
/// This should only contain members that are relevant to all object persistence operations,
/// anything operation-specific should be added to derived classes.
/// </remarks>
#if NET8_0_OR_GREATER
// The DataModel namespace doesn't support trimming yet, so annotate public classes/methods as incompatible
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)]
#endif
public abstract class BaseOperationConfig
{
/// <summary>
/// Indicates which DynamoDB table to use. This overrides the table specified
/// by the <see cref="DynamoDBTableAttribute"/> on the .NET objects that you're saving or loading.
/// </summary>
public string OverrideTableName { get; set; }

/// <summary>
/// Directs <see cref="DynamoDBContext"/> to prefix all table names
/// with a specific string. If this is null or empty, no prefix is used
/// and default table names are used.
/// </summary>
public string TableNamePrefix { get; set; }

/// <summary>
/// The object persistence model API relies on an internal cache of the DynamoDB table's metadata to
/// construct and validate requests. This controls how the cache key is derived, which influences
/// when the SDK will call DescribeTable internally to populate the cache.
/// </summary>
/// <remarks>
/// For <see cref="MetadataCachingMode.Default"/> the cache key will be a combination of the table name, credentials, region and service URL.
/// For <see cref="MetadataCachingMode.TableNameOnly"/> the cache key will only consist of the table name. This reduces cache misses in contexts
/// where you are accessing tables with identical structure but using different credentials or endpoints (such as a multi-tenant application).
/// </remarks>
public MetadataCachingMode? MetadataCachingMode { get; set; }

/// <summary>
/// If true disables fetching table metadata automatically from DynamoDB. Table metadata must be
/// defined by <see cref="DynamoDBAttribute"/> attributes and/or in <see cref = "AWSConfigsDynamoDB"/>.
/// </summary>
/// <remarks>
/// Setting this to true can avoid latency and thread starvation due to blocking asynchronous
/// DescribeTable calls that are used to populate the SDK's cache of table metadata.
/// It requires that the table's index schema be accurately described via the above methods,
/// otherwise exceptions may be thrown and/or the results of certain DynamoDB operations may change.
/// </remarks>
public bool? DisableFetchingTableMetadata { get; set; }

/// <summary>
/// Specification which controls the conversion between .NET and DynamoDB types.
/// </summary>
public DynamoDBEntryConversion Conversion { get; set; }

/// <summary>
/// Controls how <see cref="DynamoDBContext"/> interprets empty string values.
/// If the property is false (or not set), empty string values will be
/// interpreted as null values.
/// </summary>
public bool? IsEmptyStringValueEnabled { get; set; }

/// <summary>
/// Converts this to the shared <see cref="DynamoDBOperationConfig"/>
/// </summary>
/// <remarks>
/// Users should interact with the new, operation-specific configs, but we
/// convert to the internal shared config for the internal code paths.
/// </remarks>
/// <returns>A new <see cref="DynamoDBOperationConfig"/> with settings copied from the operation-specific config</returns>
internal virtual DynamoDBOperationConfig ToDynamoDBOperationConfig()
{
return new DynamoDBOperationConfig()
{
OverrideTableName = OverrideTableName,
TableNamePrefix = TableNamePrefix,
MetadataCachingMode = MetadataCachingMode,
DisableFetchingTableMetadata = DisableFetchingTableMetadata,
Conversion = Conversion,
IsEmptyStringValueEnabled = IsEmptyStringValueEnabled
};
}
}
}
58 changes: 58 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchGetConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

using System;

namespace Amazon.DynamoDBv2.DataModel
{
/// <summary>
/// Input for the BatchGet operation in the object-persistence programming model
/// </summary>
#if NET8_0_OR_GREATER
// The DataModel namespace doesn't support trimming yet, so annotate public classes/methods as incompatible
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)]
#endif
public class BatchGetConfig : BaseOperationConfig
{
/// <summary>
/// Property that directs <see cref="DynamoDBContext"/> to use consistent reads.
/// If property is not set, behavior defaults to non-consistent reads.
/// </summary>
/// <remarks>
/// Refer to the <see href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html">
/// Read Consistency</see> topic in the DynamoDB Developer Guide for more information.
/// </remarks>
public bool? ConsistentRead { get; set; }

/// <summary>
/// If true, all <see cref="DateTime"/> properties are retrieved in UTC timezone while reading data from DynamoDB. Else, the local timezone is used.
/// </summary>
/// <remarks>
/// This setting is only applicable to the high-level library. Service calls made via
/// <see cref="AmazonDynamoDBClient"/> will always return <see cref="DateTime"/> attributes in UTC.
/// </remarks>
public bool? RetrieveDateTimeInUtc { get; set; }

/// <inheritdoc/>
internal override DynamoDBOperationConfig ToDynamoDBOperationConfig()
{
var config = base.ToDynamoDBOperationConfig();
config.ConsistentRead = ConsistentRead;
config.RetrieveDateTimeInUtc = RetrieveDateTimeInUtc;

return config;
}
}
}
52 changes: 52 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchWriteConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

namespace Amazon.DynamoDBv2.DataModel
{
/// <summary>
/// Input for the BatchWrite operation in the object-persistence programming model
/// </summary>
#if NET8_0_OR_GREATER
// The DataModel namespace doesn't support trimming yet, so annotate public classes/methods as incompatible
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)]
#endif
public class BatchWriteConfig : BaseOperationConfig
{
/// <summary>
/// Property that directs <see cref="DynamoDBContext"/> to skip version checks
/// when saving or deleting an object with a version attribute.
/// If property is not set, version checks are performed.
/// </summary>
public bool? SkipVersionCheck { get; set; }

/// <summary>
/// Directs <see cref="DynamoDBContext" /> to ignore null values when
/// converting an object to a DynamoDB item. If the property is false
/// (or not set), null values will be interpreted as directives to
/// delete the specific attributes on the DynamoDB item.
/// </summary>
public bool? IgnoreNullValues { get; set; }

/// <inheritdoc/>
internal override DynamoDBOperationConfig ToDynamoDBOperationConfig()
{
var config = base.ToDynamoDBOperationConfig();
config.SkipVersionCheck = SkipVersionCheck;
config.IgnoreNullValues = IgnoreNullValues;

return config;
}
}
}
Loading