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

InsertOnly and UpdateOnly idea #134

Open
CraftyFella opened this issue Apr 8, 2022 · 1 comment
Open

InsertOnly and UpdateOnly idea #134

CraftyFella opened this issue Apr 8, 2022 · 1 comment

Comments

@CraftyFella
Copy link

CraftyFella commented Apr 8, 2022

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

@PTwr
Copy link

PTwr commented Jul 27, 2022

It might be convenient to have default behavior of not updating fields that have value same as their default, with Expression list being an advanced version.
It would be natural optimization for Insert.

Which of course leads to problem of detecting non-standard default values. Currently Contrib doesn't have equivalent to DefaultValueAttribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants