This question already has an answer here:
这个问题在这里已有答案:
- What is an ORM and where can I learn more about it? [closed] 8 answers
什么是ORM?我在哪里可以了解更多信息? [已关闭] 8个答案
As the title says; what is a ORM framework and what is it useful for?
正如标题所说;什么是ORM框架?它对什么有用?
8 个解决方案
#1
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.
计算机软件中的对象关系映射(ORM,O / RM和O / R映射)是用于在关系数据库和面向对象的编程语言中的不兼容类型系统之间转换数据的编程技术。这实际上创建了可以在编程语言中使用的“虚拟对象数据库”。虽然有些程序员选择创建自己的ORM工具,但有可用的免费和商业软件包可以执行对象关系映射。
It's good for abstracting the datastore (flat file / SQL / whatever) out in order to provide an interface that can be used in your code. For example, (in rails) instead of constructing SQL to find the first user in a users table, we could do this:
为了提供可以在代码中使用的接口,抽象数据存储区(平面文件/ SQL /无论什么)是很好的。例如,(在rails中)而不是构造SQL来查找users表中的第一个用户,我们可以这样做:
User.first
Which would return us an instance of our user model, with the attributes of the first user in the users table.
这将返回我们的用户模型的实例,其中包含users表中第一个用户的属性。
#2
A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.
一个简单的答案是,您使用编程语言将表或存储过程包装在类中,这样您就可以使用对象的方法和属性来代替编写SQL语句来与数据库进行交互。
In other words, instead of something like this:
换句话说,而不是像这样的东西:
String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];
you do something like this:
你做这样的事情:
Person p = repository.GetPerson(10);
String name = p.FirstName;
or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:
或类似的代码(这里有很多变化。)一些框架也将很多代码作为静态方法放在类本身上,这意味着你可以做这样的事情:
Person p = Person.Get(10);
Some also implement complex query systems, so you could do this:
有些还实现了复杂的查询系统,因此您可以这样做:
Person p = Person.Get(Person.Properties.Id == 10);
The framework is what makes this code possible.
该框架使这段代码成为可能。
Now, benefits. First of all, you hide the SQL away from your logic code. This has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to "get me all persons edited the last 24 hours" might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.
现在,好处。首先,您将SQL隐藏在逻辑代码之外。这样做的好处是可以让您更轻松地支持更多的数据库引擎。例如,MS SQL Server和Oracle在典型函数上有不同的名称,以及使用日期进行计算的不同方法,因此查询“让我所有人在过去24小时内编辑”可能需要针对这两个数据库引擎的不同SQL语法。这种差异可以远离您的逻辑代码。
Additionally, you can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn't contain all the "plumbing" necessary to talk to the database.
此外,您可以专注于编写逻辑,而不是让所有SQL正确。代码通常也更具可读性,因为它不包含与数据库通信所需的所有“管道”。
#3
Databases usually work on relational model: you have tables (simplifying: like a spreadsheet), and relations between them - one-to-one, one-to-many, many-to-many, etc, meaning for instance that one record in table A has many related records in table B. You can retrieve data from them as rows (collection of values representing rows from table/tables) More in wikipedia.
数据库通常用于关系模型:你有表(简化:像电子表格),以及它们之间的关系 - 一对一,一对多,多对多等等,这意味着例如一条记录表A在表B中有许多相关记录。您可以从它们中检索数据作为行(表示表/表中行的值的集合)更多内容在*中。
Modern programming languages use object model. Objects have methods, attributes (simple or complex) etc.
现代编程语言使用对象模型。对象具有方法,属性(简单或复杂)等。
ORM software does a transition between those models. For instance, it puts all related records from table B into an attribute of object A. This kind of software makes it easier to use relational databases (most popular kind) with object programming languages.
ORM软件在这些模型之间进行转换。例如,它将表B中的所有相关记录放入对象A的属性中。这种软件使得使用对象编程语言更容易使用关系数据库(最流行的类型)。
#4
Whenever you go with ORM (Object Relational Mapper) you will find DBAL (Database Abstraction Layer) side by side. So its necessary to know what these are, so as to get good insights of what you are using and whats the advantages that you will get.
每当你使用ORM(对象关系映射器)时,你会发现DBAL(数据库抽象层)并排。因此,有必要了解这些是什么,以便获得您正在使用的内容的良好见解,并了解您将获得的优势。
DBAL (Database Abstraction Layer)
DBAL(数据库抽象层)
It acts as a layer between your code and database. Irrespective of whatever your database, the code written will work fine with minor tweaking.
它充当代码和数据库之间的层。无论您的数据库是什么,编写的代码都可以通过微小的调整来正常工作。
Assume that for the current project your using MySQL once its a fully matured and gets huge traffic your team plan to switch the database to Oracle for some reason then the code you have written in MySQL must be rewritten to Oracle based queries. And rewriting the queries for the whole project is a tedious task.
假设对于当前项目,您使用MySQL一旦完全成熟并获得巨大流量,您的团队计划将数据库切换到Oracle由于某种原因,那么您在MySQL中编写的代码必须重写为基于Oracle的查询。重写整个项目的查询是一项繁琐的工作。
Instead if you use any DBAL libraries then you can switch the configuration of the database and make sure your project will be up and running within a day (May be with some minor tweaking).
相反,如果您使用任何DBAL库,那么您可以切换数据库的配置,并确保您的项目将在一天内启动并运行(可能需要进行一些小的调整)。
ORM (Object Relational Mapper)
ORM(对象关系映射器)
Object Relational Mapping (ORM) is a technique (Design Pattern) of accessing a relational database from an object-oriented language.
对象关系映射(ORM)是一种从面向对象的语言访问关系数据库的技术(设计模式)。
If you have used any kind of frameworks like Symfony (if you come from PHP background)/Hibernate (Java), then your familiar with these. Its nothing but Entities.
如果您使用过任何类型的框架,如Symfony(如果您来自PHP背景)/ Hibernate(Java),那么您熟悉这些。它只不过是实体。
In order to access the database in Object Oriented context and interface translating the object logic is necessary, this interface is called as ORM. Its make up of the object that give access to data and keep the business rules with themselves.
为了在面向对象的上下文和接口中访问数据库,需要转换对象逻辑,此接口称为ORM。它由对象组成,可以访问数据并保持业务规则。
Eg.
class User{
private $email;
private $password;
public function setEmail($email){
$this->email = $email;
return $this;
}
public function getEmail(){
return $this->email;
}
public function setPassword($password){
$this->password = $password;
return $this;
}
public function getPassword(){
return $this->password;
}
}
/* To save User details you would do something like this */
$userObj = new User();
$userObj->setEmail('sanitizedEmail');
$userObj->setPassword('sanitizedPassword');
$userObj->save();
/* To fetch user details you would do something like this */
$userObj = new User();
$userDetails = $userObj->find($id);
例如。 Doctrine,Propel,RedBean
#5
#6
From Wikipedia: http://en.wikipedia.org/wiki/Object-relational_mapping
来自*:http://en.wikipedia.org/wiki/Object-relational_mapping
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.
计算机软件中的对象关系映射(ORM,O / RM和O / R映射)是用于在关系数据库和面向对象的编程语言中的不兼容类型系统之间转换数据的编程技术。这实际上创建了可以在编程语言中使用的“虚拟对象数据库”。虽然有些程序员选择创建自己的ORM工具,但有可用的免费和商业软件包可以执行对象关系映射。
Pros and cons ORM often reduces the amount of code needed to be written, making the software more robust (the fewer the lines of code in a program, the fewer the errors contained within them).[1].
优点和缺点ORM通常会减少需要编写的代码量,使软件更加健壮(程序中的代码行越少,其中包含的错误就越少)。[1]。
There are costs as well as benefits for using O/R mapping. For instance, some O/R mapping tools do not perform well during bulk deletions of data. Stored procedures may have better performance but are not portable.
使用O / R映射有成本和好处。例如,一些O / R映射工具在批量删除数据期间表现不佳。存储过程可能具有更好的性能但不可移植。
#7
It allows you to do things like this (this is Doctrine code):
它允许你做这样的事情(这是Doctrine代码):
$activeUsers = Doctrine::getTable('User')->createQuery('u')->addWhere('u.active = false');
foreach($activeUsers as $user)
{
$user->active = true;
$user->save();
}
#8
Object-relational mapping (ORM) libraries provide this mapping of database tables to domain object classes.
对象关系映射(ORM)库提供了数据库表到域对象类的映射。
#1
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.
计算机软件中的对象关系映射(ORM,O / RM和O / R映射)是用于在关系数据库和面向对象的编程语言中的不兼容类型系统之间转换数据的编程技术。这实际上创建了可以在编程语言中使用的“虚拟对象数据库”。虽然有些程序员选择创建自己的ORM工具,但有可用的免费和商业软件包可以执行对象关系映射。
It's good for abstracting the datastore (flat file / SQL / whatever) out in order to provide an interface that can be used in your code. For example, (in rails) instead of constructing SQL to find the first user in a users table, we could do this:
为了提供可以在代码中使用的接口,抽象数据存储区(平面文件/ SQL /无论什么)是很好的。例如,(在rails中)而不是构造SQL来查找users表中的第一个用户,我们可以这样做:
User.first
Which would return us an instance of our user model, with the attributes of the first user in the users table.
这将返回我们的用户模型的实例,其中包含users表中第一个用户的属性。
#2
A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.
一个简单的答案是,您使用编程语言将表或存储过程包装在类中,这样您就可以使用对象的方法和属性来代替编写SQL语句来与数据库进行交互。
In other words, instead of something like this:
换句话说,而不是像这样的东西:
String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];
you do something like this:
你做这样的事情:
Person p = repository.GetPerson(10);
String name = p.FirstName;
or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:
或类似的代码(这里有很多变化。)一些框架也将很多代码作为静态方法放在类本身上,这意味着你可以做这样的事情:
Person p = Person.Get(10);
Some also implement complex query systems, so you could do this:
有些还实现了复杂的查询系统,因此您可以这样做:
Person p = Person.Get(Person.Properties.Id == 10);
The framework is what makes this code possible.
该框架使这段代码成为可能。
Now, benefits. First of all, you hide the SQL away from your logic code. This has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to "get me all persons edited the last 24 hours" might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.
现在,好处。首先,您将SQL隐藏在逻辑代码之外。这样做的好处是可以让您更轻松地支持更多的数据库引擎。例如,MS SQL Server和Oracle在典型函数上有不同的名称,以及使用日期进行计算的不同方法,因此查询“让我所有人在过去24小时内编辑”可能需要针对这两个数据库引擎的不同SQL语法。这种差异可以远离您的逻辑代码。
Additionally, you can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn't contain all the "plumbing" necessary to talk to the database.
此外,您可以专注于编写逻辑,而不是让所有SQL正确。代码通常也更具可读性,因为它不包含与数据库通信所需的所有“管道”。
#3
Databases usually work on relational model: you have tables (simplifying: like a spreadsheet), and relations between them - one-to-one, one-to-many, many-to-many, etc, meaning for instance that one record in table A has many related records in table B. You can retrieve data from them as rows (collection of values representing rows from table/tables) More in wikipedia.
数据库通常用于关系模型:你有表(简化:像电子表格),以及它们之间的关系 - 一对一,一对多,多对多等等,这意味着例如一条记录表A在表B中有许多相关记录。您可以从它们中检索数据作为行(表示表/表中行的值的集合)更多内容在*中。
Modern programming languages use object model. Objects have methods, attributes (simple or complex) etc.
现代编程语言使用对象模型。对象具有方法,属性(简单或复杂)等。
ORM software does a transition between those models. For instance, it puts all related records from table B into an attribute of object A. This kind of software makes it easier to use relational databases (most popular kind) with object programming languages.
ORM软件在这些模型之间进行转换。例如,它将表B中的所有相关记录放入对象A的属性中。这种软件使得使用对象编程语言更容易使用关系数据库(最流行的类型)。
#4
Whenever you go with ORM (Object Relational Mapper) you will find DBAL (Database Abstraction Layer) side by side. So its necessary to know what these are, so as to get good insights of what you are using and whats the advantages that you will get.
每当你使用ORM(对象关系映射器)时,你会发现DBAL(数据库抽象层)并排。因此,有必要了解这些是什么,以便获得您正在使用的内容的良好见解,并了解您将获得的优势。
DBAL (Database Abstraction Layer)
DBAL(数据库抽象层)
It acts as a layer between your code and database. Irrespective of whatever your database, the code written will work fine with minor tweaking.
它充当代码和数据库之间的层。无论您的数据库是什么,编写的代码都可以通过微小的调整来正常工作。
Assume that for the current project your using MySQL once its a fully matured and gets huge traffic your team plan to switch the database to Oracle for some reason then the code you have written in MySQL must be rewritten to Oracle based queries. And rewriting the queries for the whole project is a tedious task.
假设对于当前项目,您使用MySQL一旦完全成熟并获得巨大流量,您的团队计划将数据库切换到Oracle由于某种原因,那么您在MySQL中编写的代码必须重写为基于Oracle的查询。重写整个项目的查询是一项繁琐的工作。
Instead if you use any DBAL libraries then you can switch the configuration of the database and make sure your project will be up and running within a day (May be with some minor tweaking).
相反,如果您使用任何DBAL库,那么您可以切换数据库的配置,并确保您的项目将在一天内启动并运行(可能需要进行一些小的调整)。
ORM (Object Relational Mapper)
ORM(对象关系映射器)
Object Relational Mapping (ORM) is a technique (Design Pattern) of accessing a relational database from an object-oriented language.
对象关系映射(ORM)是一种从面向对象的语言访问关系数据库的技术(设计模式)。
If you have used any kind of frameworks like Symfony (if you come from PHP background)/Hibernate (Java), then your familiar with these. Its nothing but Entities.
如果您使用过任何类型的框架,如Symfony(如果您来自PHP背景)/ Hibernate(Java),那么您熟悉这些。它只不过是实体。
In order to access the database in Object Oriented context and interface translating the object logic is necessary, this interface is called as ORM. Its make up of the object that give access to data and keep the business rules with themselves.
为了在面向对象的上下文和接口中访问数据库,需要转换对象逻辑,此接口称为ORM。它由对象组成,可以访问数据并保持业务规则。
Eg.
class User{
private $email;
private $password;
public function setEmail($email){
$this->email = $email;
return $this;
}
public function getEmail(){
return $this->email;
}
public function setPassword($password){
$this->password = $password;
return $this;
}
public function getPassword(){
return $this->password;
}
}
/* To save User details you would do something like this */
$userObj = new User();
$userObj->setEmail('sanitizedEmail');
$userObj->setPassword('sanitizedPassword');
$userObj->save();
/* To fetch user details you would do something like this */
$userObj = new User();
$userDetails = $userObj->find($id);
例如。 Doctrine,Propel,RedBean
#5
#6
From Wikipedia: http://en.wikipedia.org/wiki/Object-relational_mapping
来自*:http://en.wikipedia.org/wiki/Object-relational_mapping
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.
计算机软件中的对象关系映射(ORM,O / RM和O / R映射)是用于在关系数据库和面向对象的编程语言中的不兼容类型系统之间转换数据的编程技术。这实际上创建了可以在编程语言中使用的“虚拟对象数据库”。虽然有些程序员选择创建自己的ORM工具,但有可用的免费和商业软件包可以执行对象关系映射。
Pros and cons ORM often reduces the amount of code needed to be written, making the software more robust (the fewer the lines of code in a program, the fewer the errors contained within them).[1].
优点和缺点ORM通常会减少需要编写的代码量,使软件更加健壮(程序中的代码行越少,其中包含的错误就越少)。[1]。
There are costs as well as benefits for using O/R mapping. For instance, some O/R mapping tools do not perform well during bulk deletions of data. Stored procedures may have better performance but are not portable.
使用O / R映射有成本和好处。例如,一些O / R映射工具在批量删除数据期间表现不佳。存储过程可能具有更好的性能但不可移植。
#7
It allows you to do things like this (this is Doctrine code):
它允许你做这样的事情(这是Doctrine代码):
$activeUsers = Doctrine::getTable('User')->createQuery('u')->addWhere('u.active = false');
foreach($activeUsers as $user)
{
$user->active = true;
$user->save();
}
#8
Object-relational mapping (ORM) libraries provide this mapping of database tables to domain object classes.
对象关系映射(ORM)库提供了数据库表到域对象类的映射。