什么是 ORM,它是如何工作的,我应该如何使用它? [英] What is an ORM, how does it work, and how should I use one?

查看:58
本文介绍了什么是 ORM,它是如何工作的,我应该如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人建议我将 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: 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,使用 Propel 或 Doctrine).
  • Django(Python,使用内部 ORM).
  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