简体   繁体   English

数据库层的业务逻辑

[英]business logic in database layer

I hate to ask the classic question of "business logic in database vs code" again, but I need some concrete reasons to convince an older team of developers that business logic in code is better, because it's more maintainable, above all else.我不想再问“数据库中的业务逻辑与代码中的业务逻辑”这一经典问题,但我需要一些具体的理由来说服老牌开发人员团队代码中的业务逻辑更好,因为它更易于维护,最重要的是。 I used to have a lot of business logic in the DB, because I believed it was the single point of access.我曾经在数据库中有很多业务逻辑,因为我相信它是单点访问。 Maintenance is easy, if I was the only one doing the changing it.维护很容易,如果我是唯一一个进行更改的人。 In my experience, the problems came when the projects got larger and complicated.以我的经验,当项目变得更大更复杂时,问题就出现了。 Source Control for DB Stored Procs are not so advanced as the ones for newer IDEs, nor are the editors. DB Stored Procs 的源代码控制没有新 IDE 的那么先进,编辑器也没有。 Business logic in code can scale much better than in the DB, is what I've found in my recent experience.代码中的业务逻辑可以比在数据库中更好地扩展,这是我在最近的经验中发现的。

So, just searching around stackoverflow, I found quite the opposite philosophy from its esteemed members:所以,只要搜索一下stackoverflow,我就发现了与它尊敬的成员完全相反的哲学:

https://stackoverflow.com/search?q=business+logic+in+database https://stackoverflow.com/search?q=business+logic+in+database

I know there is no absolute for any situation, but for a given asp.net solution, which will use either sql server or oracle, for a not a particularly high traffic site, why would I put the logic in the DB?我知道对于任何情况都没有绝对的,但是对于给定的 asp.net 解决方案,它将使用 sql 服务器或 oracle,对于一个不是特别高流量的网站,为什么将逻辑放入数据库?

Depends on what you call business.取决于你所说的业务。

The database should do what is expected.数据库应该做预期的事情。

If the consumers and providers of data expect the database to make certain guarantees, then it needs to be done in the database.如果数据的消费者和提供者期望数据库做出一定的保证,那么就需要在数据库中完成。

Some people don't use referential integrity in their databases and expect the other parts of the system to manage that.有些人不在他们的数据库中使用参照完整性,并期望系统的其他部分来管理它。 Some people access tables in the database directly.有些人直接访问数据库中的表。

I feel that from a systems and component perspective, the database is like any other service or class/object.我觉得从系统和组件的角度来看,数据库就像任何其他服务或类/对象一样。 It needs to protect its perimeter, hide its implementation details and provide guarantees of integrity, from low-level integrity up to a certain level, which may be considered "business".它需要保护其边界,隐藏其实施细节并提供完整性保证,从低级完整性到一定级别,这可能被认为是“业务”。

Good ways to do this are referential integrity, stored procedures, triggers (where necessary), views, hiding base tables, etc., etc.做到这一点的好方法是参照完整性、存储过程、触发器(必要时)、视图、隐藏基表等。

Database does data things, why weigh down something that is already getting hit pretty hard to give you data.数据库做数据的事情,为什么要权衡已经受到很大打击的东西才能给你数据。 It's a performance thing and a code thing.这是性能问题和代码问题。 It's MUCH easier to maintain business logic code than to store it all in the database.维护业务逻辑代码比将其全部存储在数据库中要容易得多。 Sprocs, Views and Functions can only go so far until you have Views of Views of Views with sprocs to fill that mess in. With business logic you separate your worries.到目前为止,Sprocs、Views 和 Functions 只能是 go ,直到您拥有 Views of Views with sprocs 来填补这个烂摊子。有了业务逻辑,您就可以分心。 If you have a bug that's causing something to be calculated wrong it's easier to check the business logic code than go into the DB and see if someone messed up something in a Stored Procedure.如果你有一个导致计算错误的错误,那么检查业务逻辑代码比检查 go 更容易进入数据库,看看是否有人在存储过程中搞砸了。 This is highly opinionated and in some cases it's OK to put some logic in the database but my thoughts on this are it's a database not a logicbase, put things where they belong.这是高度自以为是的,在某些情况下,可以将一些逻辑放入数据库中,但我对此的想法是它是一个数据库而不是一个逻辑库,把东西放在它们所属的地方。

PS: Might be catchin some heat for this post, it's highly opinionated and other than performance numbers there's no real evidence for either and it becomes a case of what you're working with. PS:这篇文章可能会引起一些热议,它是高度固执己见的,除了性能数据之外,没有任何真正的证据,它成为你正在使用的一个案例。

EDIT: Something that Cade mentioned that I forgot.编辑:凯德提到的我忘记的东西。 Refrential integrity.参照完整性。 By all means please have correct data integrity in your DB, no orphaned records ON DELETE CASCADE's, checks and whatnot.无论如何,请在您的数据库中保持正确的数据完整性,在 DELETE CASCADE 上没有孤立的记录,检查等等。

I have faced with database logic on one of huge projects.我在一个大型项目中遇到过数据库逻辑。 This was caused by the decision of main manager who was the DBA specialist.这是由作为 DBA 专家的主要经理的决定引起的。 He said that the application should be leightweight, it should know nothing about database scheme, joined tables, etc, and anyway stored Procs executes much faster than the transaction scopes and queries from client.他说应用程序应该是轻量级的,它应该对数据库方案、连接表等一无所知,并且无论如何存储的 Procs 执行速度比事务范围和来自客户端的查询快得多。 At the other side, we had too much bugs with database object mappings (stored prod or view based on view based on other view etc).另一方面,我们在数据库 object 映射(存储的产品或基于基于其他视图的视图的视图等)方面存在太多错误。 It was unreachable to understand what is happening with our data because of each button clicked called a huge stored proc with 70-90-120 parameters and updated several (10-15) tables.无法理解我们的数据发生了什么,因为单击的每个按钮都称为一个巨大的存储过程,其中包含 70-90-120 个参数并更新了几个 (10-15) 表。 We had no ability to query simple select request so we had to compile a view or stored Proc and class in code for this just for one simple join:-( of course when the table or view definition changes you should recompile all other dB objects based on edited object elsewhere you will get runtime Exception. So I think that logic in database is a horrible way. Of course you can store some pieces of code in stored procs if needed by performance or security issues, but you shoul not develop everything in the Database) the logic should be flexible, testable and maintenable, and you can not reach this points using database for storing logic)我们无法查询简单的 select 请求,因此我们必须编译视图或在代码中存储 Proc 和 class 仅用于一个简单的连接:-(当然,当表或视图定义更改时,您应该重新编译所有其他基于在其他地方编辑的 object 上,你会得到运行时异常。所以我认为数据库中的逻辑是一种可怕的方式。当然,如果性能或安全问题需要,你可以将一些代码存储在存储过程中,但你不应该在数据库)逻辑应该是灵活的、可测试的和可维护的,你不能使用数据库来存储逻辑)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM