每个MVC哪个项目结构是正确的? [英] Which project structure is correct as per MVC?

查看:133
本文介绍了每个MVC哪个项目结构是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到像 flask 这样的框架通常有一个名为 views 的模块来存放:

  @ app.route('/')
def index():
return render_template('index.html')

定义的类型,然后是 jinja2 模板在模板目录下,但是很多 node.js 框架(sails,geddy,locomotive)倾向于将 .ejs 模板放在 views 目录中,而不要使用模板目录。



看来这不应该是主观的;根据 MVC 模型是否正确?模板文件应该位于views目录下还是应该使用url处理程序定义?从我可以告诉, flask 应用程序似乎有正确的定义 views ;如果是这样的话,那么烧瓶控制器在哪里或是这些定义控制器呢?或错误的方式,MVC模式给你的关注点分离的指导方针,它不告诉你如何命名或组织事情。



这是我如何看待MVC组件映射到一个Flask应用程序:


  • M是最容易理解的,因为模型已经非常明确地指定了。有时候不完全清楚的是,模型不仅仅是数据库抽象,应用程序的业务逻辑也是模型的一部分。

  • 表示逻辑。在一个精心设计的Flask应用程序中,这是通过模板来处理的,所以我会说MVC模板== V. 基于从用户接收的输入来更新M和V.在Flask应用程序中,这些请求处理程序具有视图函数的一些误导性名称。当一个视图函数完成时,它会向用户返回一个更新的视图(呈现的模板)。

I noticed frameworks such as flask typically have a module named views to house the:

@app.route('/')
def index():
   return render_template('index.html')

type of definitions and then the jinja2 templates are under the templates directory, however a lot of the node.js frameworks (sails, geddy, locomotive) tend to put the .ejs templates in the views directory instead and have no templates directory.

It seems like this shouldn't be subjective; which is correct as per the MVC model? Should the template files be under the views directory or should the url handler definitions? From what I can tell, the flask application seems to have the correct definition of views; if this is in fact the case, where do flask controllers come in or are these definitions controllers too?

解决方案

There is no right or wrong way, the MVC pattern gives you the guidelines for the separation of concerns, it does not tell you how to name or organize things.

Here is how I think the MVC components map to a Flask app:

  • The M is the easiest to understand, as models are pretty clearly specified. What is sometimes not completely clear is that models are not just the database abstractions, the business logic of the application is also part of the models.

  • The V covers the presentation logic. In a well designed Flask application this is handled with templates, so I would say that in terms of MVC templates == V.

  • The C covers a thin layer that updates the M and the V based on input received from the user. In a Flask app these are the request handlers, which have the somewhat misleading name of "view functions". When a view function completes it returns an updated view (rendered template) to the user.

这篇关于每个MVC哪个项目结构是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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