Skip to content

Single Soft Delete

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

The Single Soft Delete methods have seven methods, as shown below with there comments. As you can see the methods that do something return a IStatusGeneric<int>, which can return

  • Success:
    • The status.IsValid is true
    • The status.Result holds a number saying how many entities were changed - 1 for soft delete, could be more for hard delete (0 means none)
    • The status.Message has a user-friendly success message, e.g. "Successfully soft deleted this entry".
  • Failure
    • The status.IsValid is false
    • The status.Result will be 0
    • The status.GetAllErrors returns an error, e.g. "Could not find the entry you ask for."

See GenericServices.StatusGeneric for more about how the status works.

The methods

Here are the sync methods. The async versions are the same, but return Task<IStatusGeneric<int>> and have Async at the end of the method name.

/// <summary>
/// This finds the entity using its primary key(s) and then set the single soft delete flag so it is hidden
/// </summary>
/// <param name="keyValues">primary key values</param>
/// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if error of Not Found and notFoundAllowed is true</returns>
public IStatusGeneric<int> SetSoftDeleteViaKeys<TEntity>(params object[] keyValues)
    where TEntity : class, TInterface

/// <summary>
/// This finds the entity using its primary key(s) and then it resets the single soft delete flag so it is now visible
/// </summary>
/// <param name="keyValues">primary key values</param>
/// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if error of Not Found and notFoundAllowed is true</returns>
public IStatusGeneric<int> ResetSoftDeleteViaKeys<TEntity>(params object[] keyValues)
    where TEntity : class, TInterface

/// <summary>
/// This finds the entity using its primary key(s) and then hard deletes it as long as the soft delete flag is set
/// </summary>
/// <param name="keyValues">primary key values</param>
/// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if error of Not Found and notFoundAllowed is true</returns>
public IStatusGeneric<int> HardDeleteViaKeys<TEntity>(params object[] keyValues)
    where TEntity : class, TInterface

/// <summary>
/// This will single soft delete the entity
/// </summary>
/// <param name="softDeleteThisEntity">Mustn't be null</param>
/// <param name="callSaveChanges">Defaults to calling SaveChanges. If set to false, then you must call SaveChanges</param>
/// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if error</returns>
public IStatusGeneric<int> SetSoftDelete(TInterface softDeleteThisEntity, bool callSaveChanges = true)

/// <summary>
/// This resets the single soft delete flag so that entity is now visible
/// </summary>
/// <param name="resetSoftDeleteThisEntity">Mustn't be null</param>
/// <param name="callSaveChanges">Defaults to calling SaveChanges. If set to false, then you must call SaveChanges</param>
/// <returns>Returns status. If not errors then Result return 1 to say it worked. Zero if errors</returns>
public IStatusGeneric<int> ResetSoftDelete(TInterface resetSoftDeleteThisEntity, bool callSaveChanges = true)

/// <summary>
/// This hard deletes (i.e. calls EF Core's Remove method) for this entity ONLY if it first been soft deleted.
/// This will delete the entity and possibly delete other dependent entities.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="hardDeleteThisEntity">The entity to delete</param>
/// <param name="callSaveChanges">Defaults to calling SaveChanges. If set to false, then you must call SaveChanges yourself</param>
/// <returns>The number of entities that were deleted. This will include any dependent entities that that had a cascade delete behaviour</returns>
public IStatusGeneric<int> HardDeleteSoftDeletedEntry<TEntity>(TEntity hardDeleteThisEntity, bool callSaveChanges = true)
    where TEntity : class, TInterface

/// <summary>
/// This returns the soft deleted entities of type TEntity
/// If you set up the OtherFilters property in the config then it will apply all the appropriate query filter so you only see the ones you should
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public IQueryable<TEntity> GetSoftDeletedEntries<TEntity>() where TEntity : class, TInterface

Note the bool callSaveChanges = true parameter on the methods that take in an entity class. This is there in case you want to do lots of changes together and want to call SaveChanges yourself so they are all saved at the same time.

Clone this wiki locally