Skip to content
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

Record Support in DynamoDBContext #3317

Open
2 tasks
danny-zegel-zocdoc opened this issue May 14, 2024 · 4 comments
Open
2 tasks

Record Support in DynamoDBContext #3317

danny-zegel-zocdoc opened this issue May 14, 2024 · 4 comments
Assignees
Labels
dynamodb feature-request A feature should be added or improved. p2 This is a standard priority issue queued

Comments

@danny-zegel-zocdoc
Copy link

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

@danny-zegel-zocdoc danny-zegel-zocdoc added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels May 14, 2024
@ashishdhingra
Copy link
Contributor

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.

@ashishdhingra ashishdhingra added needs-review and removed needs-triage This issue or PR still needs to be triaged. labels May 14, 2024
@ashovlin ashovlin self-assigned this May 17, 2024
@ashovlin
Copy link
Member

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

@danny-zegel-zocdoc
Copy link
Author

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

@ashishdhingra ashishdhingra added p2 This is a standard priority issue and removed needs-review labels May 24, 2024
@bhoradc bhoradc added the queued label May 24, 2024
@chris-richmond
Copy link

I have a similar record defined as @danny-zegel-zocdoc's ActorRole. I was able to load from context, but SaveAsync failed because my object "could not be instantiated".

I attempted to define an explicit default constructor like this:

[DynamoDBTable("actors")]
public record ActorRole(
    [property: DynamoDBHashKey("actor")]
    string? Name = null,
    string? Movie = null,
    string? Role = null,
    DateTime? LastUpdated = null
)
{
    public ActorRole() : this(null!) { }
}

But no luck with that approach either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamodb feature-request A feature should be added or improved. p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

5 participants