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

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

问题描述

当我越来越多地了解OOP,并开始实施各种设计模式时,我会继续回到人们讨厌的情况。活动记录



通常,人们说它没有扩展(引用Twitter作为他们的主要例子) - 但是没有人实际解释 为什么 不能很好地扩展;和/或如何实现没有利益的AR的优点(通过相似但不同的模式?)



希望这不会变成关于设计模式的圣战 - 所有我想知道的是****具体****活动记录有什么问题。



如果它不能很好地缩放,为什么不? / p>

还有什么其他问题?

解决方案

ActiveRecord the Design Pattern ActiveRecord the Rails ORM Library ,还有一大堆针对.NET和其他语言的敲门。



<这些都是不同的东西。他们大多遵循这种设计模式,但是以许多不同的方式进行扩展和修改,所以在任何人说ActiveRecord Sucks之前,需要通过说哪个ActiveRecord,有堆?来限定?



我只熟悉Rails的ActiveRecord,我会尝试解决在使用它的上下文中提出的所有投诉。


@BlaM



我使用Active Records查看的问题是,它总是只在一个表




代码:

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

这将在company.id = person.company_id 上生成具有 LEFT JOIN公司的SQL,并自动生成关联的公司对象,以便您可以执行 people.first .company ,因为数据已经存在,所以不需要打数据库。 p>


@ pix0r



Active Record的固有问题是自动生成数据库查询并执行填充对象并修改数据库记录


代码:

  person = Person.find_by_sql(giant complex sql query)

这是不鼓励的,因为它是丑陋的,但是对于你简单而简单地需要编写原始SQL的情况,很容易完成。


Tim Sullivan



...并且您选择了几个模型实例,您基本上做了一个select * from ...


代码:

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

只会选择名称和ID列从数据库中,映射对象中的所有其他属性将仅为零,除非您手动重新生成加载该对象,等等。


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.

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?

What other problems does it have?

解决方案

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.

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?"

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 )

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")

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 Sullivan

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

Code:

people = Person.find(:all, :select=>'name, 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天全站免登陆