Data Mapper是一种比Active Record更现代化的趋势

时间:2022-10-03 11:43:13

I've come across a couple of ORMs that recently announced they are planning to move their implementation from Active Record to Data Mapper. My knowledge of this subject is very limited. So a question for those who know better, is Data Mapper newer than Active Record? Was it around when the Active Record movement started? How do the two relate together?

我遇到了几个最近宣布他们计划将他们的实现从Active Record转移到Data Mapper的ORM。我对这个主题的了解非常有限。对于那些了解得更好的人来说,问题是Data Mapper比Active Record更新吗?当Active Record运动开始时它是否存在?这两者如何联系在一起?

Lastly since I'm not a database person and know little about this subject, should I follow an ORM that's moving to the Data Mapper implementation, as in what's in it for me as someone writing software (not a data person)?

最后因为我不是一个数据库人并且对这个主题知之甚少,我是否应该遵循正在转向数据映射器实现的ORM,就像在编写软件(而不是数据人)时为我做什么?

3 个解决方案

#1


20  

The DataMapper is not more modern or newer, but just more suited for an ORM.

DataMapper不是更现代或更新,但更适合ORM。

The main reason people change is because ActiveRecord does not make for a good ORM. An AR wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. So by definition, an AR is a 1:1 representation of a database record, which makes it particular suited for simple CRUD.

人们改变的主要原因是因为ActiveRecord不能提供良好的ORM。 AR在数据库表或视图中包装行,封装数据库访问,并在该数据上添加域逻辑。因此,根据定义,AR是数据库记录的1:1表示,这使得它特别适用于简单的CRUD。

Some people added fetching of related data to their AR, which made people believe AR is an ORM. It is not. The point of an ORM is to tackle the object relational impedance mismatch between your database structure and your domain objects. When using AR, you dont have this impedance mismatch because your AR represents a database row and not a proper OO design. You are tieing your db layout to your objects. Some of the object-relational behavioral patterns can still be applied though (for instance lazy loading).

有些人添加了相关数据到他们的AR,这使人们相信AR是一个ORM。不是这样。 ORM的要点是解决数据库结构和域对象之间的对象关系阻抗不匹配问题。使用AR时,您没有阻抗不匹配,因为您的AR代表数据库行而不是正确的OO设计。您正在将数据库布局绑定到对象。一些对象关系行为模式仍然可以应用(例如延迟加载)。

Another reason why AR is often criticised is because it intermingles two concerns: business logic and db access logic. This leads to unwanted coupling and can result in less maintainability and flexibility in larger applications. There is no isolation between the two layers. Coupling always leads to less flexibility.

AR经常被批评的另一个原因是它混淆了两个问题:业务逻辑和数据库访问逻辑。这导致不希望的耦合,并且可能导致较大应用中较少的可维护性和灵活性。两层之间没有隔离。耦合总是导致灵活性降低。

A DataMapper on the other hand moves data between objects and a database while keeping them independent of each other and the mapper itself. While more difficult to implement, it allows for much more flexible design in your application. Your domain objects no longer have to match the db structure. DAL and Domain layer are decoupled.

另一方面,DataMapper在对象和数据库之间移动数据,同时保持它们彼此独立以及映射器本身。虽然更难实现,但它允许在您的应用程序中进行更灵活的设计。您的域对象不再必须与db结构匹配。 DAL和域层是分离的。

#2


0  

my 2 cents:

我的2美分:

DataMapper is newer than ActiveRecord. I like DataMapper's syntax better; it is more explicit and less "magical". I could go through more reasons, but they are listed here: http://datamapper.org/why

DataMapper比ActiveRecord更新。我更喜欢DataMapper的语法;它更明确,更少“神奇”。我可以通过更多理由,但它们列在这里:http://datamapper.org/why

ActiveRecord has a lot more documentation.

ActiveRecord有更多的文档。

#3


0  

Even though the post is 8 years old, the question is still valid in 2018.

即使这篇文章是8岁,这个问题在2018年仍然有效。

Active record is Anti pattern beware of that. It creates a very tight coupling between code and database. It might not be a problem for small simple projects. However, I would strongly recommend to avoid using it in anything bigger.

活动记录是反模式提防。它在代码和数据库之间创建了非常紧密的耦合。对于小型简单项目来说,这可能不是问题。但是,我强烈建议避免在更大的范围内使用它。

A good OOP design is done in layers. Input layer, service layer, repository layer, data mapper and DB - just a simple example. You should not mix Input layer with the DB. How this can be done? For example, in Laravel, you can use a Validator rule like this:

良好的OOP设计分层完成。输入层,服务层,存储库层,数据映射器和数据库 - 只是一个简单的例子。您不应将输入图层与数据库混合使用。怎么做到这一点?例如,在Laravel中,您可以使用如下的Validator规则:

'email' => 'exists:staff,email'

It checks whether the email exists in the table staff. This is a complete OOP non-sense. It tights your top layer with the DB column name. I cannot imagine any better example of a bad OOP design.

