Skip to content

InsertOnly and UpdateOnly idea #134

@CraftyFella

Description

@CraftyFella

Hey,

So I've been playing with the idea of adding a few extra Update and Insert methods which allow you to pick properties that you want to update, rather that attempting to update all the properties.

The use case would be you want to make an atomic change without fetching the data before hand. Example

user.Name = "Dave";
user.Age = 43;
Assert.True(await connection.UpdateOnlyAsync(user, includedFields: x => x.Age).ConfigureAwait(false)); //returns true if updated, based on tracking
user = await connection.GetAsync<IUser>(id).ConfigureAwait(false);
Assert.Equal("Bob", user.Name); // Name isn't updated
Assert.Equal(43, user.Age);     // Age is updated

It's essentially the same as the existing Update method with the extra includeFields params

params Expression<Func<T, object>>[] includedFields

and the code which currently fetches all the properties from the T is swapped out for something which walks this list..

I have put together a draft PR #133 just so you can see what I mean.. and to get feedback on if this idea is in keeping with the project or not.. also for guidence if it's not as to a way to contirbute back somehow..

The PR only contains an example of UpdateAsync.. but it would be easy enough to add the others.. and also remove the duplication.

Would love to hear you thoughts on this.

Thanks Dave

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions