在 Sinatra 之上构建 MVC 结构
我正在学习 Sinatra,我想知道是否有人知道为 Sinatra 项目制作 MVC 结构的好方法。我有一些想法,但它们对我来说似乎太麻烦了。
I'm learning Sinatra and I was wondering if someone knows a good way to make an MVC structure for a project with Sinatra. I've got some ideas but they seems too much cumbersome to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Sinatra 已经是“VC”了——你的视图与你的路由(控制器)分离。如果您愿意,可以选择将其分成多个文件;有关更多信息,请参阅这个答案(我的):
通过多个文件将 Sinatra 用于大型项目
要添加“M”(模型),请选择一个数据库框架。有些人喜欢 ActiveRecord。有些人喜欢DataMapper。您还可以选择更多。我个人喜欢并强烈推荐Sequel。我上面链接的答案还建议了用于包含模型的目录结构和外壳。一旦您在模型和控制器之间分配了适当的逻辑,您就拥有了“MVC”。
请注意,MVC 不是关于单独的文件,而是关注点分离。如果您按照我上面的建议设置了 Sinatra 应用程序,但让视图从模型中获取数据,或者您的路由直接生成 HTML(而不是通过“帮助程序”),那么您就不需要确实没有 MVC。相反,您可以在单个文件中执行上述所有操作,并且仍然拥有 MVC 应用程序。只需将数据完整性逻辑放入模型中(更重要的是放入数据库本身),将表示逻辑放入视图和可重用帮助器中,将映射逻辑放入控制器中。
Sinatra is already "VC" - you have views separated from your routes (controllers). You can choose to break it into multiple files if you like; for more on that, see this answer (mine):
Using Sinatra for larger projects via multiple files
To add an "M" (model), pick a database framework. Some people like ActiveRecord. Some people like DataMapper. There are many more from which you might choose. I personally love and highly recommend Sequel. My answer linked above also suggests a directory structure and shell for including the models. Once you distribute appropriate logic between your models and controllers, you have your "MVC".
Note that MVC is not about separate files, but separation of concerns. If you set up a Sinatra application as I suggest above, but have your views fetching data from your models, or you have your routes directly generating HTML (not through a "helper"), then you don't really have MVC. Conversely, you can do all of the above in a single file and still have an MVC application. Just put your data-integrity logic in your models (and more importantly, in the database itself), your presentation logic in your views and reusable helpers, and your mapping logic in your controllers.
如果您还没有了解过 Padrino 框架,那么值得一看,它提供了一组用于扩展 Sinatra 的组件。您可以使用部分或全部 Padrino,或者只是看看项目开发人员如何处理事情。
If you haven't already, it's worth taking a look at the Padrino framework, which provides a set of components for extending Sinatra. You can use some or all of Padrino, or just take a look at how the project developers have approached things.
M 很简单 - 使用 ActiveRecord(或其他)。我有一个
models
子目录,当我的 Sinatra 应用程序加载时,require
会包含其中的内容。V 也很简单 - 只需将您的视图放入
views
子目录中 - Sinatra 会自动在那里查找。我猜想,C 可能可以通过将适当分组的 Sinatra 操作放入单独的文件中并在运行时加载它们来处理。
(坦白说:我还没有构建一个足够复杂的 Sinatra 应用程序,还没有看到需要显式控制器 - 在需要那么多结构的地方,我已经从 Rails 开始了)
M is easy - use ActiveRecord (or whatever). I have a
models
subdirectory, the content of which getrequire
d in when my Sinatra app loads.V is also easy - just put your views in a
views
subdirectory - Sinatra will look there automatically.C can, I guess, probably be handled by placing suitably-grouped Sinatra actions into separate files and loading them at run-time.
(Confession: I haven't built a Sinatra app complex enough to see a need for explicit controllers yet - where that much structure was needed I've started with Rails)