Skip to content

Commit 2f430a1

Browse files
committed
feat: Create separate operation-specific configs for the object-mapper Delete, Load, and Save operations
1 parent 41f9ab8 commit 2f430a1

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
@@ -390,11 +390,10 @@ public MultiTableTransactWrite CreateMultiTableTransactWrite(params TransactWrit
390390

391391
#region Save/serialize
392392

393-
private void SaveHelper<T>(T value, DynamoDBOperationConfig operationConfig)
393+
private void SaveHelper<T>(T value, DynamoDBFlatConfig flatConfig)
394394
{
395395
if (value == null) return;
396396

397-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
398397
ItemStorage storage = ObjectToItemStorage(value, false, flatConfig);
399398
if (storage == null) return;
400399

@@ -418,16 +417,15 @@ private void SaveHelper<T>(T value, DynamoDBOperationConfig operationConfig)
418417
}
419418

420419
#if AWS_ASYNC_API
421-
private async Task SaveHelperAsync<T>(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
420+
private async Task SaveHelperAsync<T>(T value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
422421
{
423-
await SaveHelperAsync(typeof(T), value, operationConfig, cancellationToken).ConfigureAwait(false);
422+
await SaveHelperAsync(typeof(T), value, flatConfig, cancellationToken).ConfigureAwait(false);
424423
}
425424

426-
private async Task SaveHelperAsync(Type valueType, object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
425+
private async Task SaveHelperAsync(Type valueType, object value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
427426
{
428427
if (value == null) return;
429428

430-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
431429
ItemStorage storage = ObjectToItemStorage(value, valueType, false, flatConfig);
432430
if (storage == null) return;
433431

@@ -452,24 +450,14 @@ await table.UpdateHelperAsync(
452450
}
453451
#endif
454452

455-
/// <summary>
456-
/// Serializes an object to a Document.
457-
/// </summary>
458-
/// <typeparam name="T">Type to serialize as.</typeparam>
459-
/// <param name="value">Object to serialize.</param>
460-
/// <returns>Document with attributes populated from object.</returns>
453+
/// <inheritdoc/>
461454
public Document ToDocument<T>(T value)
462455
{
463-
return ToDocument<T>(value, null);
456+
return ToDocument<T>(value, (ToDocumentConfig)null);
464457
}
465458

466-
/// <summary>
467-
/// Serializes an object to a Document.
468-
/// </summary>
469-
/// <typeparam name="T">Type to serialize as.</typeparam>
470-
/// <param name="value">Object to serialize.</param>
471-
/// <param name="operationConfig">Config object which can be used to override the table used.</param>
472-
/// <returns>Document with attributes populated from object.</returns>
459+
/// <inheritdoc/>
460+
[Obsolete("Use the ToDocument overload that takes ToDocumentConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to ToDocument.")]
473461
public Document ToDocument<T>(T value, DynamoDBOperationConfig operationConfig)
474462
{
475463
if (value == null) return null;
@@ -481,40 +469,48 @@ public Document ToDocument<T>(T value, DynamoDBOperationConfig operationConfig)
481469
return storage.Document;
482470
}
483471

472+
/// <inheritdoc/>
473+
public Document ToDocument<T>(T value, ToDocumentConfig toDocumentConfig)
474+
{
475+
if (value == null) return null;
476+
477+
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(toDocumentConfig?.ToDynamoDBOperationConfig(), Config);
478+
ItemStorage storage = ObjectToItemStorage<T>(value, false, flatConfig);
479+
if (storage == null) return null;
480+
481+
return storage.Document;
482+
}
483+
484484
#endregion
485485

486486
#region Load/deserialize
487487

488-
private T LoadHelper<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig)
488+
private T LoadHelper<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig)
489489
{
490-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
491490
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
492491
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
493492
return LoadHelper<T>(key, flatConfig, storageConfig);
494493
}
495494

496495
#if AWS_ASYNC_API
497-
private Task<T> LoadHelperAsync<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
496+
private Task<T> LoadHelperAsync<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
498497
{
499-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
500498
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
501499
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
502500
return LoadHelperAsync<T>(key, flatConfig, storageConfig, cancellationToken);
503501
}
504502
#endif
505503

506-
private T LoadHelper<T>(T keyObject, DynamoDBOperationConfig operationConfig)
504+
private T LoadHelper<T>(T keyObject, DynamoDBFlatConfig flatConfig)
507505
{
508-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
509506
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
510507
Key key = MakeKey<T>(keyObject, storageConfig, flatConfig);
511508
return LoadHelper<T>(key, flatConfig, storageConfig);
512509
}
513510

514511
#if AWS_ASYNC_API
515-
private Task<T> LoadHelperAsync<T>(T keyObject, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
512+
private Task<T> LoadHelperAsync<T>(T keyObject, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
516513
{
517-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
518514
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
519515
Key key = MakeKey<T>(keyObject, storageConfig, flatConfig);
520516
return LoadHelperAsync<T>(key, flatConfig, storageConfig, cancellationToken);
@@ -555,34 +551,27 @@ private async Task<T> LoadHelperAsync<T>(Key key, DynamoDBFlatConfig flatConfig,
555551
}
556552
#endif
557553

558-
/// <summary>
559-
/// Deserializes a document to an instance of type T.
560-
/// </summary>
561-
/// <typeparam name="T">Type to populate.</typeparam>
562-
/// <param name="document">Document with properties to use.</param>
563-
/// <returns>
564-
/// Object of type T, populated with properties from the document.
565-
/// </returns>
554+
/// <inheritdoc/>
566555
public T FromDocument<T>(Document document)
567556
{
568-
return FromDocument<T>(document, null);
557+
return FromDocument<T>(document, (FromDocumentConfig)null);
569558
}
570559

571-
/// <summary>
572-
/// Deserializes a document to an instance of type T.
573-
/// </summary>
574-
/// <typeparam name="T">Type to populate.</typeparam>
575-
/// <param name="document">Document with properties to use.</param>
576-
/// <param name="operationConfig">Config object which can be used to override the table used.</param>
577-
/// <returns>
578-
/// Object of type T, populated with properties from the document.
579-
/// </returns>
560+
/// <inheritdoc/>
561+
[Obsolete("Use the FromDocument overload that takes FromDocumentConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to FromDocument.")]
580562
public T FromDocument<T>(Document document, DynamoDBOperationConfig operationConfig)
581563
{
582564
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config);
583565
return FromDocumentHelper<T>(document, flatConfig);
584566
}
585567

568+
/// <inheritdoc/>
569+
public T FromDocument<T>(Document document, FromDocumentConfig fromDocumentConfig)
570+
{
571+
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(fromDocumentConfig?.ToDynamoDBOperationConfig(), Config);
572+
return FromDocumentHelper<T>(document, flatConfig);
573+
}
574+
586575
internal T FromDocumentHelper<T>(Document document, DynamoDBFlatConfig flatConfig)
587576
{
588577
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
@@ -592,28 +581,14 @@ internal T FromDocumentHelper<T>(Document document, DynamoDBFlatConfig flatConfi
592581
return instance;
593582
}
594583

595-
/// <summary>
596-
/// Deserializes a collections of documents to a collection of instances of type T.
597-
/// </summary>
598-
/// <typeparam name="T">Type to populate.</typeparam>
599-
/// <param name="documents">Documents to deserialize.</param>
600-
/// <returns>
601-
/// Collection of items of type T, each populated with properties from a corresponding document.
602-
/// </returns>
584+
/// <inheritdoc/>
603585
public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents)
604586
{
605-
return FromDocuments<T>(documents, null);
587+
return FromDocuments<T>(documents, (FromDocumentConfig)null);
606588
}
607589

608-
/// <summary>
609-
/// Deserializes a collections of documents to a collection of instances of type T.
610-
/// </summary>
611-
/// <typeparam name="T">Type to populate.</typeparam>
612-
/// <param name="documents">Documents to deserialize.</param>
613-
/// <param name="operationConfig">Config object which can be used to override the table used.</param>
614-
/// <returns>
615-
/// Collection of items of type T, each populated with properties from a corresponding document.
616-
/// </returns>
590+
/// <inheritdoc/>
591+
[Obsolete("Use the FromDocuments overload that takes FromDocumentConfig instead, since DynamoDBOperationConfig contains properties that are not applicable to FromDocuments.")]
617592
public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents, DynamoDBOperationConfig operationConfig)
618593
{
619594
foreach (var document in documents)
@@ -623,6 +598,16 @@ public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents, DynamoDB
623598
}
624599
}
625600

601+
/// <inheritdoc/>
602+
public IEnumerable<T> FromDocuments<T>(IEnumerable<Document> documents, FromDocumentConfig fromDocumentConfig)
603+
{
604+
foreach (var document in documents)
605+
{
606+
T item = FromDocument<T>(document, fromDocumentConfig);
607+
yield return item;
608+
}
609+
}
610+
626611
internal IEnumerable<T> FromDocumentsHelper<T>(IEnumerable<Document> documents, DynamoDBFlatConfig flatConfig)
627612
{
628613
foreach (var document in documents)
@@ -636,33 +621,30 @@ internal IEnumerable<T> FromDocumentsHelper<T>(IEnumerable<Document> documents,
636621

637622
#region Delete
638623

639-
private void DeleteHelper<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig)
624+
private void DeleteHelper<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig)
640625
{
641-
DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config);
642-
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(config);
643-
Key key = MakeKey(hashKey, rangeKey, storageConfig, config);
626+
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
627+
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
644628

645-
Table table = GetTargetTable(storageConfig, config);
629+
Table table = GetTargetTable(storageConfig, flatConfig);
646630
table.DeleteHelper(key, null);
647631
}
648632

649633
#if AWS_ASYNC_API
650-
private Task DeleteHelperAsync<T>(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
634+
private Task DeleteHelperAsync<T>(object hashKey, object rangeKey, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
651635
{
652-
DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config);
653-
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(config);
654-
Key key = MakeKey(hashKey, rangeKey, storageConfig, config);
636+
ItemStorageConfig storageConfig = StorageConfigCache.GetConfig<T>(flatConfig);
637+
Key key = MakeKey(hashKey, rangeKey, storageConfig, flatConfig);
655638

656-
Table table = GetTargetTable(storageConfig, config);
639+
Table table = GetTargetTable(storageConfig, flatConfig);
657640
return table.DeleteHelperAsync(key, null, cancellationToken);
658641
}
659642
#endif
660643

661-
private void DeleteHelper<T>(T value, DynamoDBOperationConfig operationConfig)
644+
private void DeleteHelper<T>(T value, DynamoDBFlatConfig flatConfig)
662645
{
663646
if (value == null) throw new ArgumentNullException("value");
664647

665-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
666648
flatConfig.IgnoreNullValues = true;
667649
ItemStorage storage = ObjectToItemStorage<T>(value, true, flatConfig);
668650
if (storage == null) return;
@@ -685,11 +667,10 @@ private void DeleteHelper<T>(T value, DynamoDBOperationConfig operationConfig)
685667

686668
private static readonly Task CompletedTask = Task.FromResult<object>(null);
687669

688-
private Task DeleteHelperAsync<T>(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken)
670+
private Task DeleteHelperAsync<T>(T value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
689671
{
690672
if (value == null) throw new ArgumentNullException("value");
691673

692-
DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, this.Config);
693674
flatConfig.IgnoreNullValues = true;
694675
ItemStorage storage = ObjectToItemStorage(value, true, flatConfig);
695676
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)