-
Notifications
You must be signed in to change notification settings - Fork 878
implement polymorphism support for DynamoDB entries #3643
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
Changes from 3 commits
39f6def
a3510c0
b8ac44c
e1ee970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "services": [ | ||
| { | ||
| "serviceName": "DynamoDBv2", | ||
| "type": "patch", | ||
| "changeLogMessages": [ | ||
| "Implement DynamoDBDerivedTypeAttribute to enable polymorphism support for nested items on save and load data." | ||
| ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,38 @@ public DynamoDBTableAttribute(string tableName, bool lowerCamelCaseProperties) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// DynamoDB attribute that marks a class for polymorphism support. | ||
| /// | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true, AllowMultiple = true)] | ||
| public sealed class DynamoDBPolymorphicTypeAttribute : DynamoDBAttribute | ||
| { | ||
| /// <summary> | ||
| /// Unique name discriminator of the derived type. | ||
| /// </summary> | ||
| public string TypeDiscriminator { get; } | ||
|
|
||
| /// <summary> | ||
| /// Derived type of the Property type. | ||
| /// Type must be a subclass of the Property type. | ||
|
||
| /// </summary> | ||
| [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] | ||
| public Type DerivedType { get; } | ||
|
|
||
| /// <summary> | ||
| /// Construct an instance of DynamoDBPolymorphicTypeAttribute | ||
| /// </summary> | ||
| /// <param name="typeDiscriminator"></param> | ||
| /// <param name="derivedType"></param> | ||
| public DynamoDBPolymorphicTypeAttribute(string typeDiscriminator, | ||
| [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type derivedType) | ||
| { | ||
| TypeDiscriminator = typeDiscriminator; | ||
| DerivedType = derivedType; | ||
| } | ||
|
|
||
| } | ||
| /// <summary> | ||
| /// DynamoDB attribute that directs the specified attribute not to | ||
| /// be included when saving or loading objects. | ||
|
|
@@ -243,6 +275,36 @@ public DynamoDBPropertyAttribute(string attributeName, bool storeAsEpoch) | |
| public bool StoreAsEpoch { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// DynamoDB Polymorphic Property Attribute that marks up current member for polymorphism support. | ||
|
||
| /// Specifies the field name to be used as the type discriminator and the derived types. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = true)] | ||
| public sealed class DynamoDBPolymorphicPropertyAttribute : DynamoDBPropertyAttribute | ||
| { | ||
| /// <summary> | ||
| /// Unique name discriminator of the derived type. | ||
| /// </summary> | ||
| public string TypeDiscriminator { get; } | ||
|
|
||
| /// <summary> | ||
| /// Derived type of the Property type. | ||
| /// Type must be a subclass of the Property type. | ||
| /// </summary> | ||
| public Type DerivedType{ get; } | ||
|
|
||
| /// <summary> | ||
| /// Construct an instance of DynamoDBPolymorphicPropertyAttribute | ||
| /// </summary> | ||
| /// <param name="typeDiscriminator">Name of the field to be used as the type discriminator.</param> | ||
| /// <param name="derivedType">Derived type names and their corresponding types.</param> | ||
| public DynamoDBPolymorphicPropertyAttribute(string typeDiscriminator, Type derivedType) | ||
| { | ||
| TypeDiscriminator = typeDiscriminator; | ||
| DerivedType = derivedType; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// DynamoDB property that marks up current member as a hash key element. | ||
| /// Exactly one member in a class must be marked with this attribute. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,11 @@ public DynamoDBContextConfig() | |
| /// Service calls made via <see cref="AmazonDynamoDBClient"/> will always return | ||
| /// <see cref="DateTime"/> attributes in UTC.</remarks> | ||
| public bool? RetrieveDateTimeInUtc { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Property indicating the name of the attribute used for derived types conversion. | ||
|
||
| /// </summary> | ||
| public string DerivedTypeAttributeName { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -434,6 +439,9 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte | |
| !string.IsNullOrEmpty(operationConfig.IndexName) ? operationConfig.IndexName : DefaultIndexName; | ||
| List<ScanCondition> queryFilter = operationConfig.QueryFilter ?? new List<ScanCondition>(); | ||
| ConditionalOperatorValues conditionalOperator = operationConfig.ConditionalOperator; | ||
| string derivedTypeAttributeName = | ||
| //!string.IsNullOrEmpty(operationConfig.DerivedTypeAttributeName) ? operationConfig.DerivedTypeAttributeName : | ||
| !string.IsNullOrEmpty(contextConfig.DerivedTypeAttributeName) ? contextConfig.DerivedTypeAttributeName : "$type"; | ||
|
|
||
| ConsistentRead = consistentRead; | ||
| SkipVersionCheck = skipVersionCheck; | ||
|
|
@@ -449,6 +457,7 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte | |
| MetadataCachingMode = metadataCachingMode; | ||
| DisableFetchingTableMetadata = disableFetchingTableMetadata; | ||
| RetrieveDateTimeInUtc = retrieveDateTimeInUtc; | ||
| DerivedTypeAttributeName = derivedTypeAttributeName; | ||
|
|
||
| State = new OperationState(); | ||
| } | ||
|
|
@@ -550,6 +559,12 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte | |
| // State of the operation using this config | ||
| internal OperationState State { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Property indicating the name of the attribute used for derived types conversion. | ||
| /// Default value is "$type" if not set in the config. | ||
| /// </summary> | ||
| public string DerivedTypeAttributeName { get; set; } | ||
normj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public class OperationState | ||
| { | ||
| private CircularReferenceTracking referenceTracking; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add more depth to the docs. Explaining how you add multiple declaration of the attribute for each possible subtype and how the TypeDiscriminator is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added extra code comments for docs