Intercept and update timestamps for all documents

I need to automatically set and update CreatedAt and UpdatedAt timestamps for all documents that is inserted and updated to the database without having to manually set them in code each time I update a document (c# driver).

I tried to implement an IEventSubscriber (CommandStartedEvent), but it seems that it the documents are immutable.

Is it possible to hook into the pipeline, like with IEventsubscriber, and update an UpdateAt timestamp on updates and setting a CreatedAt timestamp on inserts?

Unfortunately the .NET/C# Driver does not provide interception capabilities when persisting data. You would have to implement this logic in your domain classes, possibly some base entity. CreatedAt is simple enough to implement as it can be set in the constructor. ModifiedAt would require wiring up a property changed mechanism so it would be updated when the object’s data changed.

1 Like

Thank you.
Then I’ll stop trying to get it to work with intercepters and do a more direct approach

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.