POST/CON Postman's biggest API conference ever from April 30 - May 1 in San Francisco, California! And right now, the Postman team is offering 30% off tickets until March 26.
POST/CON 24 is for anyone who works with APIs or whose business relies on APIs. If you attend, you'll earn a certificate and a Postman Badge!
ABP Framework is a complete infrastructure for creating software solutions with modern architectures based on the ASP.NET Core platform. It comes with production-ready components, modular architecture, and Domain-Driven Design.
To delete or not to delete, that is the question (pun intended).
The traditional way to remove information in a database is through a "hard delete." A hard delete permanently erases a record from the database table. While this seems straightforward, it presents a significant risk: once that data is gone, it's gone for good.
Instead of physically removing a record, a soft delete marks it as deleted, usually by setting a flag like IsDeleted to true. The record remains in the database, but it's effectively hidden from regular application queries.
Today, we'll dive into the details of how to implement soft deletes using EF Core. We'll discuss global query filters, explore efficient ways to handle soft-deleted data, and weigh the trade-offs.
What Is a Soft Delete?
A soft delete is a data persistence strategy that prevents the permanent deletion of records from your database. Instead of removing data from the database, a flag is set on the record, indicating it as "deleted."
This approach allows the application to ignore these records during normal queries. However, you can restore these records if necessary. Soft delete is also practical if you want to keep foreign key constraints in place. Soft delete is a "non-destructive" operation in contrast with hard delete, where data is completely removed from the database.
A hard delete uses the SQL DELETE statement:
DELETEFROM bookings.Reviews
WHERE Id =@BookingId;
A soft delete, on the other hand, uses an UPDATE statement:
UPDATE bookings.Reviews
SET IsDeleted =1, DeletedOnUtc =@UtcNowWHERE Id =@BookingId;
The data is still present in the database, and the operation can be undone.
But you need to remember to filter out soft-deleted data when querying the database:
SELECT*FROM bookings.Reviews
WHERE IsDeleted =0;
Let's see how we can implement soft delete with EF Core.
Soft Deletes Using EF Core Interceptors
EF Core interceptors provide a powerful mechanism for intercepting and modifying database operations. For example, you can intercept the saving changes operation to implement soft delete functionality.
Let's create an ISoftDeletable marker interface to represent soft-deletable entities:
The entities that should support soft delete will implement this interface. You will need to apply the respective database migration to create these columns.
The next component we need is a SaveChangesInterceptor, which allows us to hook into the SavingChangesAsync (or SavingChanges) method. We can access the ChangeTracker and look for entries that implement ISoftDeletable and are flagged for deletion. We can figure this out by checking if the entity state is EntityState.Deleted.
When we find the entities flagged for deletion, we loop through them and update their state to EntityState.Modified. You should also set the respective values for the IsDeleted and DeletedOnUtc properties. This will cause EF to generate an UPDATE operation instead of a DELETE operation.
If you want to learn more, here's an article with a few practical use cases for EF Core interceptors.
Automatically Filtering Soft-Deleted Data
To ensure that soft-deleted records are automatically excluded from queries, we can use EF Core global query filters. We can apply query filters to entities using the OnModelCreating method to automatically exclude records marked as deleted. This feature dramatically simplifies writing queries.
Here's how to configure the soft delete query filter:
To enhance query performance, especially in tables with a significant number of soft-deleted records, you can use filtered indexes. A filtered index only includes records that meet the specified criteria. This reduces the index size and improves query execution times for operations that exclude filtered records. Most popular databases support filtered indexes.
It's worthwhile to think through if you even need to soft delete records.
In enterprise systems, you're typically not thinking about "deleting" data. There are business concepts that don't involve deleting data. A few examples are canceling an order, refunding a payment, or voiding an invoice. These "destructive" operations return the system to a previous state. But from a business perspective, you aren't really deleting data.
Soft deletes are helpful if there is a risk of accidental deletion. They allow you to easily restore soft-deleted records.
In any case, consider if soft deletes make sense from a business perspective.
Takeaway
Soft deletes offer a valuable safety net for data recovery and can enhance historical data tracking. However, it's crucial to assess whether they truly align with your application's specific requirements. Consider factors like the importance of deleted data recovery, any auditing needs, and your industry's regulations. Creating a filtered index can improve query performance on tables with soft-deleted records.
If you decide that soft deletes are a good fit, EF Core provides the tools necessary for a streamlined implementation.
P.S. There are 2 ways I can help you improve your skills:
Modular Monolith Architecture
This in-depth course will transform the way you build monolith systems. You will learn the best practices for applying the Modular Monolith architecture in a real-world scenario.
This is the complete blueprint for building production-ready applications using Clean Architecture. Join 2,400+ other students to accelerate your growth as a software architect.
I love hearing from readers, and I'm always looking for feedback. How am I doing with The .NET Weekly? Is there anything you'd like to see more or less of? Which aspects of the newsletter do you enjoy the most?
Hit reply and say hello - I'd love to hear from you!
Stay awesome,
Milan
You received this email because you subscribed to our list. You can unsubscribe at any time.
What Is a Modular Monolith? Read on: my website / Read time: 6 minutes BROUGHT TO YOU BY Extend your existing Blazor Applications to Desktop and Mobile Bring the best of web UI to native client
Lightweight In-Memory Message Bus Using .NET Channels Read on: my website / Read time: 6 minutes BROUGHT TO YOU BY Iron Software's Suite Expansion: Unveiling 9 Developer Libraries. Iron Software
Automatically Register Minimal APIs in ASP.NET Core Read on: milanjovanovic.tech Read time: 4 minutes The .NET Weekly is brought to you by: Become a Postman master with Postman Intergalactic
Using Scoped Services From Singletons in ASP.NET Core Read on: milanjovanovic.tech Read time: 3 minutes The .NET Weekly is brought to you by: Become a Postman master with Postman
Getting the Current User in Clean Architecture Read on: milanjovanovic.tech Read time: 4 minutes The .NET Weekly is brought to you by: Blending AI with UI: Crafting LLM UIs in Blazor,
Edition 165 of my newsletter, discussing Knowledge Management, Knowledge Work, Zen Productivity, Personal Organization, and more! Sébastien Dubois DeveloPassion's Newsletter DeveloPassion's
Implementing API Gateway Authentication With YARP Read on: my website / Read time: 5 minutes BROUGHT TO YOU BY Supercharging Development With AI and APIs Announcing Postman v11: Streamline API
Unit, Integration and End-to-End Tests 🔧 View on the Web Archives ISSUE 218 May 4th 2024 COMMENT Welcome to the 218th issue! I loved going through this discussion among software engineers: What is your
Plus: Med-Gemini; Vidu - Chinese answer to OpenAI's Sora; the first race of Abu Dhabi Autonomous Racing League; deepfaking celebrities to teach math and physics; and more! ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
Plus: Fisker's legal woes and Sprinklr lays off 100 View this email online in your browser By Christine Hall Friday, May 3, 2024 Good afternoon, and welcome to TechCrunch PM. We made it to Friday,
Also: Smart Home Mistakes to Avoid, and More! How-To Geek Logo May 3, 2024 Did You Know Half of the world's geysers are located in Yellowstone National Park. 🔑 More Passkeys Happy Friday! You can
JSK Daily for May 3, 2024 View this email in your browser A community curated daily e-mail of JavaScript news The Power of React's Virtual DOM: A Comprehensive Explanation Modern JavaScript
Also, is TikTok dodging Apple's commissions? View this email online in your browser By Haje Jan Kamps Friday, May 3, 2024 Welcome to Startups Weekly — Haje's weekly recap of everything you can
Weekly articles & videos about people, culture and leadership: everything you need to design the org that makes the product. A weekly newsletter by Oren Ellenbogen with the best content I found
Is Swift 6 hitting one of the REAL hard problems? Not generics, not data race safety, but naming things! 😬 View on the Web Archives ISSUE 659 May 3rd 2024 Comment Naming things is one of the two hard