Implementing a soft-delete pattern

Sometimes you do not want to delete the information from the database, but instead to mark it as deleted. This technique is called soft-delete. In this recipe, we will show you how to implement a soft-delete pattern with NHibernate.

How to do it…

  1. Create a new class library project named SoftDeleteExample.
  2. Install the NHibernate package using the NuGet Package Manager Console by executing the following command:
    Install-Package NHibernate
    
  3. Add the ISoftDeletable interface using the following code:
    public interface ISoftDeletable
    {
      bool IsDeleted { get; }
      DateTime? DeletedAt { get; }
    }
  4. Add an App.config with a standard NHibernate configuration.
  5. Just before the end of the sessionfactory element, add the following three ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.