New functionality : [ReturnOnUpdate] attribute

Hello,

For fields like RowVersion or some computed columns the value can change after an Db.Update(), so can we have an attribute like [ReturnOnInsert] but for update statements ?

Thank you

Can you provide an example of your optimal usage example and how you want it to work please.

// servicemodel
public class MyEntity : IHasId<int>, IReturn<MyEntity>
{
    [AutoIncrement]
    [ReturnOnInsert]
    public int Id { get; set; }

    public string Name { get; set; } = null!;

    [ReturnOnInsert]
    [ReturnOnUpdate]
    public ulong RowVersion { get; set; }
}

// in my service
public async Task<MyEntity> PutAsync(MyEntity request)
{
    await Db.UpdateAsync(request);

    // now request.RowVersion contains the new RowVersion value generated by the database so we can return it directly to the client.

    // without this new [ReturnOnUpdate] attribute, we would need to re-query the database to get the new RowVersion value with something like this :
    // return await Db.SingleByIdAsync<MyEntity>(request.Id);

    return request;
}

// for SQLServer the query would look something like that :
update "MyEntity" set "Name"='newvalue'
output inserted."RowVersion"
where "Id"=1

I don’t know if it’s doable

The Update APIs return the number of rows affected, that’s why I’m looking for what the ideal proposed API should look like?

Not sure if I understand your question.

I don’t think the API should change, just like when you have implemented the [ReturnOnInsert] attribute for the Insert and InsertAsync API

If the [ReturnOnUpdate] is present on some properties, those properties should be refreshed after the update for Update and UpdateAsync API when it updates only 1 POCO

It would be nice if it also worked when you want to update an array of POCO

And maybe you could do that for UpdateOnlyFields and UpdateOnlyFieldsAsync too

Doesn’t look like it’s going to be possible with existing APIs, with a number of issues preventing it:

  • The return type of Update APIs are int to return the rows updated
  • The built-in Optimistic Concurrency support requires returning the number of rows updated to ensure the row version hasn’t been changed.
  • Most of the Update APIs accept an array of objects to update params T[] objs

Ok I had a doubt and I’ve just tested Insert and InsertAll with an array of POCO.
Each POCO containing some [ReturnOnInsert] properties and it doen’t work as I excepted : those ReturnOnInsert properties are not updated in the array after the insert.
Is this a bug ?

Can you show an example of how you’re using it, also the RDBMS you’re using.

Also where did you learn about the feature?

I’m using it for SQL Server.
I’ve learn about it here : SQL Server Features
Also mentioned here : Declarative Dev Model using Attributes

My typical use case is pretty simple :

public class MyEntity : IHasId<int>, IReturn<MyEntity>
{
    [AutoIncrement]
    [ReturnOnInsert]
    public int Id { get; set; }

    public string Name { get; set; } = null!;

    [ReturnOnInsert]
    public ulong RowVersion { get; set; }
}
public class InsertMyEntity : IReturn<MyEntity[]>
{
    public MyEntity[] Items { get; set; } = null!;
}

// in my service
public async Task<MyEntity> PostAsync(MyEntity request)
{
    await Db.InsertAsync(request);
    // working example : here request.Id and request.RowVersion contains the value generated by the database 

    // here i'm also logging inserted POCO in an audit trail
    return request;
}
public async Task<MyEntity[]> PostAsync(InsertMyEntity request)
{
    await Db.InsertAllAsync(request.Items);
    // not working as expected : here none of the POCO in request.Items have their Id and RowVersion updated with the value generated by the database 

    // here i'm also logging inserted POCOs in an audit trail
    return request.Items;
}