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