我正在使用ActiveRecord和nHibernate,我正在尝试实现软删除,因此对象被标记为“IsDeleted”而不是实际删除它们。我显然需要级联选项才能工作。
[assembly: AddEventListener(typeof(MyNamespace.SoftDeleteListener))] namespace MyNamespace { public class SoftDeleteListener : DefaultDeleteEventListener
{
protected override void DeleteEntity(IEventSource session, object entity, EntityEntry entityEntry, bool isCascadeDeleteEnabled,
IEntityPersister persister, ISet transientEntities)
{
if (entity is IPermanentRecord)
{
((IPermanentRecord)entity).IsDeleted = true;
this.CascadeBeforeDelete(session, persister, entity,
entityEntry, transientEntities);
this.CascadeAfterDelete(session, persister, entity, transientEntities);
}
else
{
base.DeleteEntity(session, entity, entityEntry,
isCascadeDeleteEnabled, persister, transientEntities);
}
}
}
}
我的实体 - “产品”实施
interface IPermanentRecord
{
bool IsDeleted { get; set; }
}
问题是,在删除实体时,侦听器不会被触发它只是删除它!
答案 0 :(得分:1)
对任何有兴趣的人......
我找到了nHibernate配置并手动添加了监听器并且它被解雇..
foreach (NHibernate.Cfg.Configuration cfg in ActiveRecordMediator.GetSessionFactoryHolder().GetAllConfigurations())
{
cfg.SetListener(ListenerType.Delete, new SoftDeleteListener());
cfg.AddAssembly(assem);
}