它检查表格工作人员中是否存在电子邮件。这是一个完整的OOP无意义。它使用DB列名称来压缩顶层。我无法想象任何糟糕的OOP设计的更好的例子。

The bottom line - if you are creating a simple site with 2-3 tables, like a blog, Active record might not be a problem. For anything bigger, go for Data Mapper and be careful about OOP principles such as IoC, SoC, etc.

底线 - 如果您要创建一个包含2-3个表的简单站点,如博客,活动记录可能不是问题。对于任何更大的东西,请使用Data Mapper并注意OOP原则,如IoC,SoC等。

#1


20  

The DataMapper is not more modern or newer, but just more suited for an ORM.

DataMapper不是更现代或更新,但更适合ORM。

The main reason people change is because ActiveRecord does not make for a good ORM. An AR wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data. So by definition, an AR is a 1:1 representation of a database record, which makes it particular suited for simple CRUD.

人们改变的主要原因是因为ActiveRecord不能提供良好的ORM。 AR在数据库表或视图中包装行,封装数据库访问,并在该数据上添加域逻辑。因此,根据定义,AR是数据库记录的1:1表示,这使得它特别适用于简单的CRUD。

Some people added fetching of related data to their AR, which made people believe AR is an ORM. It is not. The point of an ORM is to tackle the object relational impedance mismatch between your database structure and your domain objects. When using AR, you dont have this impedance mismatch because your AR represents a database row and not a proper OO design. You are tieing your db layout to your objects. Some of the object-relational behavioral patterns can still be applied though (for instance lazy loading).

有些人添加了相关数据到他们的AR,这使人们相信AR是一个ORM。不是这样。 ORM的要点是解决数据库结构和域对象之间的对象关系阻抗不匹配问题。使用AR时,您没有阻抗不匹配,因为您的AR代表数据库行而不是正确的OO设计。您正在将数据库布局绑定到对象。一些对象关系行为模式仍然可以应用(例如延迟加载)。

Another reason why AR is often criticised is because it intermingles two concerns: business logic and db access logic. This leads to unwanted coupling and can result in less maintainability and flexibility in larger applications. There is no isolation between the two layers. Coupling always leads to less flexibility.

AR经常被批评的另一个原因是它混淆了两个问题:业务逻辑和数据库访问逻辑。这导致不希望的耦合,并且可能导致较大应用中较少的可维护性和灵活性。两层之间没有隔离。耦合总是导致灵活性降低。

A DataMapper on the other hand moves data between objects and a database while keeping them independent of each other and the mapper itself. While more difficult to implement, it allows for much more flexible design in your application. Your domain objects no longer have to match the db structure. DAL and Domain layer are decoupled.

另一方面,DataMapper在对象和数据库之间移动数据,同时保持它们彼此独立以及映射器本身。虽然更难实现,但它允许在您的应用程序中进行更灵活的设计。您的域对象不再必须与db结构匹配。 DAL和域层是分离的。

#2


0  

my 2 cents:

我的2美分:

DataMapper is newer than ActiveRecord. I like DataMapper's syntax better; it is more explicit and less "magical". I could go through more reasons, but they are listed here: http://datamapper.org/why

DataMapper比ActiveRecord更新。我更喜欢DataMapper的语法;它更明确,更少“神奇”。我可以通过更多理由,但它们列在这里:http://datamapper.org/why

ActiveRecord has a lot more documentation.

ActiveRecord有更多的文档。

#3


0  

Even though the post is 8 years old, the question is still valid in 2018.

即使这篇文章是8岁,这个问题在2018年仍然有效。

Active record is Anti pattern beware of that. It creates a very tight coupling between code and database. It might not be a problem for small simple projects. However, I would strongly recommend to avoid using it in anything bigger.

活动记录是反模式提防。它在代码和数据库之间创建了非常紧密的耦合。对于小型简单项目来说,这可能不是问题。但是,我强烈建议避免在更大的范围内使用它。

A good OOP design is done in layers. Input layer, service layer, repository layer, data mapper and DB - just a simple example. You should not mix Input layer with the DB. How this can be done? For example, in Laravel, you can use a Validator rule like this:

良好的OOP设计分层完成。输入层,服务层,存储库层,数据映射器和数据库 - 只是一个简单的例子。您不应将输入图层与数据库混合使用。怎么做到这一点?例如,在Laravel中,您可以使用如下的Validator规则:

'email' => 'exists:staff,email'

It checks whether the email exists in the table staff. This is a complete OOP non-sense. It tights your top layer with the DB column name. I cannot imagine any better example of a bad OOP design.

它检查表格工作人员中是否存在电子邮件。这是一个完整的OOP无意义。它使用DB列名称来压缩顶层。我无法想象任何糟糕的OOP设计的更好的例子。

The bottom line - if you are creating a simple site with 2-3 tables, like a blog, Active record might not be a problem. For anything bigger, go for Data Mapper and be careful about OOP principles such as IoC, SoC, etc.

底线 - 如果您要创建一个包含2-3个表的简单站点,如博客,活动记录可能不是问题。对于任何更大的东西,请使用Data Mapper并注意OOP原则,如IoC,SoC等。