IsChanged 功能应该在哪里处理?

发布于 2024-08-07 17:04:03 字数 675 浏览 5 评论 0原文

我正在内部争论应该在哪里处理数据更改检查,但无法决定最有意义的做法:

  1. 在 GUI 中处理 IsChanged - 这需要在页面加载和发布数据之间保留数据可能会产生大量带宽/页面传送开销。这在 win 表单应用程序中并不是那么糟糕,但在网站中,这可能会对带宽成本产生重大的财务影响。
  2. 在 DAL 中处理它 - 这需要多次调用数据库以检查在保存数据之前是否有任何数据已更改。这可能意味着对数据库进行额外的不必要的调用,可能会因不必要的数据库查询而导致可伸缩性问题。
  3. 在 Save() 存储过程中处理它 - 这可能需要存储过程对表进行额外的不必要的调用来进行检查,但会保存从 DAL 到数据库的额外调用。这可能比让 DAL 处理它更好地扩展,但我的直觉告诉我这可以做得更好。
  4. 在触发器中处理它 - 这需要使用触发器(我在情感上反对,除非绝对必要,否则我倾向于避免触发器)。
  5. 根本不处理 IsChanged 功能 - 不仅处理“LastUpdated”日期变得困难,而且将不必要的数据保存到数据库本身对于可伸缩性来说似乎是一种不好的做法。

因此,每种方法都有其缺点,我不知道哪种方法是最好的。有没有人有任何更可扩展的想法来处理数据持久性,以查看是否有任何变化的特定目的?

架构:SQL Server 2005、ASP.NET MVC、IIS7、针对非特定全球受众的可扩展性要求。

I'm having an internal debate about where I should handle checking for changes to data and can't decide on the practice that makes the most sense:

  1. Handling IsChanged in the GUI - This requires persistence of data between page load and posting of data which is potentially a lot of bandwidth/page delivery overhead. This isn't so bad in a win forms application, but in a website, this could start having a major financial impact for bandwidth costs.
  2. Handling it in the DAL - This requires multiple calls to the database to check if any data has changed prior to saving it. This potentially means an extra needless call to the database potentially causing scalability issues by needless database queries.
  3. Handling it in a Save() stored proc - This would require the stored proc to potentially make an extra needless call to the table to check, but would save the extra call from the DAL to the database. This could potentially scale better than having the DAL handle it, but my gut says this can be done better.
  4. Handling it in a trigger - This would require using a trigger (which I'm emotionally averse to, I tend to avoid triggers except where absolutely necessary).
  5. Don't handle IsChanged functionality at all - It not only becomes hard to handle the "LastUpdated" date, but saving data unnecessarily to the database seems like a bad practice for scalability in itself.

So each approach has its drawbacks and I'm at a loss as to which is the best of this bad bunch. Does anyone have any more scalable ideas for handling data persistence for the specific purpose of seeing if anything has changed?

Architecture: SQL Server 2005, ASP.NET MVC, IIS7, High scalability requirements for non-specific global audience.

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

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

发布评论

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

评论(3

旧时模样 2024-08-14 17:04:03

好的,这是另一个解决方案 - 我没有考虑所有的影响,但我认为它可以工作:

考虑一下 GetHashCode() 比较功能:

在页面加载时,您计算页面数据的哈希码。如果您愿意,可以将哈希码存储在页面数据或视图状态/会话中。

在数据发布(回发)时,您计算发布的数据的哈希代码并将其与原始哈希进行比较。如果不同,则用户更改了某些内容,您可以将其保存回数据库。

  • 优点
    • 您不必在页面加载时存储所有原始数据,从而减少带宽/页面传输开销。
    • 您不必让 DAL 对数据库进行多次调用来确定某些内容是否发生更改。
    • 仅当某些内容发生更改时,记录才会在数据库中更新,从而保持正确的上次更新日期。
  • 缺点
    • 您仍然需要将数据库中未存储的任何原始数据加载到业务对象中,而“视图状态”是将有效记录保存到数据库所必需的。
    • 改变一个字段就会改变哈希值,但你不知道是哪个字段,除非你调用数据库中的原始数据进行比较。顺便说一句,也许您不需要。如果您必须更新任何字段,时间戳会发生变化,并且覆盖未更改的字段不会产生任何影响。
    • 您不能完全排除发生碰撞的可能性,但这种情况很少见。这归结为碰撞的后果是否可以接受?
  • 要么/要么
    • 如果您将哈希存储在会话中,则可以节省带宽,但会增加服务器资源,因此在任何一种情况下都需要考虑潜在的可扩展性问题。
  • 未知数
    • 更新单个列的开销与更新记录中的多个/所有列的开销是否不同?我不知道性能开销是多少。

Okay, here's another solution - I've not thought through all the repercussions, but it could work I think:

Think about the GetHashCode() comparison functionality:

At page load time, you calculate the hash code for your page data. You store the hashcode in the page data or viewstate/session if that's what you prefer.

At data post (postback) you calculate the hash code of the data that was posted and compare it to the original hash. If it's different, the user changed something and you can save it back to the database.

  • Pros
    • You don't have to store all your original data in the page load which cuts down on bandwidth/page delivery overhead.
    • You don't have to have your DAL do multiple calls to the database to determine if something's changed.
    • The record will only be updated in the database if something's changed and hence maintain your correct LastUpdated date.
  • Cons
    • You still have to load any original data from the database into your business object that wasn't stored in the "viewstate" that is necessary to save a valid record to your database.
    • Change of one field will change the hash, but you don't know which field unless you call the original data from the database to compare. On a side note, perhaps you don't need to. If you've gotta update any of the fields the timestamp changes and overwriting a field that hasn't changed for all intensive purposes doesn't have any effect.
    • You can't completely rule out the chance of collisions but they would be rare. This comes down to is the repercussion of a collision acceptable or not?
  • Either/Or
    • If you store the hash in the session, then that saves bandwidth, but increases server resources so you have a potential scalability issue in either case to consider.
  • Unknowns
    • Is the overhead of updating a single column and different than that of updating multiple/all columns in a record? I don't know what that performance overhead is.
二智少女猫性小仙女 2024-08-14 17:04:03

我在 DAL 中处理它 - 它包含原始值,因此无需访问数据库。

I handle it in the DAL - it has the original values in it so no need to go to the database.

灰色世界里的红玫瑰 2024-08-14 17:04:03

对于系统中的每个实体,引入额外的版本字段。通过此字段,您将能够检查数据库级别的更改。

由于您有一个 Web 应用程序,并且通常可扩展性对于 Web 应用程序很重要,因此我建议您避免 UI 级别的 IsChanged 逻辑。 LastUpdated 日期可以在保存操作期间在数据库级别设置。

For each entity in your system introduce additional Version field. With this field you will be able to check for changes at the database level.

Since you have a web application and usually scalability matters for web application, I would suggest you to avoid IsChanged logic at the UI level. LastUpdated date can be set at the database level during Save operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文