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…
- Create a new class library project named
SoftDeleteExample. - Install the
NHibernatepackage using the NuGet Package Manager Console by executing the following command:Install-Package NHibernate - Add the
ISoftDeletableinterface using the following code:public interface ISoftDeletable { bool IsDeleted { get; } DateTime? DeletedAt { get; } } - Add an
App.configwith a standard NHibernate configuration. - Just before the end of the
sessionfactoryelement, add the following three event elements:<event type="delete"> <listener class=" SoftDeleteExample.SoftDeleteEventListener, SoftDeleteExample" /> </event> - Add the following
EventListener:public class SoftDeleteEventListener...