Model-Glue在Coldfusion中的模型和其他MVC框架中的模型一样吗?
如果您遵循 Model-Glue 官方文档提供的快速入门指南,请在此处找到:
看起来“模型”是执行应用程序操作的类。在此示例中,他们创建了一个 Translator 类,用于将短语翻译成 Pig Latin。从这里很容易推断出程序逻辑也应该是“模型”,例如数据库操作类和HTML帮助器。
然而,我最近收到了我在这里提出的有关 MVC 的问题的答案:
使用 MVC,如何设计视图,使其不需要了解所设置的变量 在其中一个答案中,有人提到MVC中的“模型
”应该是一个对象,控制器用数据填充该对象,然后将其传递给视图,视图将其用作用于呈现数据的强类型对象。这意味着,对于上面提供的 Model-Glue 示例,应该有一个翻译器控制器、一个翻译器视图、一个 PigLatinTranslator
类,以及一个看起来像这样的 Translation
模型:
component Translation
{
var TranslatedPhrase = "";
}
该控制器将像这样使用它:
component TranslatorController
{
public function Translate(string phrase)
{
var translator = new PigLatinTranslator();
var translation = new Translation();
translation.TranslatedPhrase = translator.Translate(phrase);
event.setValue("translation", translation);
}
}
并且视图将像这样渲染它:
<p>Your translated phrase was: #event.getValue("translation").TranslatedPhrase#</p>
在这种情况下,PigLatinTranslator
只是一个驻留在某处的类,以及不能被视为模型、控制器或视图。
我的问题是,ColdFusion Model-Glue 的模型与 MVC 模型不同吗?或者他们提供的快速入门指南是一个糟糕的 MVC 示例,而我上面列出的代码是正确的方法吗?或者我完全偏离了这一切?
If you follow the quickstart guide provided by the official Model-Glue docs, found here:
It will seem like the "model" is a class that performs an application operation. In this example, they created a Translator
class that will translate a phrase into Pig Latin. It's easy to infer from here that the program logic should also be "models", such as database operation classes and HTML helpers.
However, I recently received an answer for a question I asked here about MVC:
In one of the answers, it was mentioned that the "model" in MVC should be an object that the controller populates with data, which is then passed to the view, and the view uses it as a strongly-typed object to render the data. This means that, for the Model-Glue example provided above, there should've been a translator controller, a translator view, a PigLatinTranslator
class, and a Translation
model that looks like this:
component Translation
{
var TranslatedPhrase = "";
}
This controller will use it like this:
component TranslatorController
{
public function Translate(string phrase)
{
var translator = new PigLatinTranslator();
var translation = new Translation();
translation.TranslatedPhrase = translator.Translate(phrase);
event.setValue("translation", translation);
}
}
And the view will render it like this:
<p>Your translated phrase was: #event.getValue("translation").TranslatedPhrase#</p>
In this case, the PigLatinTranslator
is merely a class that resides somewhere, and cannot be considered a model, controller, or a view.
My question is, is ColdFusion Model-Glue's model different than a MVC model? Or is the quickstart guide they provided a poor example of MVC, and the code I listed above the correct way of doing it? Or am I completely off course on all of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可能陷入了实施细节的困境。
我对(一般)MVC 的理解如下:
这是故意非常抽象的。
我认为 MG 文档中的示例适当地实现了这一点,尽管该示例非常人为。控制器调用处理数据的模型(将输入转换为输出),然后设置结果。然后控制器调用获取数据并显示数据的视图。
我不同意这个问题的前提“使用 MVC,我如何设计视图,以便它不需要了解控制器设置的变量?”视图不应该关心数据来自哪里,它应该只知道它需要什么数据,并从[某处]获取它。系统中确实需要有一个约定,模型将要使用的数据放在某个地方,视图从某个地方获取它需要的数据(否则它怎么可能工作?);解耦是模型只是将数据放在被告知的地方,而视图只是从被告知的地方获取数据。控制器(或正在使用的 MVC 系统的约定)决定了如何实现。我不认为 MG 处理这个问题的方式违反了 MVC 的任何原则。
就这个说法而言,“在这种情况下,PigLatinTranslator 只是一个驻留在某处的类,不能被视为模型、控制器或视图。”嗯...是的...所有模型都是一些代码。因此 PigLatinTranslator.cfc 在这里对业务逻辑进行建模。
而这个:“在其中一个答案中,有人提到 MVC 中的“模型”应该是控制器用数据填充的对象,然后将其传递给视图”......我不认为这是正确的。控制器只是争论需要调用哪些模型和哪些视图来满足要求,以及它们之间可能的数据交换(尽管这也可以按照惯例完成)。控制器不进行数据处理;它决定需要进行哪些数据处理。
最后,我不认为“强类型”注释在 CF 环境中是相关的或适当的考虑因素,因为 CF 不是强类型的。这是特定于平台的考虑因素,与 MVC 原则无关。
I think perhaps you're getting bogged down in the specifics of implementation.
My understanding of (general) MVC is as follows:
That's purposely very abstract.
I think the example in the MG docs implement this appropriately, although the example is pretty contrived. The controller calls the model which processes the data (an input is converted into an output), and then sets the result. The controller then calls the view which takes the data and displays it.
I disagree with the premise of this question "Using MVC, how do I design the view so that it does not require knowledge of the variables being set by the controller?" The view should not care where the data comes from, it should just know what data it needs, and grab it from [somewhere]. There does need to be a convention in the system somewhere that the model puts the data to be used somewhere, and the view gets the data it needs from somewhere (otherwise how would it possibly work?); the decoupling is that model just puts the data where it's been told, and the view just gets the data out from where it's been told. The controller (or the convention of the MVC system in use) dictates how that is implemented. I don't think MG is breaking any principles of MVC in the way it handles this.
As far as this statement goes "In this case, the PigLatinTranslator is merely a class that resides somewhere, and cannot be considered a model, controller, or a view." Well... yeah... all a model IS is some code. So PigLatinTranslator.cfc models the business logic here.
And this one: "In one of the answers, it was mentioned that the "model" in MVC should be an object that the controller populates with data, which is then passed to the view"... I don't think that is correct. The controller just wrangles which models and which views need to be called to fulfil the requirement, and possible interchanges data between them (although this could be done by convention, too). The controller doesn't do data processing; it decides which data processing needs to be done.
Lastly, I don't think the "strongly-typed" commentary is relevant or an apporpriate consideration in a CF environment because CF is not strongly typed. That is a platform-specific consideration, and nothing to do with MVC principles.
我认为 MVC 的常见困惑之一是有多个视图、多个控制器,但只有一个模型。 cfWheels 对于每个持久域对象都有一个“模型”对象,我认为这非常令人困惑 - 但当然 cfWheels 是从 Ruby on Rails 中提取的,它也在该上下文中使用“模型”。
一般来说,在 MVC 中,“模型”代表整个业务数据和逻辑。该模型由许多域对象(通常是持久性的)和许多服务对象(其存在是为了协调跨多个域对象的操作)组成。在现实世界的应用程序中,您通常有一个数据层来管理域对象的持久性 - 可以通过多种方式进行分区。
它还可能有助于将视图所需的输入数据视为“API”,并且控制器的工作是通过提供兼容的数据来满足该 API。更多地认为控制器需要知道什么类型的数据将满足视图而不是相反。
I think one of the common confusions around MVC is that there are multiple Views, multiple Controllers but only one Model. cfWheels has a "model" object for each persistent domain object which I think is very confusing - but of course cfWheels is drawn from Ruby on Rails which also uses "model" in that context.
In general, in MVC, "The Model" represents your business data and logic as a whole. The Model is made up of a number of domain objects (which are typically persistent) and a number of service objects (which exist to orchestrate operations across multiple domain objects). In real world applications, you typically have a data layer that manages persistence of domain objects - which may be partitioned in a number of ways.
It may also help to think of the input data that the view needs as it's "API" and it is the controller's job to satisfy that API by providing compatible data. Think of it more that the controller needs to know what type of data will satisfy the view rather than the other way around.