业务逻辑应该写在哪里?在前端(业务层)还是在存储过程中?

发布于 2025-01-02 05:53:42 字数 418 浏览 3 评论 0原文

我正在编写一个带有 SQLServer 数据库的 ASP.NET 应用程序,我必须在其中计算应用程序成员的费率。这些计算影响超过 10 个数据库表。

我相信我有两个选择:

  1. 在数据访问层中,从数据库中的第一个表中获取成员的数据并将其返回到业务层。到达那里后,对获取的数据执行一些计算以生成要保存在第二个表中的新数据。最后回调到数据访问层保存结果。然后,重复整个过程,从第二个表中提取信息来计算结果,并将结果保存在第三个表中,并对所有必要的表继续执行此操作。

  2. 使用存储过程封装在数据库事务中正确表中计算和保存成员的新费率。

您认为哪种选择最好,为什么?如果我使用第一个选项,我应该如何从业务逻辑层执行一个 ADO.NET 事务中的所有插入和更新?我目前被禁止在数据访问层之外使用 ADO.NET 事务。

I am writing an ASP.NET application with a SQLServer database in which I have to calculate rates for members of my application. These calculations affect more than 10 database tables.

I believe I have two options :

  1. In the data access layer, fetch the data for a member from the first table in the database and return it to the business layer. Once there, perform some calculation on the fetched data to generate new data to be saved in a second table. Finally, call back into the data access layer to save the result. Afterwards, repeat this whole process, pulling information from the second table to calculate results to be saved in a third table, and keep doing this for all necessary tables.

  2. Use a stored procedure to encapsulate calculating and saving the new rates for a member in the correct tables within a database transaction.

Which option do you think is best and why? If I use the first option, how should I perform all of the inserts and updates in one ADO.NET transaction from the business logic layer? I am currently barred from using an ADO.NET transaction outside of the data access layer.

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

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

发布评论

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

评论(1

梦里南柯 2025-01-09 05:53:42

在我看来,这取决于需要对性能和模块化设计给予多少优先级。

如果这是一个交易应用程序,我必须从不同的表中即时计算价格,我会使用存储过程

  • 对于一定量的负载有更好的性能,但当查询太多时,分布式数据库就变得至关重要
  • 一个缺点是如果您想将来移动到不同的数据库(在大多数情况下人们不会),那么您必须将存储过程移植到新数据库中。

我的另一个选择是将大部分值保留在分布式数据库中缓存(如 memcache)并在业务中进行计算 层。

  • 这些值将被预先填充到缓存中,并且当发生更改时缓存将被刷新。
  • 这在消除数据库依赖性方面提供了灵活性。

但在我看来,您的操作在功能上非常依赖数据库,并建议使用存储过程路线。如果你认为缓存的第二种选择是可能的,那么值得一试。

IMO it depends on how much priority needs to be given for performance and modular design.

If this was a trading application where I would have to calculate the prices instantaneously from different tables, I would use a stored procedure

  • Better performance for certain amount of load but when it gets too much queries, then a distributed database becomes essential
  • One disadvantage is that if you want to move to a different database in the future (in most cases people don't), then you have to port the stored proc to the new ones

The other option I would have is to keep most of the values in a distributed cache (like memcache) and do the calculations in a business layer.

  • The values will be pre-populated into the cache and the cache will be refreshed as and when there are changes.
  • This gives flexibility in terms of removing the database dependency.

But it seems to me that your operations are quite heavily DB dependent in functionality and suggest a stored procedure route. If you think the second option of cache is possible, it is worth a try.

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