-
Notifications
You must be signed in to change notification settings - Fork 10
General Overview
The two types of soft delete, single soft delete and cascade soft delete, have the same approach - its just that each one uses different types, configuration classes and differently names methods. This overview will walk you though the setting up of a single soft delete, with notes/links to more detailed pages for each setups. The steps are:
-
Your DbContext
- Create your interfaces / entity classes.
- Setup your Query filter.
- Create the Soft Delete configuration
- Registering the Soft Delete Services
- Using the soft delete methods
The Soft Delete services relies on an interface(s) that you create that define want configuration it should use. Here is an example for a single soft delete.
public interface ISingleSoftDelete
{
bool SoftDeleted { get; set; }
}
This interface should then be added to all the entity classes that you want to soft delete. Here is an example entity class.
public class Book : ISingleSoftDelete
{
public int Id { get; set; }
//Other properties left out
public bool SoftDeleted { get; set; }
}
NOTE: If you want to use Cascade Soft Delete the property would be public byte SoftDeleteLevel { get; set; }
- see ICascadeSoftDelete.
Finally, you need to add a Query Filter in your DbContext. There are a number of ways to do this
Manually setting up Query Filters is fine (see code below), but its so easy to forget. Which is why I recommend NOT using this
public class EfCoreContext : DbContext
{
//Other code left out to focus on Soft delete
protected override OnModelCreating(ModelBuilder modelBuilder)
{
//Other configuration left out to focus on Soft delete
modelBuilder.Entity<Book>().HasQueryFilter(p => !p.SoftDeleted);
}
}
There is a way to scan all your entity classes in the OnModelCreating
method and use a extension method to set up the query filter (one is available in the EfCore.SoftDeleteServices repo here). Here is an example DbContext using this approach
public class EfCoreContext : DbContext
{
//Other code left out to focus on Soft delete
protected override OnModelCreating(ModelBuilder modelBuilder)
{
//Other configuration left out to focus on Soft delete
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (typeof(ISingleSoftDelete).IsAssignableFrom(entityType.ClrType))
{
entityType.AddSingleSoftDeleteQueryFilter();
}
}
}
}
NOTE: You can find more versions of the AddSingleSoftDeleteQueryFilter
methods that handle multiple filters in the EfCore.SoftDeleteServices DataLayer.
The Soft Delete configuration is the key to setting up the Soft Delete services. Here is annotated example of a configuration of a single soft delete showing all the main parts
NOTE: See the Configuring page for whichever type of Soft Delete service you need.
Most people will use this library in a application that uses DI (Dependency Injection), so the library contains a extension method that will do this. The code below shows an example of calling this method inside ASP.Net Core’s startup method.
public void ConfigureServices(IServiceCollection services)
{
//other setup code left out
services.RegisterSoftDelServicesAndYourConfigurations(
Assembly.GetAssembly(typeof(ConfigSoftDeleted)));
}
NOTE: See Registering the service for more detailed info.
There are four features that both the single soft delete and cascade soft delete services provide.
- Set the SoftDeleted flag on an entity class, with checks.
- Provides a query to find all the Soft Deleted entities for a specific entity class.
- Reset the SoftDeleted flag on an entity class, which checks.
- Hard delete (i.e. call EF Core
Remove
method) a entity class, but only if it is already Soft Deleted.
Each soft delete type has:
- One method that accesses the entity type via its primary key
- One method that accepts a entity class you loaded (that entity class must be tracked).
That gives you seven methods (the "find all the Soft Deleted" doesn't take anything in). Then, on top of that you have a sync and async version of the Soft Delete service you are using.
NOTE: See the Soft Delete type specific pages for whichever type of Soft Delete service you need.