什么是ORM,它如何工作,应该如何使用? [英] What is an ORM, how does it work, and how should I use one?
问题描述
有人建议我在正在设计的项目中使用ORM,但在查找有关其内容或工作方式的信息时遇到了麻烦.
Someone suggested I use an ORM for a project that I'm designing, but I'm having trouble finding information on what it is or how it works.
任何人都可以简要说明一下ORM是什么,它如何工作以及应该如何开始使用它.
Can anyone give me a brief explanation of what an ORM is and how it works and how I should get started using one?
推荐答案
简介
对象关系映射(ORM)是一种可让您查询和操作的技术使用面向对象范例从数据库中获取数据.在谈论ORM时,大多数人指的是实现对象关系映射技术的库,因此也称为"ORM".
Introduction
Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase "an ORM".
ORM库是用您选择的语言编写的完全普通的库,其中封装了处理数据所需的代码,因此您不再使用SQL.您可以直接使用与您使用的相同语言进行互动.
An ORM library is a completely ordinary library written in your language of choice that encapsulates the code needed to manipulate the data, so you don't use SQL anymore; you interact directly with an object in the same language you're using.
例如,这是一个完全虚构的情况,其中包含伪语言:
For example, here is a completely imaginary case with a pseudo language:
您有一本书类,您想要检索作者为"Linus"的所有书籍.手动执行以下操作:
You have a book class, you want to retrieve all the books of which the author is "Linus". Manually, you would do something like that:
book_list = new List();
sql = "SELECT book FROM library WHERE author = 'Linus'";
data = query(sql); // I over simplify ...
while (row = data.next())
{
book = new Book();
book.setAuthor(row.get('author');
book_list.add(book);
}
使用ORM库,它看起来像这样:
With an ORM library, it would look like this:
book_list = BookTable.query(author="Linus");
机械零件通过ORM库自动处理.
The mechanical part is taken care of automatically via the ORM library.
使用ORM可以节省大量时间,因为:
- DRY :您只在一个地方编写数据模型,并且更容易更新,维护和重用代码.
- 从数据库处理到 I18N ,很多工作都是自动完成的.
- 它迫使您编写 MVC 代码,最终使您的代码更简洁.
- 您不必编写格式不好的SQL(大多数Web程序员确实很讨厌它,因为SQL被视为一种子"语言,而实际上却是一种非常强大和复杂的语言).
- 消毒;使用准备好的语句或事务就像调用方法一样容易.
- DRY: You write your data model in only one place, and it's easier to update, maintain, and reuse the code.
- A lot of stuff is done automatically, from database handling to I18N.
- It forces you to write MVC code, which, in the end, makes your code a little cleaner.
- You don't have to write poorly-formed SQL (most Web programmers really suck at it, because SQL is treated like a "sub" language, when in reality it's a very powerful and complex one).
- Sanitizing; using prepared statements or transactions are as easy as calling a method.
使用ORM库更加灵活,因为:
- 它适合您的自然编码方式(这是您的语言!).
- 它抽象了数据库系统,因此您可以随时更改它.
- 该模型与应用程序的其余部分紧密绑定,因此您可以更改它或在其他任何地方使用它.
- 它使您可以轻松使用OOP之类的优势,例如数据继承.
但是ORM可能会很痛苦:
- 您必须学习它,并且ORM库不是轻量级的工具;
- 您必须进行设置.同样的问题.
- 对于常规查询而言,性能还可以,但是对于大型项目,SQL管理员始终可以使用自己的SQL来做得更好.
- 它抽象数据库.尽管您知道幕后发生的一切都可以,但是这对于新程序员来说是一个陷阱,他们可以编写非常贪婪的语句,例如
for
循环中的重击.
- You have to learn it, and ORM libraries are not lightweight tools;
- You have to set it up. Same problem.
- Performance is OK for usual queries, but a SQL master will always do better with his own SQL for big projects.
- It abstracts the DB. While it's OK if you know what's happening behind the scene, it's a trap for new programmers that can write very greedy statements, like a heavy hit in a
for
loop.
好吧,用一个.无论选择哪个ORM库,它们都使用相同的原理.这里有很多ORM库:
Well, use one. Whichever ORM library you choose, they all use the same principles. There are a lot of ORM libraries around here:
- Java:休眠.
- PHP: Propel 或 SQLAlchemy (我最喜欢的ORM库).
- C#: NHibernate 或
- Java: Hibernate.
- PHP: Propel or Doctrine (I prefer the last one).
- Python: the Django ORM or SQLAlchemy (My favorite ORM library ever).
- C#: NHibernate or Entity Framework
如果您想在Web编程中尝试ORM库,最好使用整个框架堆栈,例如:
If you want to try an ORM library in Web programming, you'd be better off using an entire framework stack like:
- Symfony (PHP, using Propel or Doctrine).
- Django (Python, using a internal ORM).
除非尝试学习某些知识,否则请不要尝试编写自己的ORM.这是一项巨大的工作,而旧的工作花了很多时间和工作才能变得可靠.
Do not try to write your own ORM, unless you are trying to learn something. This is a gigantic piece of work, and the old ones took a lot of time and work before they became reliable.
这篇关于什么是ORM,它如何工作,应该如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!