Skip to content

Implement Soft Delete Support in CleanArchitecture Template #1329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ejimeoghenefejiro opened this issue May 20, 2025 · 0 comments
Open

Implement Soft Delete Support in CleanArchitecture Template #1329

ejimeoghenefejiro opened this issue May 20, 2025 · 0 comments

Comments

@ejimeoghenefejiro
Copy link

ejimeoghenefejiro commented May 20, 2025

Feature Proposal: Soft Delete Support

Problem:
The current jasontaylordev/CleanArchitecture template uses hard deletes by default, permanently removing data from the database. In real-world enterprise applications, soft deletion is often a critical requirement for:

  • Auditability: Retaining data history for compliance and investigations.
  • Data Recovery: Allowing easy restoration of deleted records.
  • Non-Destructive Workflows: Supporting user deactivation, historical reporting, etc., without losing data.

Adding support for soft delete would align the template with robust data handling practices and long-term maintainability.


Proposed Solution:

To introduce soft delete functionality without compromising the template’s clean architecture:

  1. Create ISoftDelete Interface
    Define an interface with DateTime? Deleted and string? DeletedBy properties to mark entities as soft-deleted.

  2. Update BaseAuditableEntity
    Implement ISoftDelete in BaseAuditableEntity, or create a BaseSoftDeletableEntity if separation is preferred. This allows shared audit and delete functionality.

  3. Apply Global Query Filters
    In ApplicationDbContext, use EF Core’s global filters to automatically exclude soft-deleted entities (Deleted != null) from standard queries.

  4. Override SaveChanges() for Soft Delete
    Intercept EntityState.Deleted entities implementing ISoftDelete and convert them into Modified with deletion metadata set.

  5. Add Hard Delete Option
    Provide an opt-in method (e.g., HardDeleteAsync) for physical deletion when required.

  6. Test Coverage
    Add tests to verify:

    • Soft deletes mark the entity and exclude it from queries
    • Restoring or accessing deleted entities works via .IgnoreQueryFilters()
    • Unique constraints behave correctly with filtered indexes (if needed)

Impact Areas:

  • Domain.Common/ISoftDelete.cs (new)
  • Domain.Common/BaseAuditableEntity.cs (modified)
  • Infrastructure/Data/ApplicationDbContext.cs
  • Application/TodoLists/Commands/DeleteTodoList/ (usage example)

This feature will enhance the CleanArchitecture template’s production readiness and align it with common enterprise data practices.

I’m currently implementing this feature and will submit a pull request shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant