更换数据库?
看起来许多 ORM 工具和自定义数据访问层(DAO 模式等)的目标是将数据库抽象到可以用最少的工作替换整个数据库系统的程度。
在代码中遵循常见的 DAL 模式通常是一个好主意,但交换数据库似乎永远不是最简单的工作。 (成本、培训、数据迁移等)
是否有人有在大型系统中将一个数据库替换为另一个数据库并处理代码中的影响的经验? 是否值得担心从代码中抽象出实际的数据库?
It seems like the goal of a lot of ORM tools and custom data access layers (DAO pattern, etc.) is to abstract the database to the point where you could supposedly swap out the entire database system with minimal work.
Following the common DAL patterns is usually a good idea in code, but it seems like it would never be minimal work to swap out a database. (Cost, training, data migration, etc.)
Does anyone have any experience with swapping out one database for another in a large system, and dealing with the implications in code? Is it worth it to worry about abstracting the actual database from your code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,我们尝试过。 我们的客户正在使用基于 MS Access 的大型 Delphi 客户端服务器应用程序。 大约五年后,我们考虑切换到 SQL Server。 我们分析了这个问题并得出结论,交换数据库的成本非常高,而且只能带来一些好处。 客户决定不交换数据库。 该应用程序仍然运行良好,客户仍然满意。
请注意:
您必须考虑的最重要的问题:
是的。 在理想的情况下,交换数据库只是调整数据连接字符串。 在现实世界中这是不可能的,因为所有数据库都是不同的。 它们都有表和 SQL 支持,但差异在于细节。 如果您可以通过抽象来屏蔽数据库的差异 - 请这样做。 列出您需要支持的数据库。 检查所选数据库系统的差异。 提供集中代码来处理差异。 支持一种 RDBMS 并为将来支持其他 RDBMS 提供存根。
Yes we tried it. Our customer is using a large MS Access based Delphi client server application. After about five years we considered switching to SQL Server. We analyzed the problem and concluded that swapping the database would be very costly and provide only a few advantages. Customer decided not to swap the database. The application is still running fine and the customer is still happy.
Note that:
The most important issues you will have to consider:
Yes. In an ideal world, swapping a database would just be adjusting the data connection string. In the real world this is not possible because all databases are different. They all have tables and SQL support but the differences are in the details. If you can keep the differences of the databases shielded through abstraction - please do so. Make a list of the databases you need to support. Check the selected database systems for the differences. Provide centralized code to handle the differences. Support one RDBMS and provide stubs for future support of other RDBMS.
我不同意其目的是能够交换数据库,并且我认为您对 ORM 实现这一目标的怀疑是正确的。
然而,我仍然会使用 ORM,因为它抽象了数据访问的细节。 这不就是面向对象编程的目标吗? 将您的担忧分开。
I disagree that the purpose is to be able to swap out databases, and I think you are correct in showing some suspicion about ORMs leading towards that goal.
However, I would still use an ORM, as it abstracts away the details of data access. Isn't this the goal of object oriented programming? Keep your concerns separated.
我认为数据库抽象(通过 ORM 工具)的主要用例是能够发布与多个数据库品牌一起使用的产品。 我相信公司在数据库供应商之间切换的情况比较罕见,但这仍然是用例之一。
我曾经从事过一些工作,一开始我们出于经济原因使用 MySQL(想想一家初创公司),当我们开始赚钱时,我们想切换到 Oracle。 我们最终没有做出改变,但很高兴有这个选择。
尽管如此,ORM 工具并不是完全无泄漏的抽象,而且我知道我们的迁移仍然会是痛苦且昂贵的。 这完全取决于您正在构建的内容,但根据我的经验,通常出于性能原因,您最终要么围绕 ORM 解决方案工作,要么在某个时候利用供应商特定的功能。
I think the primary use case for database abstraction (via ORM tools) is to be able to ship a product that works with multiple database brands. I believe it's a rarer occurrence for a company to switch between database vendors, but that's still one of the use cases.
I've worked jobs where we started out using MySQL for monetary reasons (think a startup) and, one we started making money, wanted to switch to Oracle. We didn't end up making the switch, but it was nice to have the option.
Still, ORM tools are not a completely leak-less abstractions and I know our migration still would have been painful and costly. It totally depends on what you are building, but it has been my experience that -- for performance reasons, usually -- you end up either working around your ORM solution or exploiting vendor-specific features at some point.
我唯一一次看到数据库切换是随着项目的进展从早期开发期间的 HSQL 切换到 Oracle。 ORM 让这一切变得简单。
我经常使用 DAO 模式来交换数据服务(从数据库到 Web 服务或将 Web 服务交换到测试存根)。
对于 ORM,我认为目标不是让您能够切换数据库 - 它是让您免受不同数据库实现的复杂性的影响,并且无需担心从数据的关系表示转换为对象表示的细节。
通过让某人聪明地编写一个处理缓存的 ORM,只更新已更改的字段、组更新等,我不需要这样做。 尽管在我需要一些特殊的情况下,如果我愿意,我仍然可以恢复到 SQL。
The only time I've seen a database switch was from HSQL during early development to Oracle as the project progressed. The ORM made this easy.
I often use the DAO pattern to swap out data services (from a database to web service or to swap a web service to a test stub).
For ORM I don't think the goal is to enable you to switch databases - it is to hide you from the complexities of different database implementations and removing the need to worry about the fine details of translating from relational to object represenations of your data.
By having someone smart write an ORM that handles caching, only updates fields that have changed, groups updates, etc I don't need to. Although in the cases where I need something special I can still revert to SQL if I want.