MVC模式vs三层 - 逻辑属于哪里?

时间:2021-05-29 11:27:18

I am trying to introduce some good practices into my website coding, so I started to look into MVC, since it is a buzzword in website designing :-) I am confused by the MVC pattern though. I am used to thinking in a Three-tier pattern, where you have 3 layers:

我试图在我的网站编码中引入一些好的做法,所以我开始研究MVC,因为它是网站设计的流行语:-)我对MVC模式感到困惑。我习惯于以三层模式思考,你有三层:

  • presentation
  • logic
  • data

Two things confuse me around MVC:

有两件事让我对MVC感到困惑:

  1. "Model" component is often presented as the data layer above (database abstraction). But where does the "high-level" logic belong to? Like deciding what you will do with the data and how, checking permissions etc. Sometimes I've seen some of this in the controller, but it is really confusing for me to decide which belongs where.

    “模型”组件通常表示为上面的数据层(数据库抽象)。但“高级”逻辑属于哪里呢?就像决定你将如何处理数据以及如何处理,检查权限等。有时候我在控制器中看到了一些这样的东西,但是我确定哪个属于哪个地方真的很混乱。

  2. The MVC pattern is presented as a circle of 3 components which send messages to each other, like M -> V, V -> C, C -> M, and the other way around. I understand perfectly the Three-tier design, where one layer calls the layer below itself, but not the other way around! The functions in your programming language can call other functions (you can call it "sending a message") - but it is an oriented tree graph. But how can the lower layer, or, how can the called function "send a message" or "notify" the calling function?

    MVC模式呈现为3个组件的圆圈,它们相互发送消息,如M - > V,V - > C,C - > M,反之亦然。我完全理解三层设计,其中一层称自己下面的层,但不是相反!编程语言中的函数可以调用其他函数(可以称之为“发送消息”) - 但它是一个面向对象的树形图。但是下层怎么可以,或者,被叫函数如何“发送消息”或“通知”调用函数?

Maybe I am too much concerned by the MVC pattern and could happily stay with my Three-tier designing? Anyway, I would like to understand MVC pattern to at least see if it is worth using for me.

也许我太关心MVC模式,并且可以愉快地继续我的三层设计?无论如何,我想了解MVC模式,至少看看它是否值得为我使用。

1 个解决方案

#1


0  

The model is another way of saying your domain knowledge, your controller should be deciding what models to display, update, create, and your view should just present the data the controller has decided to present. To address your second question the model is normally passed to the view through the controller to the view for the data to be presented.

该模型是另一种表达您的领域知识的方式,您的控制器应该决定显示,更新,创建哪些模型,并且您的视图应该只显示控制器决定呈现的数据。为了解决您的第二个问题,模型通常通过控制器传递到视图,以显示要呈现的数据。

For more details scroll down the the Model View Controller section of this page

有关更多详细信息,请向下滚动此页面的“模型视图控制器”部分

#1


0  

The model is another way of saying your domain knowledge, your controller should be deciding what models to display, update, create, and your view should just present the data the controller has decided to present. To address your second question the model is normally passed to the view through the controller to the view for the data to be presented.

该模型是另一种表达您的领域知识的方式,您的控制器应该决定显示,更新,创建哪些模型,并且您的视图应该只显示控制器决定呈现的数据。为了解决您的第二个问题,模型通常通过控制器传递到视图,以显示要呈现的数据。

For more details scroll down the the Model View Controller section of this page

有关更多详细信息,请向下滚动此页面的“模型视图控制器”部分