简体   繁体   English

代码生成器与ORM与存储过程

[英]Code generators vs. ORMs vs. Stored Procedures

In what domains do each of these software architectures shine or fail? 这些软件体系结构在哪些领域发挥作用或失败?

Which key requirements would prompt you to choose one over the other? 哪些关键要求会促使您选择其中一个?

Please assume that you have developers available who can do good object oriented code as well as good database development. 请假设您有开发人员可以做好面向对象的代码以及良好的数据库开发。

Also, please avoid holy wars :) all three technologies have pros and cons, I'm interested in where is most appropriate to use which. 另外,请避免神圣的战争:)这三种技术都有利有弊,我对哪种方式最适合使用感兴趣。

Every one of these tools provides differing layers of abstraction, along with differing points to override behavior. 这些工具中的每一个都提供不同的抽象层,以及覆盖行为的不同点。 These are architecture choices, and all architectural choices depend on trade-offs between technology, control, and organization, both of the application itself and the environment where it will be deployed. 这些是架构选择,所有架构选择都取决于技术,控制和组织之间的权衡,包括应用程序本身和部署它的环境。

  • If you're dealing with a culture where DBAs 'rule the roost', then a stored-procedure-based architecture will be easier to deploy. 如果您正在处理DBA“掌控”的文化,那么基于存储过程的架构将更易于部署。 On the other hand, it can be very difficult to manage and version stored procedures. 另一方面,管理和版本存储过程可能非常困难。

  • Code generators shine when you use statically-typed languages, because you can catch errors at compile-time instead of at run-time. 使用静态类型语言时,代码生成器会发光,因为您可以在编译时而不是在运行时捕获错误。

  • ORMs are ideal for integration tools, where you may need to deal with different RDBMSes and schemas on an installation-to-installation basis. ORM是集成工具的理想选择,您可能需要在安装到安装的基础上处理不同的RDBMS和模式。 Change one map and your application goes from working with PeopleSoft on Oracle to working with Microsoft Dynamics on SQL Server. 更改一个地图,您的应用程序将从使用Oracle上的PeopleSoft到使用SQL Server上的Microsoft Dynamics。

I've seen applications where Generated Code is used to interface with Stored Procedures, because the stored procedures could be tweaked to get around limitations in the code generator. 我已经看到了使用Generated Code与存储过程进行交互的应用程序,因为可以调整存储过程以绕过代码生成器中的限制。

Ultimately the only correct answer will depend upon the problem you're trying to solve and the environment where the solution needs to execute. 最终,唯一正确的答案将取决于您尝试解决的问题以及解决方案需要执行的环境。 Anything else is arguing the correct pronunciation of 'potato'. 还有其他东西在争论'马铃薯'的正确发音。

I'll add my two cents: 我加上我的两分钱:

Stored procedures 存储过程

  • Can be easily optimized 可以轻松优化
  • Abstract fundamental business rules, enhancing data integrity 抽象的基本业务规则,增强数据完整性
  • Provide a good security model (no need to grant read or write permissions to a front facing db user) 提供良好的安全模型(无需向前端数据库用户授予读取或写入权限)
  • Shine when you have many applications accessing the same data 当您有许多应用程序访问相同的数据时闪耀

ORMs 奥姆斯

  • Let you concentrate only on the domain and have a more "pure" object oriented approach to development 让您只关注域,并采用更“纯粹”的面向对象的开发方法
  • Shine when your application must be cross db compatible 当您的应用程序必须与数据库兼容时,请发光
  • Shine when your application is mostly driven by behaviour instead of data 当您的应用程序主要由行为而不是数据驱动时,请发光

Code Generators 代码生成器

  • Provide you similar benefits as ORMs, with higher maintenance costs, but with better customizability. 为您提供与ORM类似的好处,具有更高的维护成本,但具有更好的可定制性。
  • Are generally superior to ORMs in that ORMs tend to trade compile-time errors for runtime errors, which is generally to be avoided 通常优于ORM,因为ORM倾向于为运行时错误交换编译时错误,这通常是要避免的

I agree that there are pros and cons to everything and a lot depends on your architecture. 我同意一切都有利有弊,很大程度上取决于你的架构。 That being said, I try to use ORM's where it makes sense. 话虽这么说,我尝试在有意义的地方使用ORM。 A lot of the functionality is already there and usually they help prevent SQL Injection (plus it helps avoid re-inventing the wheel). 许多功能已经存在,通常它们有助于防止SQL注入(加上它有助于避免重新发明轮子)。

Please see these other two posts on the topic (dynamic SQL vs stored procedures vs ORM) for more information 有关更多信息,请参阅有关该主题的其他两篇帖子(动态SQL与存储过程与ORM)

Dynamic SQL vs. stored procedures 动态SQL与存储过程
Which is better: Ad hoc queries, or stored procedures? 哪个更好:即席查询或存储过程?

ORMs vs. stored procedures ORM与存储过程
Why is parameterized SQL generated by NHibernate just as fast as a stored procedure? 为什么NHibernate生成的参数化SQL和存储过程一样快?

ORMs and code generators are kind of on one side of the field, and stored procedures are on another. ORM和代码生成器位于字段的一侧,存储过程位于另一侧。 Typically, it's easier to use ORMs and code generators in greenfield projects, because you can tailor your database schema to match the domain model you create. 通常,在绿地项目中使用ORM和代码生成器更容易,因为您可以定制数据库模式以匹配您创建的域模型。 It's much more difficult to use them with legacy projects, because once software is written with a "data-first" mindset, it's difficult to wrap it with a domain model. 将它们与遗留项目一起使用要困难得多,因为一旦软件使用“数据优先”思维模式编写,就很难用域模型进行包装。

That being said, all three of the approaches have value. 话虽如此,这三种方法都有价值。 Stored procedures can be easier to optimize, but it can be tempting to put business logic in them that may be repeated in the application itself. 存储过程可以更容易优化,但是可能很容易将业务逻辑放在其中,这些逻辑可能会在应用程序本身中重复出现。 ORMs work well if your schema matches the concept of the ORM, but can be difficult to customize if not. 如果您的模式与ORM的概念匹配,ORM可以很好地工作,但如果没有,则很难自定义。 Code generators can be a nice middle ground, because they provide some of the benefits of an ORM but allow customization of the generated code -- however, if you get into the habit of altering the generated code, you then have two problems, because you will have to alter it each time you re-generate it. 代码生成器可以是一个很好的中间层,因为它们提供了ORM的一些好处,但允许自定义生成的代码 - 但是,如果你养成改变生成的代码的习惯,那么你有两个问题,因为你每次重新生成它时都必须改变它。

There is no one true answer, but I tend more towards the ORM side because I believe it makes more sense to think with an object-first mindset. 没有一个真正的答案,但我更倾向于ORM方面,因为我认为用对象优先思维思考更有意义。

You forgot a significant option that deserves a category of its own: a hybrid data mapping framework such as iBatis . 您忘记了一个值得拥有自己类别的重要选项:混合数据映射框架,例如iBatis

I have been pleased with iBatis because it lets your OO code remain OO in nature, and your database remain relational in nature, and solves the impedance mismatch by adding a third abstraction (the mapping layer between the objects and the relations) that is responsible for mapping the two, rather than trying to force fit one paradigm into the other. 我对iBatis很满意,因为它让你的OO代码本质上保持OO,你的数据库本质上仍然是关系的,并通过添加第三个抽象(对象和关系之间的映射层)来解决阻抗不匹配问题。映射两者,而不是试图强制将一个范式强加到另一个范例中。

Stored Procedures 存储过程

  • Pros: Encapsulates data access code and is application-independent 优点:封装数据访问代码,与应用程序无关
  • Cons: Can be RDBMS-specific and increase development time 缺点:可以特定于RDBMS并增加开发时间

ORM ORM

At least some ORMs allow mapping to stored procedures 至少一些ORM允许映射到存储过程

  • Pros: Abstracts data access code and allows entity objects to be written in domain-specific way 优点:抽象数据访问代码,并允许以特定于域的方式编写实体对象
  • Cons: Possible performance overhead and limited mapping capability 缺点:可能的性能开销和有限的映射功能

Code generation 代码生成

  • Pros: Can be used to generate stored-proc based code or an ORM or a mix of both 优点:可用于生成基于存储过程的代码或ORM或两者的混合
  • Cons: Code generator layer may have to be maintained in addition to understanding generated code 缺点:除了理解生成的代码之外,可能还必须维护代码生成器层

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

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