Skip to content
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

Add DynamoDBContext.SaveAsync(Type valueType, object value, ...) #2503

Closed
1 of 2 tasks
ericjpeters opened this issue Dec 20, 2022 · 6 comments
Closed
1 of 2 tasks

Add DynamoDBContext.SaveAsync(Type valueType, object value, ...) #2503

ericjpeters opened this issue Dec 20, 2022 · 6 comments
Labels
dynamodb feature-request A feature should be added or improved. p2 This is a standard priority issue queued

Comments

@ericjpeters
Copy link

ericjpeters commented Dec 20, 2022

Describe the feature

The DynamoDBContext save a SaveAsync(T value, ...) method. There are scenarios where the type is not known at compile-time -- for example, when hooking into EF's ChangeTracker to record audit logs from EF into DynamoDB.

It would be helpful to expose the pre-existing non-generic functionality of DynamoDBContext so that SaveAsync(Type valueType, object value, ...) is possible, as an alternative but equivalent path to SaveAsync(T value, ...) when T is not known at compile-time.

Use Case

There are scenarios where the type is not known at compile-time -- for example, when hooking into EF's ChangeTracker to record audit logs from EF into DynamoDB.

Proposed Solution

I will provide a PR. It's quite trivial.

D:\src\OpenSource\aws-sdk-net [master ≡ +0 ~3 -0 !]> git st
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
        modified:   sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs
        modified:   sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/IDynamoDBContext.Async.cs

no changes added to commit (use "git add" and/or "git commit -a")
D:\src\OpenSource\aws-sdk-net [master ≡ +0 ~3 -0 !]> git diff sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs
diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs
index ff9bb019e14..0527cc8e714 100644
--- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs
+++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/Context.Async.cs
@@ -14,14 +14,11 @@
  */
 #pragma warning disable 1574

+using Amazon.DynamoDBv2.DocumentModel;
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Threading;
 using System.Threading.Tasks;
-using Amazon.DynamoDBv2.DocumentModel;
-using Amazon.DynamoDBv2.Model;
-using Amazon.Runtime.Internal;

 namespace Amazon.DynamoDBv2.DataModel
 {
@@ -60,6 +57,33 @@ namespace Amazon.DynamoDBv2.DataModel
             return SaveHelperAsync(value, operationConfig, cancellationToken);
         }

+        /// <summary>
+        /// Initiates the asynchronous execution of the Save operation.
+        /// <seealso cref="Amazon.DynamoDBv2.DataModel.DynamoDBContext.Save"/>
+        /// </summary>
+        /// <param name="valueType">Type of the Object to save.</param>
+        /// <param name="value">Object to save.</param>
+        /// <param name="cancellationToken">Token which can be used to cancel the task.</param>
+        /// <returns>A Task that can be used to poll or wait for results, or both.</returns>
+        public Task SaveAsync(Type valueType, object value, CancellationToken cancellationToken = default(CancellationToken))
+        {
+            return SaveHelperAsync(valueType, value, null, cancellationToken);
+        }
+
+        /// <summary>
+        /// Initiates the asynchronous execution of the Save operation.
+        /// <seealso cref="Amazon.DynamoDBv2.DataModel.DynamoDBContext.Save"/>
+        /// </summary>
+        /// <param name="valueType">Type of the Object to save.</param>
+        /// <param name="value">Object to save.</param>
+        /// <param name="operationConfig">Overriding configuration.</param>
+        /// <param name="cancellationToken">Token which can be used to cancel the task.</param>
+        /// <returns>A Task that can be used to poll or wait for results, or both.</returns>
+        public Task SaveAsync(Type valueType, object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken))
+        {
+            return SaveHelperAsync(valueType, value, operationConfig, cancellationToken);
+        }
+
         #endregion

         #region Load async
D:\src\OpenSource\aws-sdk-net [master ≡ +0 ~3 -0 !]> git diff sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/IDynamoDBContext.Async.cs
diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/IDynamoDBContext.Async.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/IDynamoDBContext.Async.cs
index 97ef6d9c4d7..ab11c25394d 100644
--- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/IDynamoDBContext.Async.cs
+++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/_async/IDynamoDBContext.Async.cs
@@ -14,12 +14,11 @@
  */
 #pragma warning disable 1574

+using Amazon.DynamoDBv2.DocumentModel;
 using System;
 using System.Collections.Generic;
 using System.Threading;
 using System.Threading.Tasks;
-using Amazon.DynamoDBv2.DocumentModel;
-using Amazon.DynamoDBv2.Model;

 namespace Amazon.DynamoDBv2.DataModel
 {
@@ -52,6 +51,27 @@ namespace Amazon.DynamoDBv2.DataModel
         /// <returns>A Task that can be used to poll or wait for results, or both.</returns>
         Task SaveAsync<T>(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken));

