如何在 Rails 应用程序中实现模块化?

发布于 2024-11-25 16:41:22 字数 1233 浏览 3 评论 0原文

将大型 Rails 应用程序分解为较小的应用程序?

模块化 Rails 应用程序

构建“大型”Rails 应用程序的最佳实践

我有一个快速关于大型 Rub​​y 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不会使用引擎。引擎旨在用于在应用程序之间共享功能。您想要做的不是共享功能,而是对其进行组织。

为了组织你的代码,你可以做很多事情。

  • 使用命名空间。
  • 保持控制器的纤薄。
  • 保持模型精简(是的,您的模型;请参阅下一个要点)
  • 使用数据上下文交互 (DCI) 模式。
  • 使用 Apotomo 或 Cells 等小部件框架。
  • 编写测试,以便您可以重构。
  • 如果您的应用程序的责任增长太多,请考虑面向服务的架构(考虑超媒体 API 设计)。

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.

  • Use namespaces.
  • Keep your controllers thin.
  • Keep your models thin (Yes, your models; see next bullet point)
  • Use the Data Context Interaction (DCI) pattern.
  • Use a widget framework like Apotomo, or Cells.
  • Write tests, so you can refactor.
  • Consider a Service Oriented Architecture(Consider Hypermedia API design) if your app's responsability grows too much.

Andrzej has very good articles about DCI.

http://andrzejonsoftware.blogspot.com/2011/08/dci-patterns-how-to-write-dci-contexts.html

那支青花 2024-12-02 16:41:22
  • 一个应用程序,一个数据库。
  • 在模块之间共享模型。
  • 为您的控制器和视图命名空间。
  • 测试。
  • One application, one database.
  • Share the models between the modules.
  • Namespace your controllers and views.
  • Test.
掐死时间 2024-12-02 16:41:22

您还可以使用机架中间件来处理特定任务。

对于您的大部分应用程序,引擎似乎是最好的解决方案 - 这也是我正在考虑做的事情。看起来您可以在引擎中轻松定义生成器

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.

格子衫的從容 2024-12-02 16:41:22

根据数据的结构和访问模式,将应用程序分成多个应用程序可能会很有用,并且可能还可以通过(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文