标记“已删除”而不是使用 Castle ActiveRecord 进行物理删除

发布于 2024-08-05 14:22:34 字数 769 浏览 7 评论 0原文

在我当前的项目中,我们收到了一个相当不寻常的请求(对我来说)。客户端希望所有的删除过程都标记一个标志,而不是从数据库表中物理删除记录。乍一看似乎很简单。我只是改变了

public void DeleteRecord(Record record)
{ 
    record.DeleteAndFlush();
}

public IList GetAllRecords()
{
    Record.FindAll().ToList();
}

public void DeleteRecord(Record record)
{ 
   record.Deleted = true;
   record.UpdateAndFlush();
}

public IList GetAllRecords()
{
    Record.FindAll().Where(x=>x.Deleted==false).ToList();
} 

但在我有一点时间并再次思考之后。我发现这个小小的改变会给我的级联设置带来很大的问题。因为我对 Active Record 业务还很陌生。我不相信自己能够简单地将所有 CascaeEnum.Delete 更改为 CascadeEnum.SaveUpdate。所以,我在这里寻找一些意见。

1)该标志是否是一种标志而不是常见的物理要求?

2) 如果问题 1 的答案是肯定的,那么我相信 NHibernate 中有内置的东西可以处理这个问题。有人可以告诉我解决此类问题的正确方法是什么吗?

感谢您的意见。

In my current project, we got a rather unusual request(to me). The client wants all the deletion procedure to mark a flag instead of physically delete the record from the database table. It looks pretty easy at the first glance. I just have change

public void DeleteRecord(Record record)
{ 
    record.DeleteAndFlush();
}

public IList GetAllRecords()
{
    Record.FindAll().ToList();
}

To

public void DeleteRecord(Record record)
{ 
   record.Deleted = true;
   record.UpdateAndFlush();
}

public IList GetAllRecords()
{
    Record.FindAll().Where(x=>x.Deleted==false).ToList();
} 

But as after I get a bit of time and think though again. I found that this little change would cause a huge problem in my cascade settings. As I am pretty new to the Active Record business. I wouldn't trust myself to simply change all the CascaeEnum.Delete to CascadeEnum.SaveUpdate. So, I am looking some input here.

1) Is the mark a flag instead of physical requirement a common one?

2) If the answer to question 1 is Yes, then I believe there is something built-in in NHibernate to handle this. Can someone tell me what is the right approach for this kind of problem?

Thanks for your input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老娘不死你永远是小三 2024-08-12 14:22:35

这称为软删除,很常见。关于最佳实践存在一些争论 - 请查看最近的这篇博客文章:http://ayende.com/Blog/archive/2009/08/30/avoid-soft-deletes.aspx

This is known as Soft Deletes and it is very common. There is some debate about the best practice - check out this recent blog post: http://ayende.com/Blog/archive/2009/08/30/avoid-soft-deletes.aspx

南烟 2024-08-12 14:22:35

这很常见,称为“软删除”。 这是 NHibernate 的实现

This is quite common and called "soft delete". Here's an implementation of this for NHibernate.

天邊彩虹 2024-08-12 14:22:35

这是一个相对常见的请求,有时出于(至少)两个原因实现它:

  1. 审核和历史记录 - 将行标记为已删除而不是物理删除它意味着如果需要,信息仍然可用(包括恢复)例如,如果您不小心删除了错误的客户)。
  2. 性能 - 我见过使用这种方法批量删除的系统,这样它们就可以在安静的时间物理地执行。我怀疑现代 DBMS 是否需要这样做,但我可以看到,如果您想避免在严重过载的系统上进行级联删除(当然,您不应该 首先在这样的系统上运行)。 Oracle 8 引入了这样的功能,您可以通过这种方式删除列,并且只有在您要求时才会物理删除它们 - 即使信息尚未完全删除,您也根本无法使用该列。当然,删除列比删除行更密集,但它仍然可能有帮助。

This is a relatively common request and it's sometimes implemented for (at least) two reasons:

  1. Auditing and history - Marking a row as deleted rather than physically deleting it means the information is still available if needed (including recovery of information if, for example, you accidentally delete the wrong customer).
  2. Performance - I've seen systems that batch up deletes with this method so they can be performed physically at a quiet time. I'm doubtful this is needed with modern DBMS' but I can see how it might have been so in the past if you wanted to avoid cascaded deletes on severely overloaded systems (of course, you shouldn't be running on such a system in the first place). Oracle 8 introduced a feature like this where you could drop columns in this manner and it would only physically remove them when you asked - you couldn't use the column at all even though the information had not yet been removed fully. Granted removal of a column is more intensive than removal of a row but it may still help.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文