Handling Deletions within the Repository Pattern Using a SQL Backend

In my C# implementation, I manage Post objects with comment lists using stored procedures for data mapping. Adding comments works well, but removals are problematic. What strategies effectively detect and process deleted items?

In my experience, a viable solution is to separate the deletion detection from the main update cycle by working with a soft delete mechanism. Instead of directly removing comments from your main list, mark them as deleted using a dedicated flag. This allows your stored procedure to determine which records need actual deletion during a subsequent transaction. This two-step approach not only increases clarity regarding which items have been removed but also reduces the risk of unintentional data loss. A robust logging process further enhances transparency, ensuring that all changes remain auditable.

hey, have you tried using rowversion or timestamp cols for detecting changes? maby it could help in flagging removed comments on the fly without extra checks. what do u think about its effect on concurrent updates?

maybe try sql server’s change tracking. i found it captures removals directly so you can sync your in-mem snapshot without extra diff logic. it’s handy and less error prone in concurrent scenarios

hey, consider using sql triggers to log deletions realtime. that way, you catch removals as they occur rather than waiting on a separate cleanup phase. it worked wel in my last project

In my experience, a more robust strategy involves integrating deletion tracking at the domain level prior to the persistence stage. By incorporating change-tracking within your business logic, you capture the intended removals in a controlled environment before updating the SQL backend. This allows you to compare the current list of comments with a recorded snapshot of the previous state and identify deletions. This method not only minimizes accidental data loss but also helps maintain consistent synchronization between your in-memory objects and the database.