-
Notifications
You must be signed in to change notification settings - Fork 12
Configuring Soft Delete
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.
You have seen this in General Overview - configurations, but lets talk about the GetSoftDeleteValue and SetSoftDeleteValue (see code below).
- The
GetSoftDeleteValuecontains an expession that can be used in a LINQ query, and also by the code to read an entity's value. - The
SetSoftDeleteValueis 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.
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
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;
}
}By default, if the ...ViaKeys doesn't find