PetaPoco icon indicating copy to clipboard operation
PetaPoco copied to clipboard

Hook into Insert/Update on specific POCOs

Open picasso566 opened this issue 7 years ago • 5 comments

I'm just in the planning stages on a project and a requirement is to save a copy (in an almost duplicate table) of any changes to specific records.

I was thinking that I may hook into the Insert/Update methods to also save the same data into another POCO. Looking at the code I'm not sure what the best approach is for raising and event / subscribing to an observable / etc...

I thought I would ask here in case anyone has already done this using PetaPOCO. (Doesn't look like the OnExecutedCommand event is suitable for this purpose unfortunately)

Thanks, Paul

picasso566 avatar Mar 27 '17 12:03 picasso566

@picasso566 Maybe you could create a wrapper for PetaPoco and intercept any method call you like?

pleb avatar May 04 '17 00:05 pleb

Hmm 🤔

What are your thoughts on making

private object ExecuteInsert(string tableName, string primaryKeyName, bool autoIncrement, object poco)

private int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)

protected virtual?

protected virtual object ExecuteInsert(string tableName, string primaryKeyName, bool autoIncrement, object poco)

protected virtual int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable<string> columns)

This way you could use extend the PetaPoco.Database class and intercept those methods to achieve the insert/update hook.

I can't see any issue with this, and I'm sure it'll be of use to others. If you agree, can you submit a PR and I'll merge it

pleb avatar May 04 '17 00:05 pleb

Thank you for your help on this. I will certainly consider it.

I was thinking I might use it for logging purposes and there might be as better was to go on the sql side. But this may also work.

Paul

On May 4, 2017 09:17, "Wade Baglin" [email protected] wrote:

Hmm 🤔

What are your thoughts on making

private object ExecuteInsert(string tableName, string primaryKeyName, bool autoIncrement, object poco)

private int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable columns)

protected virtual?

protected virtual object ExecuteInsert(string tableName, string primaryKeyName, bool autoIncrement, object poco)

protected virtual int ExecuteUpdate(string tableName, string primaryKeyName, object poco, object primaryKeyValue, IEnumerable columns)

This way you could use extend the PetaPoco.Database class and intercept those methods to achieve the insert/update hook.

I can't see any issue with this, and I'm sure it'll be of use to others. If you agree, can you submit a PR and I'll merge it

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/CollaboratingPlatypus/PetaPoco/issues/380#issuecomment-299070524, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYEgOrHTOECX8sH6eMr8eIsFzgGAms-ks5r2RkogaJpZM4MqK8Q .

picasso566 avatar May 04 '17 00:05 picasso566

While doing this can you make "private AddParam" protected, too. UseCase: If deriving from Database you can implement new execute functionsand use the AddParam function for robust parameter insertion.

Rafael-Dabrowski avatar May 11 '17 18:05 Rafael-Dabrowski

Sounds good

pleb avatar May 15 '17 00:05 pleb