DapperLib / Dapper.Contrib

Dapper community contributions - additional extensions for Dapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

InsertOnly and UpdateOnly idea

CraftyFella opened this issue · comments

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

commented

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.