Record Support in DynamoDBContext
danny-zegel-zocdoc opened this issue · comments
Describe the feature
Currently DynamoDBContext
supports class types for the object model type parameter but does not seem to work with record types. It would be great if it would support record types as well.
Use Case
Using record types with DynamoDBContext.
Proposed Solution
No response
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
Targeted .NET Platform
.NET 6 and up
Operating System and version
Any
Per Create record types, Records are types that use value-based equality. This is opposite of class types which use reference based equality. Unsure if record types would fit into object based model. Needs review with the team.
Is it record struct
support that you're interested in? I do see those getting stopped in our CanInstantiateHelper
:
This seemed to work for me for record class
, though didn't test exhaustively.
[DynamoDBTable("actors", lowerCamelCaseProperties:true)]
public record class ActorRole
{
[DynamoDBHashKey("actor")]
public string Name { get; set; }
public string Movie { get; set; }
public string Role { get; set; }
public DateTime LastUpdated { get; set; }
}
static async Task Main(string[] args)
{
var client = new AmazonDynamoDBClient();
IDynamoDBContext context = new DynamoDBContext(client);
var actor = await context.LoadAsync<ActorRole>("Tom Hanks", "Toy Story");
Console.WriteLine(actor.Role);
actor.LastUpdated = DateTime.UtcNow;
await context.SaveAsync(actor);
var document = context.ToDocument<ActorRole>(actor);
foreach (var value in document)
{
Console.WriteLine(value.Key + " " + value.Value);
}
var query = context.QueryAsync<ActorRole>("Tom Hanks");
foreach (var item in await query.GetRemainingAsync())
{
Console.WriteLine(item.Name + " " + item.Movie + " " + item.LastUpdated);
}
}
no i had trouble with regular record
s but I wrote them with constructor properties like this
[DynamoDBTable("actors", lowerCamelCaseProperties:true)]
public record ActorRole(
[property: DynamoDBHashKey("actor")] string Name,
string Movie,
string Role,
DateTime LastUpdated);