Skip to content

Configuring Soft Delete

Jon P Smith edited this page Jan 12, 2021 · 8 revisions

The General Overview - configurations gave you an overview of how to create a configuration, and the Multiple Query filters told you how to handle Query Filters with multiple filter parts. This just focuses on setting the properties that read/write the soft deleted part of your entity.

Normal use

You have seen this in General Overview - configurations, but lets talk about the GetSoftDeleteValue and SetSoftDeleteValue (see code below).

  • The GetSoftDeleteValue contains an expession that can be used in a LINQ query, and also by the code to read an entity's value.
  • The SetSoftDeleteValue is an expression that the code can use to set the entity's value
public class ConfigSingleSoftDelete : SingleSoftDeleteConfiguration<ISingleSoftDelete>
{

    public ConfigSingleSoftDelete (SingleSoftDelDbContext context)
        : base(context)
    {
        GetSoftDeleteValue = entity => entity.SoftDeleted;
        SetSoftDeleteValue = (entity, value) => { entity.SoftDeleted = value; };
    }
}

The SoftDeleted is a 'bool' property, but it could be part of a [Flags] enum or something else. You can see an example of a configuration working with a Domain-Driven Design here.

If this was a cascade configuration (with extra IUserId handling) it would look like this

public class ConfigCascadeDeleteWithUserId : CascadeSoftDeleteConfiguration<ICascadeSoftDelete>
{

    public ConfigCascadeDeleteWithUserId(CascadeSoftDelDbContext context)
        : base(context)
    {
        GetSoftDeleteValue = entity => entity.SoftDeleteLevel;
        SetSoftDeleteValue = (entity, value) => { entity.SoftDeleteLevel = value; };
        OtherFilters.Add(typeof(IUserId), entity => ((IUserId)entity).UserId == context.UserId);
    }
}

Note that the the SoftDeleteLevel is a byte, not a bool

Using shadow properties for the soft delete value

The configuration seen above doesn't work with a shadow property, because you need a different expression for a query and the code accessing the value. To make this work you need to use the optional QuerySoftDeleteValue property in the configuration class.

public class ConfigSoftDeleteShadowDel : SingleSoftDeleteConfiguration<IShadowSoftDelete>
{
    public ConfigSoftDeleteShadowDel(SingleSoftDelDbContext context)
        : base(context)
    {
        GetSoftDeleteValue = entity => 
            (bool)context.Entry(entity).Property("SoftDeleted").CurrentValue;
        QuerySoftDeleteValue = entity => EF.Property<bool>(entity, "SoftDeleted");
        SetSoftDeleteValue = (entity, value) => 
            context.Entry(entity).Property("SoftDeleted").CurrentValue = value;
    }
}

Other configuration properties

NotFoundIsNotAnError

By default, if the ...ViaKeys doesn't find

Clone this wiki locally