diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/BaseOperationConfig.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/BaseOperationConfig.cs index 5f52b622ba09..e8a5a5ea8759 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/BaseOperationConfig.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/BaseOperationConfig.cs @@ -71,7 +71,7 @@ public abstract class BaseOperationConfig public DynamoDBEntryConversion Conversion { get; set; } /// - /// Contorls how interprets emptry string values. + /// Controls how interprets empty string values. /// If the property is false (or not set), empty string values will be /// interpreted as null values. /// diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchGetConfig.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchGetConfig.cs new file mode 100644 index 000000000000..f7fb57066ee2 --- /dev/null +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchGetConfig.cs @@ -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 +{ + /// + /// Input for the BatchGet operation in the object-persistence programming model + /// +#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 + { + /// + /// Property that directs to use consistent reads. + /// If property is not set, behavior defaults to non-consistent reads. + /// + /// + /// Refer to the + /// Read Consistency topic in the DynamoDB Developer Guide for more information. + /// + public bool? ConsistentRead { get; set; } + + /// + /// If true, all properties are retrieved in UTC timezone while reading data from DynamoDB. Else, the local timezone is used. + /// + /// + /// This setting is only applicable to the high-level library. Service calls made via + /// will always return attributes in UTC. + /// + public bool? RetrieveDateTimeInUtc { get; set; } + + /// + internal override DynamoDBOperationConfig ToDynamoDBOperationConfig() + { + var config = base.ToDynamoDBOperationConfig(); + config.ConsistentRead = ConsistentRead; + config.RetrieveDateTimeInUtc = RetrieveDateTimeInUtc; + + return config; + } + } +} diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchWriteConfig.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchWriteConfig.cs new file mode 100644 index 000000000000..d96bb4bb93e9 --- /dev/null +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/BatchWriteConfig.cs @@ -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 +{ + /// + /// Input for the BatchWrite operation in the object-persistence programming model + /// +#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 + { + /// + /// Property that directs to skip version checks + /// when saving or deleting an object with a version attribute. + /// If property is not set, version checks are performed. + /// + public bool? SkipVersionCheck { get; set; } + + /// + /// Directs 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. + /// + public bool? IgnoreNullValues { get; set; } + + /// + internal override DynamoDBOperationConfig ToDynamoDBOperationConfig() + { + var config = base.ToDynamoDBOperationConfig(); + config.SkipVersionCheck = SkipVersionCheck; + config.IgnoreNullValues = IgnoreNullValues; + + return config; + } + } +} diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs index 80e4b78ed8e9..80e9d1458b42 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs @@ -19,7 +19,6 @@ #if AWS_ASYNC_API using System.Threading.Tasks; #endif -using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DocumentModel; namespace Amazon.DynamoDBv2.DataModel @@ -208,7 +207,7 @@ public void Dispose() #endregion - #region Factory Creates + #region BatchGet /// /// Creates a strongly-typed BatchGet object, allowing @@ -218,7 +217,7 @@ public void Dispose() /// Empty strongly-typed BatchGet object public BatchGet CreateBatchGet() { - return CreateBatchGet(null); + return CreateBatchGet((BatchGetConfig)null); } /// @@ -228,12 +227,25 @@ public BatchGet CreateBatchGet() /// Type of objects to get /// Config object which can be used to override that table used. /// Empty strongly-typed BatchGet object + [Obsolete("Use the CreateBatchGet overload that takes BatchGetConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchGet.")] public BatchGet CreateBatchGet(DynamoDBOperationConfig operationConfig) { DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config); return new BatchGet(this, config); } + /// + /// Creates a strongly-typed BatchGet object, allowing + /// a batch-get operation against DynamoDB. + /// + /// Type of objects to get + /// Config object that can be used to override properties on the table's context for this request + /// Empty strongly-typed BatchGet object + public BatchGet CreateBatchGet(BatchGetConfig batchGetConfig) + { + return new BatchGet(this, new DynamoDBFlatConfig(batchGetConfig?.ToDynamoDBOperationConfig(), Config)); + } + /// /// Creates a MultiTableBatchGet object, composed of multiple /// individual BatchGet objects. @@ -245,6 +257,10 @@ public MultiTableBatchGet CreateMultiTableBatchGet(params BatchGet[] batches) return new MultiTableBatchGet(batches); } + #endregion + + #region BatchWrite + /// /// Creates a strongly-typed BatchWrite object, allowing /// a batch-write operation against DynamoDB. @@ -253,7 +269,7 @@ public MultiTableBatchGet CreateMultiTableBatchGet(params BatchGet[] batches) /// Empty strongly-typed BatchWrite object public BatchWrite CreateBatchWrite() { - return CreateBatchWrite(null); + return CreateBatchWrite((BatchWriteConfig)null); } /// @@ -263,6 +279,7 @@ public BatchWrite CreateBatchWrite() /// Type of objects to write /// Config object which can be used to override that table used. /// Empty strongly-typed BatchWrite object + [Obsolete("Use the CreateBatchWrite overload that takes BatchWriteConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchWrite.")] public BatchWrite CreateBatchWrite(DynamoDBOperationConfig operationConfig) { DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config); @@ -283,7 +300,7 @@ public BatchWrite CreateBatchWrite(DynamoDBOperationConfig operationConfig /// Empty strongly-typed BatchWrite object public BatchWrite CreateBatchWrite(Type valuesType) { - return CreateBatchWrite(valuesType, null); + return CreateBatchWrite(valuesType, (BatchWriteConfig)null); } /// @@ -299,12 +316,43 @@ public BatchWrite CreateBatchWrite(Type valuesType) /// Type of objects to write /// Config object which can be used to override that table used. /// Empty strongly-typed BatchWrite object + [Obsolete("Use the CreateBatchWrite overload that takes BatchWriteConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchWrite.")] public BatchWrite CreateBatchWrite(Type valuesType, DynamoDBOperationConfig operationConfig) { DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config); return new BatchWrite(this, valuesType, config); } + /// + /// Creates a strongly-typed BatchWrite object, allowing + /// a batch-write operation against DynamoDB. + /// + /// Type of objects to write + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed BatchWrite object + public BatchWrite CreateBatchWrite(BatchWriteConfig batchWriteConfig) + { + return new BatchWrite(this, new DynamoDBFlatConfig(batchWriteConfig?.ToDynamoDBOperationConfig(), Config)); + } + + /// + /// Creates a strongly-typed BatchWrite object, allowing + /// a batch-write operation against DynamoDB. + /// + /// This is intended for use only when the valuesType is not known at compile-time, for example, + /// when hooking into EF's ChangeTracker to record audit logs from EF into DynamoDB. + /// + /// In scenarios when the valuesType is known at compile-time, the + /// method is generally preferred. + /// + /// The type of data which will be persisted in this batch. + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed BatchWrite object + public BatchWrite CreateBatchWrite(Type valuesType, BatchWriteConfig batchWriteConfig) + { + return new BatchWrite(this, valuesType, new DynamoDBFlatConfig(batchWriteConfig.ToDynamoDBOperationConfig(), Config)); + } + /// /// Creates a MultiTableBatchWrite object, composed of multiple /// individual BatchWrite objects. @@ -316,6 +364,10 @@ public MultiTableBatchWrite CreateMultiTableBatchWrite(params BatchWrite[] batch return new MultiTableBatchWrite(batches); } + #endregion + + #region TransactGet + /// /// Creates a strongly-typed TransactGet object, allowing /// a transactional get operation against DynamoDB. @@ -324,7 +376,7 @@ public MultiTableBatchWrite CreateMultiTableBatchWrite(params BatchWrite[] batch /// Empty strongly-typed TransactGet object. public TransactGet CreateTransactGet() { - return CreateTransactGet(null); + return CreateTransactGet((TransactGetConfig)null); } /// @@ -334,12 +386,26 @@ public TransactGet CreateTransactGet() /// Type of objects to get. /// Config object which can be used to override that table used. /// Empty strongly-typed TransactGet object. + [Obsolete("Use the CreateTransactGet overload that takes TransactGetConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchGet.")] public TransactGet CreateTransactGet(DynamoDBOperationConfig operationConfig) { DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config); return new TransactGet(this, config); } + /// + /// Creates a strongly-typed TransactGet object, allowing + /// a transactional get operation against DynamoDB. + /// + /// Type of objects to get. + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed TransactGet object. + public TransactGet CreateTransactGet(TransactGetConfig transactGetConfig) + { + DynamoDBFlatConfig config = new DynamoDBFlatConfig(transactGetConfig?.ToDynamoDBOperationConfig(), this.Config); + return new TransactGet(this, config); + } + /// /// Creates a MultiTableTransactGet object, composed of multiple /// individual TransactGet objects. @@ -351,6 +417,10 @@ public MultiTableTransactGet CreateMultiTableTransactGet(params TransactGet[] tr return new MultiTableTransactGet(transactionParts); } + #endregion + + #region TransactWrite + /// /// Creates a strongly-typed TransactWrite object, allowing /// a transactional write operation against DynamoDB. @@ -359,7 +429,7 @@ public MultiTableTransactGet CreateMultiTableTransactGet(params TransactGet[] tr /// Empty strongly-typed TransactWrite object. public TransactWrite CreateTransactWrite() { - return CreateTransactWrite(null); + return CreateTransactWrite((TransactWriteConfig)null); } /// @@ -369,12 +439,27 @@ public TransactWrite CreateTransactWrite() /// Type of objects to write. /// Config object which can be used to override that table used. /// Empty strongly-typed TransactWrite object. + + [Obsolete("Use the CreateTransactWrite overload that takes TransactWriteConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to CreateTransactWrite.")] public TransactWrite CreateTransactWrite(DynamoDBOperationConfig operationConfig) { DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config); return new TransactWrite(this, config); } + /// + /// Creates a strongly-typed TransactWrite object, allowing + /// a transactional write operation against DynamoDB. + /// + /// Type of objects to write. + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed TransactWrite object. + public TransactWrite CreateTransactWrite(TransactWriteConfig transactWriteConfig) + { + DynamoDBFlatConfig config = new DynamoDBFlatConfig(transactWriteConfig?.ToDynamoDBOperationConfig(), this.Config); + return new TransactWrite(this, config); + } + /// /// Creates a MultiTableTransactWrite object, composed of multiple /// individual TransactWrite objects. diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/IDynamoDBContext.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/IDynamoDBContext.cs index 4270efccaa08..17868bbf21f2 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/IDynamoDBContext.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/IDynamoDBContext.cs @@ -113,6 +113,15 @@ public partial interface IDynamoDBContext : IDisposable #endregion #region BatchGet + + /// + /// Creates a strongly-typed BatchGet object, allowing + /// a batch-get operation against DynamoDB. + /// + /// Type of objects to get + /// Empty strongly-typed BatchGet object + BatchGet CreateBatchGet(); + /// /// Creates a strongly-typed BatchGet object, allowing /// a batch-get operation against DynamoDB. @@ -120,8 +129,18 @@ public partial interface IDynamoDBContext : IDisposable /// Type of objects to get /// Config object which can be used to override that table used. /// Empty strongly-typed BatchGet object + [Obsolete("Use the CreateBatchGet overload that takes BatchGetConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchGet.")] BatchGet CreateBatchGet(DynamoDBOperationConfig operationConfig = null); + /// + /// Creates a strongly-typed BatchGet object, allowing + /// a batch-get operation against DynamoDB. + /// + /// Type of objects to get + /// Config object that can be used to override properties on the table's context for this request + /// Empty strongly-typed BatchGet object + public BatchGet CreateBatchGet(BatchGetConfig batchGetConfig); + /// /// Creates a MultiTableBatchGet object, composed of multiple /// individual BatchGet objects. @@ -134,6 +153,14 @@ public partial interface IDynamoDBContext : IDisposable #region Batch Write + /// + /// Creates a strongly-typed BatchWrite object, allowing + /// a batch-write operation against DynamoDB. + /// + /// Type of objects to write + /// Empty strongly-typed BatchWrite object + BatchWrite CreateBatchWrite(); + /// /// Creates a strongly-typed BatchWrite object, allowing /// a batch-write operation against DynamoDB. @@ -141,8 +168,24 @@ public partial interface IDynamoDBContext : IDisposable /// Type of objects to write /// Config object which can be used to override that table used. /// Empty strongly-typed BatchWrite object + [Obsolete("Use the CreateBatchWrite overload that takes BatchWriteConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchWrite.")] + BatchWrite CreateBatchWrite(DynamoDBOperationConfig operationConfig = null); + /// + /// Creates a strongly-typed BatchWrite object, allowing + /// a batch-write operation against DynamoDB. + /// + /// This is intended for use only when the valuesType is not known at compile-time, for example, + /// when hooking into EF's ChangeTracker to record audit logs from EF into DynamoDB. + /// + /// In scenarios when the valuesType is known at compile-time, the + /// method is generally preferred. + /// + /// The type of data which will be persisted in this batch. + /// Empty strongly-typed BatchWrite object + BatchWrite CreateBatchWrite(Type valuesType); + /// /// Creates a strongly-typed BatchWrite object, allowing /// a batch-write operation against DynamoDB. @@ -156,8 +199,33 @@ public partial interface IDynamoDBContext : IDisposable /// The type of data which will be persisted in this batch. /// Config object which can be used to override that table used. /// Empty strongly-typed BatchWrite object + [Obsolete("Use the CreateBatchWrite overload that takes BatchWriteConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchWrite.")] BatchWrite CreateBatchWrite(Type valuesType, DynamoDBOperationConfig operationConfig = null); + /// + /// Creates a strongly-typed BatchWrite object, allowing + /// a batch-write operation against DynamoDB. + /// + /// Type of objects to write + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed BatchWrite object + BatchWrite CreateBatchWrite(BatchWriteConfig batchWriteConfig); + + /// + /// Creates a strongly-typed BatchWrite object, allowing + /// a batch-write operation against DynamoDB. + /// + /// This is intended for use only when the valuesType is not known at compile-time, for example, + /// when hooking into EF's ChangeTracker to record audit logs from EF into DynamoDB. + /// + /// In scenarios when the valuesType is known at compile-time, the + /// method is generally preferred. + /// + /// The type of data which will be persisted in this batch. + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed BatchWrite object + BatchWrite CreateBatchWrite(Type valuesType, BatchWriteConfig batchWriteConfig); + /// /// Creates a MultiTableBatchWrite object, composed of multiple /// individual BatchWrite objects. @@ -170,6 +238,14 @@ public partial interface IDynamoDBContext : IDisposable #region Transact Get + /// + /// Creates a strongly-typed TransactGet object, allowing + /// a transactional get operation against DynamoDB. + /// + /// Type of objects to get. + /// Empty strongly-typed TransactGet object. + TransactGet CreateTransactGet(); + /// /// Creates a strongly-typed TransactGet object, allowing /// a transactional get operation against DynamoDB. @@ -177,8 +253,18 @@ public partial interface IDynamoDBContext : IDisposable /// Type of objects to get. /// Config object which can be used to override that table used. /// Empty strongly-typed TransactGet object. + [Obsolete("Use the CreateTransactGet overload that takes TransactGetConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to BatchGet.")] TransactGet CreateTransactGet(DynamoDBOperationConfig operationConfig = null); + /// + /// Creates a strongly-typed TransactGet object, allowing + /// a transactional get operation against DynamoDB. + /// + /// Type of objects to get. + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed TransactGet object. + TransactGet CreateTransactGet(TransactGetConfig transactGetConfig); + /// /// Creates a MultiTableTransactGet object, composed of multiple /// individual TransactGet objects. @@ -191,6 +277,14 @@ public partial interface IDynamoDBContext : IDisposable #region Transact Write + /// + /// Creates a strongly-typed TransactWrite object, allowing + /// a transactional write operation against DynamoDB. + /// + /// Type of objects to write. + /// Empty strongly-typed TransactWrite object. + TransactWrite CreateTransactWrite(); + /// /// Creates a strongly-typed TransactWrite object, allowing /// a transactional write operation against DynamoDB. @@ -198,8 +292,18 @@ public partial interface IDynamoDBContext : IDisposable /// Type of objects to write. /// Config object which can be used to override that table used. /// Empty strongly-typed TransactWrite object. + [Obsolete("Use the CreateTransactWrite overload that takes TransactWriteConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to CreateTransactWrite.")] TransactWrite CreateTransactWrite(DynamoDBOperationConfig operationConfig = null); + /// + /// Creates a strongly-typed TransactWrite object, allowing + /// a transactional write operation against DynamoDB. + /// + /// Type of objects to write. + /// Config object that can be used to override properties on the table's context for this request. + /// Empty strongly-typed TransactWrite object. + TransactWrite CreateTransactWrite(TransactWriteConfig transactWriteConfig); + /// /// Creates a MultiTableTransactWrite object, composed of multiple /// individual TransactWrite objects. diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactGetConfig.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactGetConfig.cs new file mode 100644 index 000000000000..2b4749a46081 --- /dev/null +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactGetConfig.cs @@ -0,0 +1,47 @@ +/* + * 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 +{ + /// + /// Input for the TransactGet operation in the object-persistence programming model + /// +#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 TransactGetConfig : BaseOperationConfig + { + /// + /// If true, all properties are retrieved in UTC timezone while reading data from DynamoDB. Else, the local timezone is used. + /// + /// + /// This setting is only applicable to the high-level library. Service calls made via + /// will always return attributes in UTC. + /// + public bool? RetrieveDateTimeInUtc { get; set; } + + /// + internal override DynamoDBOperationConfig ToDynamoDBOperationConfig() + { + var config = base.ToDynamoDBOperationConfig(); + config.RetrieveDateTimeInUtc = RetrieveDateTimeInUtc; + + return config; + } + } +} diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactWriteConfig.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactWriteConfig.cs new file mode 100644 index 000000000000..7e7231d12ae0 --- /dev/null +++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/TransactWriteConfig.cs @@ -0,0 +1,44 @@ +/* + * 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 +{ + /// + /// Input for the TransactWrite operation in the object-persistence programming model + /// +#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 TransactWriteConfig : BaseOperationConfig + { + /// + /// Directs 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. + /// + public bool? IgnoreNullValues { get; set; } + + /// + internal override DynamoDBOperationConfig ToDynamoDBOperationConfig() + { + var config = base.ToDynamoDBOperationConfig(); + config.IgnoreNullValues = IgnoreNullValues; + + return config; + } + } +} diff --git a/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DataModelOperationSpecificConfigTests.cs b/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DataModelOperationSpecificConfigTests.cs index 0ecf3bc18180..7c00ee205772 100644 --- a/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DataModelOperationSpecificConfigTests.cs +++ b/sdk/test/Services/DynamoDBv2/UnitTests/Custom/DataModelOperationSpecificConfigTests.cs @@ -1,11 +1,13 @@ -using Amazon.DynamoDBv2.DataModel; +using Amazon.DynamoDBv2; +using Amazon.DynamoDBv2.DataModel; +using Amazon.DynamoDBv2.Model; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; namespace AWSSDK_DotNet.UnitTests { /// - /// These tests serve as a reminder to developers and reviewers to - /// ensure that new DynamoDB operation-specific properties are plumbed + /// These tests that new DynamoDB operation-specific properties are plumbed /// into the internal code paths correctly /// [TestClass] @@ -18,5 +20,143 @@ public void BaseOperationConfig() // `ToDynamoDBOperationConfig` before updating this unit test Assert.AreEqual(6, typeof(BaseOperationConfig).GetProperties().Length); } + + [TestMethod] + public void BatchGetConfig() + { + // If this fails because you've added a property, be sure to add it to + // `ToDynamoDBOperationConfig` before updating this unit test + Assert.AreEqual(8, typeof(BatchGetConfig).GetProperties().Length); + } + + [TestMethod] + public void BatchGetConfig_OverridesTableName() + { + var mockClient = new Mock(); + mockClient.Setup(client => client.BatchGetItem(It.Is(request => request.RequestItems.ContainsKey("OperationPrefix-TableName")))) + .Returns(new BatchGetItemResponse { Responses = new(), UnprocessedKeys = new() }) + .Verifiable(); + + // Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used + var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig { + TableNamePrefix = "ContextPrefix-", + DisableFetchingTableMetadata = true + }); + + var batchGetConfig = new BatchGetConfig() { TableNamePrefix = "OperationPrefix-" }; + + var batchGet = context.CreateBatchGet(batchGetConfig); + batchGet.AddKey("123"); + batchGet.Execute(); + + // We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown + mockClient.VerifyAll(); + } + + [TestMethod] + public void BatchWriteConfig() + { + // If this fails because you've added a property, be sure to add it to + // `ToDynamoDBOperationConfig` before updating this unit test + Assert.AreEqual(8, typeof(BatchWriteConfig).GetProperties().Length); + } + + [TestMethod] + public void BatchWriteConfig_OverridesTableName() + { + var mockClient = new Mock(); + mockClient.Setup(x => x.BatchWriteItem(It.Is(x => x.RequestItems.ContainsKey("OperationPrefix-TableName")))) + .Returns(new BatchWriteItemResponse { UnprocessedItems = new() }) + .Verifiable(); + + // Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used + var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig + { + TableNamePrefix = "ContextPrefix-", + DisableFetchingTableMetadata = true + }); + + var batchWriteConfig = new BatchWriteConfig() { TableNamePrefix = "OperationPrefix-" }; + + var batchWrite = context.CreateBatchWrite(batchWriteConfig); + batchWrite.AddPutItem(new DataModel { Id = "123" }); + batchWrite.Execute(); + + // We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown + mockClient.VerifyAll(); + } + + [TestMethod] + public void TransactGetConfig() + { + // If this fails because you've added a property, be sure to add it to + // `ToDynamoDBOperationConfig` before updating this unit test + Assert.AreEqual(7, typeof(TransactGetConfig).GetProperties().Length); + } + + [TestMethod] + public void TransactGetConfig_OverridesTableName() + { + var mockClient = new Mock(); + mockClient.Setup(x => x.TransactGetItems(It.Is(x => x.TransactItems[0].Get.TableName == "OperationPrefix-TableName"))) + .Returns(new TransactGetItemsResponse { Responses = new() }) + .Verifiable(); + + // Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used + var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig + { + TableNamePrefix = "ContextPrefix-", + DisableFetchingTableMetadata = true + }); + + var transactGetConfig = new TransactGetConfig() { TableNamePrefix = "OperationPrefix-" }; + + var transactGet = context.CreateTransactGet(transactGetConfig); + transactGet.AddKey("123"); + transactGet.Execute(); + + // We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown + mockClient.VerifyAll(); + } + + [TestMethod] + public void TransactWriteConfig() + { + // If this fails because you've added a property, be sure to add it to + // `ToDynamoDBOperationConfig` before updating this unit test + Assert.AreEqual(7, typeof(TransactWriteConfig).GetProperties().Length); + } + + [TestMethod] + public void TransactWriteConfig_OverridesTableName() + { + var mockClient = new Mock(); + mockClient.Setup(x => x.TransactWriteItems(It.Is(x => x.TransactItems[0].Update.TableName == "OperationPrefix-TableName"))) + .Returns(new TransactWriteItemsResponse()) + .Verifiable(); + + // Set a prefix on the context config, but we'll override it on the operation config so we don't expect it to be used + var context = new DynamoDBContext(mockClient.Object, new DynamoDBContextConfig + { + TableNamePrefix = "ContextPrefix-", + DisableFetchingTableMetadata = true + }); + + var transactWriteConfig = new TransactWriteConfig { TableNamePrefix = "OperationPrefix-" }; + + var transactWrite = context.CreateTransactWrite(transactWriteConfig); + transactWrite.AddSaveItem(new DataModel { Id = "123" }); + transactWrite.Execute(); + + // We expect the setup with the correct prefix to have been called, otherwise an exception would have been thrown + mockClient.VerifyAll(); + } + + [DynamoDBTable("TableName")] + private class DataModel + { + [DynamoDBHashKey] + public string Id { get; set; } + } } }