为什么所有的活动记录恨? [英] Why all the Active Record hate?

查看:127
本文介绍了为什么所有的活动记录恨?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解更多有关面向对象,并开始实施各种设计模式,我继续回来的情况下,人们都恨上的活动记录

As I learn more and more about OOP, and start to implement various design patterns, I keep coming back to cases where people are hating on Active Record.

通常情况下,人们会说,它并不能很好地扩展(援引Twitter作为其典型的例子) - 但没有人真正解释的为什么它没有很好地扩展;和/或如何实现AR的优点没有缺点的(通过相似,但不同的模式?)

Often, people say that it doesn't scale well (citing Twitter as their prime example) -- but nobody actually explains why it doesn't scale well; and / or how to achieve the pros of AR without the cons (via a similar but different pattern?)

希望这不会变成关于设计模式的圣战 - 这一切我想知道的是 **专** 什么是错的活动记录

Hopefully this won't turn into a holy war about design patterns -- all I want to know is **specifically** what's wrong with Active Record.

如果它不能很好地扩展,为什么不呢?

If it doesn't scale well, why not?

它有什么其他的问题?

推荐答案

的ActiveRecord的设计模式并的 ActiveRecord的Rails的ORM图书馆和另外还有大量的翻版为.NET和其他语言。

There's ActiveRecord the Design Pattern and ActiveRecord the Rails ORM Library, and there's also a ton of knock-offs for .NET, and other languages.

这些都是不同的事情。他们大多遵循的设计模式,而是扩展和修改它在许多不同的方式,所以任何人都表示,之前的ActiveRecord的吮它需要说合格的ActiveRecord的,有堆?

These are all different things. They mostly follow that design pattern, but extend and modify it in many different ways, so before anyone says "ActiveRecord Sucks" it needs to be qualified by saying "which ActiveRecord, there's heaps?"

我只熟悉Rails的ActiveRecord的,我会尽力解决所有这些都在使用它的上下文中提出的投诉。

I'm only familiar with Rails' ActiveRecord, I'll try address all the complaints which have been raised in context of using it.

@BlaM

这是我用的活动记录中看到的问题是,它始终只是一个表

The problem that I see with Active Records is, that it's always just about one table

code:

class Person
    belongs_to :company
end
people = Person.find(:all, :include => :company )

这生成SQL与 LEFT JOIN上companies.id = person.company_id 公司,并自动生成相关的公司对象,所以你可以做的人。 first.company ,它并不需要访问数据库,因为数据已经是present。

This generates SQL with LEFT JOIN companies on companies.id = person.company_id, and automatically generates associated Company objects so you can do people.first.company and it doesn't need to hit the database because the data is already present.

@ pix0r

固有的问题,活动记录是数据库查询会自动生成并执行填充物和修改数据库记录

The inherent problem with Active Record is that database queries are automatically generated and executed to populate objects and modify database records

code:

person = Person.find_by_sql("giant complicated sql query")

这是气馁,因为它的丑陋,但对于那些你只是普通的,只是需要编写原始SQL的情况下,它是很容易做到。

This is discouraged as it's ugly, but for the cases where you just plain and simply need to write raw SQL, it's easily done.

@Tim沙利文

...你选择模型的多个实例,你基本上是做一个SELECT * FROM ...

...and you select several instances of the model, you're basically doing a "select * from ..."

code:

people = Person.find(:all, :select=>'name, id')

这将只在映射的对象从数据库中选择的名称和ID列,其他所有的属性只会是零,除非你手动重新加载的对象,等等。

This will only select the name and ID columns from the database, all the other 'attributes' in the mapped objects will just be nil, unless you manually reload that object, and so on.

这篇关于为什么所有的活动记录恨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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