按照MVC,哪个项目结构是正确的?

时间:2021-05-31 11:25:38

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

我注意到,flask等框架通常都有一个名为views的模块,用来存放以下内容:

@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.

定义的类型,然后jinja2模板在templates目录下,但是很多节点。js框架(sail, geddy, locomotive)倾向于将.ejs模板放在视图目录中,而没有模板目录。

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?

看起来这不应该是主观的;根据MVC模型,哪个是正确的?模板文件应该在视图目录下还是url处理程序定义下?据我所知,flask应用程序似乎有正确的视图定义;如果这是事实,那么flask控制器是在哪里出现的呢,还是这些定义控制器呢?

2 个解决方案

#1


2  

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.

没有正确或错误的方法,MVC模式为您提供了关注点分离的指导原则,它没有告诉您如何命名或组织事物。

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

下面是我如何将MVC组件映射到Flask应用程序:

  • 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.

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

  • 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.

    V包含表示逻辑。在一个设计良好的Flask应用程序中,这是通过模板来处理的,所以我要说的是MVC模板== 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.

    C包含一个薄层,根据用户的输入更新M和V。在Flask应用程序中,这些是请求处理程序,它们有一个有点误导的名称“视图函数”。当视图函数完成时,它将向用户返回一个更新的视图(呈现的模板)。

#2


2  

When it comes to frameworks like Flask or Django, they rather use MVT (Model/View/Template) but it is similar to MVC except for terminology. The difference is that the 'T' in MVT stands for Template which is actually the Controller in MVC. So View in Flask is the same as Controller.

当涉及到Flask或Django等框架时,它们更喜欢使用MVT(模型/视图/模板),但是除了术语外,它与MVC相似。不同的是,MVT中的T代表的是模板,而模板实际上是MVC中的控制器。所以Flask的视图和控制器是一样的。

So don't think of it as templates vs views but focus on the part that you still have 3 components: Model, Business Logic (view/controllers etc), Visuals (template/html etc)

不要把它看成是模板和视图,而要把重点放在仍有3个组件的部分:模型、业务逻辑(视图/控制器等)、可视化(模板/html等)

#1


2  

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.

没有正确或错误的方法,MVC模式为您提供了关注点分离的指导原则,它没有告诉您如何命名或组织事物。

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

下面是我如何将MVC组件映射到Flask应用程序:

  • 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.

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

  • 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.

    V包含表示逻辑。在一个设计良好的Flask应用程序中,这是通过模板来处理的,所以我要说的是MVC模板== 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.

    C包含一个薄层,根据用户的输入更新M和V。在Flask应用程序中,这些是请求处理程序,它们有一个有点误导的名称“视图函数”。当视图函数完成时,它将向用户返回一个更新的视图(呈现的模板)。

#2


2  

When it comes to frameworks like Flask or Django, they rather use MVT (Model/View/Template) but it is similar to MVC except for terminology. The difference is that the 'T' in MVT stands for Template which is actually the Controller in MVC. So View in Flask is the same as Controller.

当涉及到Flask或Django等框架时,它们更喜欢使用MVT(模型/视图/模板),但是除了术语外,它与MVC相似。不同的是,MVT中的T代表的是模板,而模板实际上是MVC中的控制器。所以Flask的视图和控制器是一样的。

So don't think of it as templates vs views but focus on the part that you still have 3 components: Model, Business Logic (view/controllers etc), Visuals (template/html etc)

不要把它看成是模板和视图,而要把重点放在仍有3个组件的部分:模型、业务逻辑(视图/控制器等)、可视化(模板/html等)