aws / aws-sdk-net

The official AWS SDK for .NET. For more information on the AWS SDK for .NET, see our web site:

Home Page:http://aws.amazon.com/sdkfornet/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

private static bool CanInstantiateHelper(Type objectType, Type[][] validConstructorInputs)


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 records 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);