如何在 Rails 应用程序中实现模块化?
我有一个快速关于大型 Ruby on Rails 应用程序中的模块化的问题。
设置:
我正在构建一个存储有关人员信息的核心应用程序。我还有几个“模块”以非常不同的方式使用这些信息。 (例如,一个可以显示有关人员的信息,另一个可以找出他们之间的联系和共性,等等)。
问题:
如何有效地模块化这个应用程序?
可能的答案:
由于模块与核心应用程序(以及彼此)共享模型和视图,因此我将它们合并到一个应用程序中是有意义的。然而,随着应用程序的增长,这显然会导致问题。对我来说,这建议要么为控制器和模型命名空间“如何组织中等大型 Rails 应用程序中的控制器?”或使用引擎“模块化Rails 应用程序”。
由于模块正在积极开发中,因此在它们上使用轨道生成器非常有帮助,这似乎使使用引擎变得很痛苦。似乎虽然从 Rails 的角度来看引擎得到了完全支持,但由于缺乏生成器支持和数据库迁移,它们看起来仍然相当老套。有人有成功开发发动机的经验吗?如果您有一个正在运行的应用程序并希望将其移植到插件中(即复制粘贴代码),那么引擎似乎是一个很好的解决方案,但如果您正在积极开发它(更改模型等),那就会很困难。
我见过的最后一件事是使用多个应用程序和一个数据库。这种方式看起来像是迁移和保持模型直线等方面的巨大痛苦。但我想我也会对此有所思考。
Breaking a large rails app into smaller apps?
Modularizing Rails applications
Best practice for structuring a 'large' Rails app
I have a quick question on modularization in a large Ruby on Rails App.
Setup:
I am building a core app that stores information about people. I also have several 'modules' that use that information in very different ways. (e.g. one could display information about the people, another could figure out connections and commonalities between them, etc).
The question:
How do I modularize this app efficiently?
Potential Answers:
Since the modules share models and views with the core app (and eachother) it makes sense for me to combine them into one app. However, as the app grows this will obviously lead to problems. This to me suggests either Namespacing the controllers and models "How to organize controller in moderately large Rails application?" or using engines "Modularizing Rails applications".
Since the modules are in active development it is very helpful to use rails generators on them, which seems to make using Engines a pain in the butt. It also seems that while engines are fully supported from a Rails point of view, they still seem rather hacky with regard to lack of generator support and database migrations. Does anyone have experience with developing engines successfully? It seems like engines would be a great solution if you had a working app and wanted to port it into a plugin (i.e. copy paste code) but if you are actively developing it (changing models etc) it would be difficult.
The last thing I have seen around is using multiple apps and one database. This way seems like a royal pain with migrations and keeping models straight etc etc. But I thought I would get thoughts on that as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Rails 引擎似乎是适合您的解决方案。我在使用 Refinery CMS 时使用过引擎,它构建在 Rails Engines 之上。
以下是一些讨论 Rails 引擎的好链接:
http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/
http: //www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/
Rails engines seem to be the correct solution for you. I have used engines when I worked with Refinery CMS, which is built on top of Rails Engines.
The following are some nice links which talks about Rails Engines:
http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/
http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/
我不会使用引擎。引擎旨在用于在应用程序之间共享功能。您想要做的不是共享功能,而是对其进行组织。
为了组织你的代码,你可以做很多事情。
Andrzej 有关于 DCI 的非常好的文章。
http://andrzejonsoftware.blogspot.com /2011/08/dci-patterns-how-to-write-dci-contexts.html
I wouldn't use engines. Engines are meant to be used to share functionality between apps. What you want to do is not to share functionality but to organise it.
In order to organise your code you can do a lot of things.
Andrzej has very good articles about DCI.
http://andrzejonsoftware.blogspot.com/2011/08/dci-patterns-how-to-write-dci-contexts.html
您还可以使用机架中间件来处理特定任务。
对于您的大部分应用程序,引擎似乎是最好的解决方案 - 这也是我正在考虑做的事情。看起来您可以在引擎中轻松定义生成器。
You can also use Rack Middleware to handle specific tasks.
For the bulk of your application, Engines seem like the best solution – it's something I'm looking at doing too. Looks like you can define generators in the Engine easily enough.
根据数据的结构和访问模式,将应用程序分成多个应用程序可能会很有用,并且可能还可以通过(RESTful)API 提供数据访问。
根据我的经验,当您的应用程序从中型增长到大型时,这可以实现最佳结构,并迫使您考虑结构和关注点分离。扩大规模通常也更容易。
Depending on the structure of the data, and access patterns, it might be useful to separate the app into multiple apps, and possibly, additionally, provide data access by (RESTful) APIs.
In my experience, this allows for the best structures when your application(s) grow from middle to large size, and forces you to think about structures and separation of concern. Scaling up is also usually easier.