+        /// <summary>
+        /// Initiates the asynchronous execution of the Save operation.
+        /// <seealso cref="Amazon.DynamoDBv2.DataModel.DynamoDBContext.Save"/>
+        /// </summary>
+        /// <param name="valueType">Type of the Object to save.</param>
+        /// <param name="value">Object to save.</param>
+        /// <param name="cancellationToken">Token which can be used to cancel the task.</param>
+        /// <returns>A Task that can be used to poll or wait for results, or both.</returns>
+        Task SaveAsync(Type valueType, object value, CancellationToken cancellationToken = default(CancellationToken));
+
+        /// <summary>
+        /// Initiates the asynchronous execution of the Save operation.
+        /// <seealso cref="Amazon.DynamoDBv2.DataModel.DynamoDBContext.Save"/>
+        /// </summary>
+        /// <param name="valueType">Type of the Object to save.</param>
+        /// <param name="value">Object to save.</param>
+        /// <param name="operationConfig">Overriding configuration.</param>
+        /// <param name="cancellationToken">Token which can be used to cancel the task.</param>
+        /// <returns>A Task that can be used to poll or wait for results, or both.</returns>
+        Task SaveAsync(Type valueType, object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken));
+
         #endregion

         #region Load async
D:\src\OpenSource\aws-sdk-net [master ≡ +0 ~3 -0 !]> git diff sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
diff --git a/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs b/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
index d44902ae66a..c69cf281d98 100644
--- a/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
+++ b/sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
@@ -19,7 +19,6 @@ using System.Threading;
 #if AWS_ASYNC_API
 using System.Threading.Tasks;
 #endif
-using Amazon.DynamoDBv2;
 using Amazon.DynamoDBv2.DocumentModel;

 namespace Amazon.DynamoDBv2.DataModel
@@ -297,6 +296,34 @@ namespace Amazon.DynamoDBv2.DataModel
                 PopulateInstance(storage, value, flatConfig);
             }
         }
+
+        private async Task SaveHelperAsync(Type valueType, object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
+        {
+            if (value == null) return;
+
+            DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
+            ItemStorage storage = ObjectToItemStorage(value, valueType, false, flatConfig);
+            if (storage == null) return;
+
+            Table table = GetTargetTable(storage.Config, flatConfig);
+            if (
+                (flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value)
+                || !storage.Config.HasVersion)
+            {
+                await table.UpdateHelperAsync(storage.Document, table.MakeKey(storage.Document), null, cancellationToken).ConfigureAwait(false);
+            }
+            else
+            {
+                Document expectedDocument = CreateExpectedDocumentForVersion(storage);
+                SetNewVersion(storage);
+                await table.UpdateHelperAsync(
+                    storage.Document,
+                    table.MakeKey(storage.Document),
+                    new UpdateItemOperationConfig { Expected = expectedDocument, ReturnValues = ReturnValues.None },
+                    cancellationToken).ConfigureAwait(false);
+                PopulateInstance(storage, value, flatConfig);
+            }
+        }
 #endif

         /// <summary>

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS .NET SDK and/or Package version used

AWSSDK.DynamoDBv2 v3.7.101.10

Targeted .NET Platform

.NET 7

Operating System and version

Windows 11 22H2

@ericjpeters ericjpeters added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Dec 20, 2022
@ashishdhingra
Copy link
Contributor

Needs review with the team.

@ashishdhingra ashishdhingra added p2 This is a standard priority issue needs-review and removed needs-triage This issue or PR still needs to be triaged. labels Dec 21, 2022
@ericjpeters
Copy link
Author

@ashishdhingra - I'm happy to do more on the PR if somebody can point me to docs on how to get unit tests to build. I've pulled latest master branch in, but the unit tests project is failing to build, and I have not yet figured out what I'm doing wrong.

Let me know if there is anything else I can do to help with this, it's quite needed for my use-case.

@ashishdhingra ashishdhingra added the pr/needs-review This PR needs a review from a Member. label Jan 27, 2023
@normj
Copy link
Member

normj commented Jan 27, 2023

@ericjpeters What are the compiler errors you are seeing for the unit test projects?

@ericjpeters
Copy link
Author

ericjpeters commented Jan 27, 2023 via email

@ashishdhingra ashishdhingra removed the pr/needs-review This PR needs a review from a Member. label Jul 17, 2023
@ashishdhingra
Copy link
Contributor

Corresponding PR #2506 was merged and released. Closing the issue.

Copy link

github-actions bot commented Jun 7, 2024

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamodb feature-request A feature should be added or improved. p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

3 participants