How to implement Soft Delete In C#

i’m using C# driver
i want to implement Soft Delete, like global query filter in EF Core
to automatically add some filters to fetching data commands
anyway?

another thing is important is changing modificationDate property when the document is updated

Hi, @ali_yeganeh,

Welcome to the MongoDB Community Forums. I understand that you have some questions about implementing soft deletes with MongoDB.

You could implement soft deletes via a data layer abstraction. You could expose an IRepository<T> implementation in your data layer that returns an IQueryable<T> to your application. Rather than returning coll.AsQueryable(), you would return coll.AsQueryable().Where(x => x.Deleted == false).

If you are running on MongoDB Atlas, another way to implement soft deletes would be to leverage Atlas App Services. You could implement the soft delete logic using query filters:

Regarding your second requirement of implementing a modificationDate, that would be easiest to implement in your application logic whereby you would modify a ModificationDate property on your base entity when the entity is modified. This could be done manually in every property or using an INotifyPropertyChanged implementation. Other options include implementing it in your Repository<T> implementation on update/delete. Yet another would be using Atlas App Services rule to set the modificationDate when updating or (soft) deleting an entity.

Hopefully this provides you with some ideas for how to accomplish your design goals.

Sincerely,
James

2 Likes

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