Skip to content

Commit b49b1ca

Browse files
committed
feat: Create separate operation-specific configs for the object-mapper Delete, Load, and Save operations
1 parent 7dcb4c5 commit b49b1ca

17 files changed

+1107
-517
lines changed

sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,10 @@ public MultiTableTransactWrite CreateMultiTableTransactWrite(params TransactWrit
475475

476476
#region Save/serialize
477477

478-
private void SaveHelper<T>(T value, DynamoDBOperationConfig operationConfig)
478+
private void SaveHelper<T>(T value, DynamoDBFlatConfig flatConfig)
479479
{
480480
if (value == null) return;
481481

482-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
483482
ItemStorage storage = ObjectToItemStorage(value, false, flatConfig);
484483
if (storage == null) return;
485484

@@ -503,16 +502,15 @@ private void SaveHelper<T>(T value, DynamoDBOperationConfig operationConfig)
503502
}
504503

505504
#if AWS_ASYNC_API
506-
private async Task SaveHelperAsync<T>(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
505+
private async Task SaveHelperAsync<T>(T value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
507506
{
508-
await SaveHelperAsync(typeof(T), value, operationConfig, cancellationToken).ConfigureAwait(false);
507+
await SaveHelperAsync(typeof(T), value, flatConfig, cancellationToken).ConfigureAwait(false);
509508
}
510509

511-
private async Task SaveHelperAsync(Type valueType, object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
510+
private async Task SaveHelperAsync(Type valueType, object value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
512511
{
513512
if (value == null) return;
514513

515-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
516514
ItemStorage storage = ObjectToItemStorage(value, valueType, false, flatConfig);
517515
if (storage == null) return;
518516

@@ -537,24 +535,14 @@ await table.UpdateHelperAsync(
537535
}
538536
#endif
539537

540-
/// <summary>
541-
/// Serializes an object to a Document.
542-
/// </summary>
543-
/// <typeparam name="T">Type to serialize as.</typeparam>
544-
/// <param name="value">Object to serialize.</param>
545-
/// <returns>Document with attributes populated from object.</returns>
538+
/// <inheritdoc/>
546539
public Document ToDocument<T>(T value)
547540
{
548-
return ToDocument<T>(value, null);
541+
return ToDocument<T>(value, (ToDocumentConfig)null);
549542
}
550543

551-
/// <summary>
552-
/// Serializes an object to a Document.
553-
/// </summary>
554-
/// <typeparam name="T">Type to serialize as.</typeparam>
555-
/// <param name="value">Object to serialize.</param>
556-
/// <param name="operationConfig">Config object which can be used to override the table used.</param>
557-
/// <returns>Document with attributes populated from object.</returns>
544+
/// <inheritdoc/>
545+
[Obsolete("Use the ToDocument overload that takes ToDocumentConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to ToDocument.")]
558546
public Document ToDocument<T>(T value, DynamoDBOperationConfig operationConfig)
559547
{
560548
if (value == null) return null;
@@ -566,40 +554,48 @@ public Document ToDocument<T>(T value, DynamoDBOperationConfig operationConfig)
566554
return storage.Document;
567555
}
568556

557+
/// <inheritdoc/>
558+
public Document ToDocument<T>(T value, ToDocumentConfig toDocumentConfig)
559+
{
560+
if (value == null) return null;
561+
562+
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(toDocumentConfig?.ToDynamoDBOperationConfig(), Config);
563+
ItemStorage storage = ObjectToItemStorage<T>(value, false, flatConfig);
564+
if (storage == null) return null;
565+
566+
return storage.Document;
567+
}
568+
569569
#endregion
570570

571571
#region Load/deserialize
572572

573-
private T LoadHelper<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig)
573+
private T LoadHelper<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig)
574574
{
575-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
576575
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
577576
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
578577
return LoadHelper<T>(key, flatConfig, storageConfig);
579578
}
580579

581580
#if AWS_ASYNC_API
582-
private Task<T> LoadHelperAsync<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
581+
private Task<T> LoadHelperAsync<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
583582
{
584-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
585583
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
586584
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
587585
return LoadHelperAsync<T>(key, flatConfig, storageConfig, cancellationToken);
588586
}
589587
#endif
590588

591-
private T LoadHelper<T>(T keyObject, DynamoDBOperationConfig operationConfig)
589+
private T LoadHelper<T>(T keyObject, DynamoDBFlatConfig flatConfig)
592590
{
593-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
594591
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
595592
Key key = MakeKey<T>(keyObject, storageConfig, flatConfig);
596593
return LoadHelper<T>(key, flatConfig, storageConfig);
597594
}
598595

599596
#if AWS_ASYNC_API
600-
private Task<T> LoadHelperAsync<T>(T keyObject, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
597+
private Task<T> LoadHelperAsync<T>(T keyObject, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
601598
{
602-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
603599
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
604600
Key key = MakeKey<T>(keyObject, storageConfig, flatConfig);
605601
return LoadHelperAsync<T>(key, flatConfig, storageConfig, cancellationToken);
@@ -640,34 +636,27 @@ private async Task<T> LoadHelperAsync<T>(Key key, DynamoDBFlatConfig flatConfig,
640636
}
641637
#endif
642638

643-
/// <summary>
644-
/// Deserializes a document to an instance of type T.
645-
/// </summary>
646-
/// <typeparam name="T">Type to populate.</typeparam>
647-
/// <param name="document">Document with properties to use.</param>
648-
/// <returns>
649-
/// Object of type T, populated with properties from the document.
650-
/// </returns>
639+
/// <inheritdoc/>
651640
public T FromDocument<T>(Document document)
652641
{
653-
return FromDocument<T>(document, null);
642+
return FromDocument<T>(document, (FromDocumentConfig)null);
654643
}
655644

656-
/// <summary>
657-
/// Deserializes a document to an instance of type T.
658-
/// </summary>
659-
/// <typeparam name="T">Type to populate.</typeparam>
660-
/// <param name="document">Document with properties to use.</param>
661-
/// <param name="operationConfig">Config object which can be used to override the table used.</param>
662-
/// <returns>
663-
/// Object of type T, populated with properties from the document.
664-
/// </returns>
645+
/// <inheritdoc/>
646+
[Obsolete("Use the FromDocument overload that takes FromDocumentConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to FromDocument.")]
665647
public T FromDocument<T>(Document document, DynamoDBOperationConfig operationConfig)
666648
{
667649
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config);
668650
return FromDocumentHelper<T>(document, flatConfig);
669651
}
670652

653+
/// <inheritdoc/>
654+
public T FromDocument<T>(Document document, FromDocumentConfig fromDocumentConfig)
655+
{
656+
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(fromDocumentConfig?.ToDynamoDBOperationConfig(), Config);
657+
return FromDocumentHelper<T>(document, flatConfig);
658+
}
659+
671660
internal T FromDocumentHelper<T>(Document document, DynamoDBFlatConfig flatConfig)
672661
{
673662
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
@@ -677,28 +666,14 @@ internal T FromDocumentHelper<T>(Document document, DynamoDBFlatConfig flatConfi
677666
return instance;
678667
}
679668

680-
/// <summary>
681-
/// Deserializes a collections of documents to a collection of instances of type T.
682-
/// </summary>
683-
/// <typeparam name="T">Type to populate.</typeparam>
684-
/// <param name="documents">Documents to deserialize.</param>
685-
/// <returns>
686-
/// Collection of items of type T, each populated with properties from a corresponding document.
687-
/// </returns>
669+
/// <inheritdoc/>
688670
public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents)
689671
{
690-
return FromDocuments<T>(documents, null);
672+
return FromDocuments<T>(documents, (FromDocumentConfig)null);
691673
}
692674

693-
/// <summary>
694-
/// Deserializes a collections of documents to a collection of instances of type T.
695-
/// </summary>
696-
/// <typeparam name="T">Type to populate.</typeparam>
697-
/// <param name="documents">Documents to deserialize.</param>
698-
/// <param name="operationConfig">Config object which can be used to override the table used.</param>
699-
/// <returns>
700-
/// Collection of items of type T, each populated with properties from a corresponding document.
701-
/// </returns>
675+
/// <inheritdoc/>
676+
[Obsolete("Use the FromDocuments overload that takes FromDocumentConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to FromDocuments.")]
702677
public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents, DynamoDBOperationConfig operationConfig)
703678
{
704679
foreach (var document in documents)
@@ -708,6 +683,16 @@ public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents, DynamoDB
708683
}
709684
}
710685

686+
/// <inheritdoc/>
687+
public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents, FromDocumentConfig fromDocumentConfig)
688+
{
689+
foreach (var document in documents)
690+
{
691+
T item = FromDocument<T>(document, fromDocumentConfig);
692+
yield return item;
693+
}
694+
}
695+
711696
internal IEnumerable<T> FromDocumentsHelper<T>(IEnumerable<Document> documents, DynamoDBFlatConfig flatConfig)
712697
{
713698
foreach (var document in documents)
@@ -721,33 +706,30 @@ internal IEnumerable<T> FromDocumentsHelper<T>(IEnumerable<Document> documents,
721706

722707
#region Delete
723708

724-
private void DeleteHelper<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig)
709+
private void DeleteHelper<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig)
725710
{
726-
DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config);
727-
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(config);
728-
Key key = MakeKey(hashKey, rangeKey, storageConfig, config);
711+
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
712+
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
729713

730-
Table table = GetTargetTable(storageConfig, config);
714+
Table table = GetTargetTable(storageConfig, flatConfig);
731715
table.DeleteHelper(key, null);
732716
}
733717

734718
#if AWS_ASYNC_API
735-
private Task DeleteHelperAsync<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
719+
private Task DeleteHelperAsync<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
736720
{
737-
DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config);
738-
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(config);
739-
Key key = MakeKey(hashKey, rangeKey, storageConfig, config);
721+
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
722+
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
740723

741-
Table table = GetTargetTable(storageConfig, config);
724+
Table table = GetTargetTable(storageConfig, flatConfig);
742725
return table.DeleteHelperAsync(key, null, cancellationToken);
743726
}
744727
#endif
745728

746-
private void DeleteHelper<T>(T value, DynamoDBOperationConfig operationConfig)
729+
private void DeleteHelper<T>(T value, DynamoDBFlatConfig flatConfig)
747730
{
748731
if (value == null) throw new ArgumentNullException("value");
749732

750-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
751733
flatConfig.IgnoreNullValues = true;
752734
ItemStorage storage = ObjectToItemStorage<T>(value, true, flatConfig);
753735
if (storage == null) return;
@@ -770,11 +752,10 @@ private void DeleteHelper<T>(T value, DynamoDBOperationConfig operationConfig)
770752

771753
private static readonly Task CompletedTask = Task.FromResult<object>(null);
772754

773-
private Task DeleteHelperAsync<T>(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
755+
private Task DeleteHelperAsync<T>(T value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
774756
{
775757
if (value == null) throw new ArgumentNullException("value");
776758

777-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
778759
flatConfig.IgnoreNullValues = true;
779760
ItemStorage storage = ObjectToItemStorage(value, true, flatConfig);
780761
if (storage == null) return CompletedTask;

sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,9 @@ internal static Expression CreateConditionExpressionForVersion(ItemStorage stora
121121
#region Table methods
122122

123123
// Retrieves the target table for the specified type
124-
private Table GetTargetTableInternal<T>(DynamoDBOperationConfig operationConfig)
124+
private Table GetTargetTableInternal<T>(DynamoDBFlatConfig flatConfig)
125125
{
126126
Type type = typeof(T);
127-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
128127
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig(type, flatConfig);
129128
Table table = GetTargetTable(storageConfig, flatConfig, Table.DynamoDBConsumer.DocumentModel);
130129
return table;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
namespace Amazon.DynamoDBv2.DataModel
17+
{
18+
/// <summary>
19+
/// Input for the Delete operation in the object-persistence programming model
20+
/// </summary>
21+
#if NET8_0_OR_GREATER
22+
// The DataModel namespace doesn't support trimming yet, so annotate public classes/methods as incompatible
23+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)]
24+
#endif
25+
public class DeleteConfig : BaseOperationConfig
26+
{
27+
/// <summary>
28+
/// Property that directs <see cref="DynamoDBContext"/> to skip version checks
29+
/// when saving or deleting an object with a version attribute.
30+
/// If property is not set, version checks are performed.
31+
/// </summary>
32+
public bool? SkipVersionCheck { get; set; }
33+
34+
/// <inheritdoc/>
35+
internal override DynamoDBOperationConfig ToDynamoDBOperationConfig()
36+
{
37+
var config = base.ToDynamoDBOperationConfig();
38+
config.SkipVersionCheck = SkipVersionCheck;
39+
40+
return config;
41+
}
42+
}
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace Amazon.DynamoDBv2.DataModel
19+
{
20+
/// <summary>
21+
/// Input for the FromDocument operation in the object-persistence programming model
22+
/// </summary>
23+
#if NET8_0_OR_GREATER
24+
// The DataModel namespace doesn't support trimming yet, so annotate public classes/methods as incompatible
25+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(Custom.Internal.InternalConstants.RequiresUnreferencedCodeMessage)]
26+
#endif
27+
public class FromDocumentConfig : BaseOperationConfig
28+
{
29+
/// <summary>
30+
/// If true, all <see cref="DateTime"/> properties are retrieved in UTC timezone while reading data from DynamoDB. Else, the local timezone is used.
31+
/// </summary>
32+
/// <remarks>
33+
/// This setting is only applicable to the high-level library. Service calls made via
34+
/// <see cref="AmazonDynamoDBClient"/> will always return <see cref="DateTime"/> attributes in UTC.
35+
/// </remarks>
36+
public bool? RetrieveDateTimeInUtc { get; set; }
37+
38+
/// <inheritdoc/>
39+
internal override DynamoDBOperationConfig ToDynamoDBOperationConfig()
40+
{
41+
var config = base.ToDynamoDBOperationConfig();
42+
config.RetrieveDateTimeInUtc = RetrieveDateTimeInUtc;
43+
44+
return config;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)