@@ -71,6 +71,91 @@ public DynamoDBTableAttribute(string tableName, bool lowerCamelCaseProperties)
71
71
}
72
72
}
73
73
74
+ /// <summary>
75
+ /// DynamoDB attribute that marks a class or property for polymorphism support.
76
+ /// This allows DynamoDB to store and retrieve instances of derived types
77
+ /// while preserving their original types during serialization and deserialization.
78
+ /// </summary>
79
+ /// <remarks>
80
+ /// To enable polymorphic serialization, this attribute should be applied multiple times,
81
+ /// once for each possible subtype. The <see cref="TypeDiscriminator"/> serves as a unique
82
+ /// identifier used in the database to distinguish between different derived types.
83
+ ///
84
+ /// The name of the stored discriminator attribute in DynamoDB can be configured via
85
+ /// <see cref="DynamoDBContextConfig.DerivedTypeAttributeName"/>.
86
+ /// If not explicitly set, the SDK will use a default attribute name for the discriminator.
87
+ /// </remarks>
88
+ [ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Struct | AttributeTargets . Field | AttributeTargets . Property , Inherited = true , AllowMultiple = true ) ]
89
+ public sealed class DynamoDBPolymorphicTypeAttribute : DynamoDBAttribute
90
+ {
91
+ /// <summary>
92
+ /// Unique name discriminator of the derived type.
93
+ /// </summary>
94
+ /// <remarks>
95
+ /// The <see cref="TypeDiscriminator"/> is stored in DynamoDB and is used to
96
+ /// determine the actual type of the object when deserializing.
97
+ /// It should be unique among all declared polymorphic types for a given base type.
98
+ ///
99
+ /// The attribute name under which this discriminator is stored in DynamoDB
100
+ /// is configurable via <see cref="DynamoDBContextConfig.DerivedTypeAttributeName"/>.
101
+ ///
102
+ /// Example:
103
+ /// <code>
104
+ /// [DynamoDBPolymorphicType("dog", typeof(Dog))]
105
+ /// [DynamoDBPolymorphicType("cat", typeof(Cat))]
106
+ /// public class Animal { }
107
+ /// </code>
108
+ ///
109
+ /// When retrieving an item, DynamoDB will use this discriminator value to
110
+ /// deserialize into the appropriate derived type.
111
+ /// </remarks>
112
+ public string TypeDiscriminator { get ; }
113
+
114
+ /// <summary>
115
+ /// The specific derived type that corresponds to this polymorphic entry.
116
+ /// </summary>
117
+ /// <remarks>
118
+ /// This should be a subclass of the property type where the attribute is applied.
119
+ /// When an instance of this type is stored in DynamoDB, the <see cref="TypeDiscriminator"/>
120
+ /// will be saved along with it, allowing correct type resolution during deserialization.
121
+ /// </remarks>
122
+ [ DynamicallyAccessedMembers ( InternalConstants . DataModelModeledType ) ]
123
+ public Type DerivedType { get ; }
124
+
125
+ /// <summary>
126
+ /// Constructs an instance of <see cref="DynamoDBPolymorphicTypeAttribute"/>.
127
+ /// </summary>
128
+ /// <param name="typeDiscriminator">
129
+ /// A unique string identifier representing this derived type.
130
+ /// This value is stored in DynamoDB and used for deserialization.
131
+ /// </param>
132
+ /// <param name="derivedType">
133
+ /// The actual derived type that this attribute represents.
134
+ /// Must be a subclass of the property type to which it is applied.
135
+ /// </param>
136
+ /// <example>
137
+ /// Usage for a polymorphic property:
138
+ /// <code>
139
+ /// public class Zoo
140
+ /// {
141
+ /// [DynamoDBPolymorphicType("dog", typeof(Dog))]
142
+ /// [DynamoDBPolymorphicType("cat", typeof(Cat))]
143
+ /// public Animal Resident { get; set; }
144
+ /// }
145
+ /// </code>
146
+ ///
147
+ /// In this example, when a `Dog` instance is stored, DynamoDB will save `"dog"` as its discriminator
148
+ /// under the attribute name configured in <see cref="DynamoDBContextConfig.DerivedTypeAttributeName"/>.
149
+ /// When retrieved, the stored discriminator ensures that the value is deserialized as a `Dog` instance.
150
+ /// </example>
151
+ public DynamoDBPolymorphicTypeAttribute ( string typeDiscriminator ,
152
+ [ DynamicallyAccessedMembers ( InternalConstants . DataModelModeledType ) ] Type derivedType )
153
+ {
154
+ TypeDiscriminator = typeDiscriminator ;
155
+ DerivedType = derivedType ;
156
+ }
157
+ }
158
+
74
159
/// <summary>
75
160
/// DynamoDB attribute that directs the specified attribute not to
76
161
/// be included when saving or loading objects.
0 commit comments