谁负责下一个视图?

发布于 2024-09-18 21:52:08 字数 230 浏览 6 评论 0原文

在典型的 MVC 架构中,决定接下来显示哪个视图的逻辑在哪里?

假设是某种具有多个视图(窗口)的应用程序,这些视图在不同时间可能可见或不可见,具体取决于用户操作。例如,有时应用程序可能需要用户填写包含其他详细信息的表单,有时则可能不需要。控制器有责任要求特定视图变得可见吗?

我这个想法是不是错了?也许控制器决定哪个其他控制器应该承担控制权,而视图只是根据哪个控制器处于活动状态而更新?

我很困惑。

In an archetypical MVC architectur, where does the logic go, that determines which view to show next?

The assumption is some kind of app with several views (windows) that may or may not be visible at different times, depending on user actions. For instance, sometimes the application may need the user to fill out a form with additional details, other times it may not. Is it the controllers responsibility to ask for a specifik view to become visible?

Am I thinking about this wrong? Maybe the controller determines which other controller should assume control, and the view just updates according to which controller is active?

I'm confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2024-09-25 21:52:08

控制器是MVC模式中的老大。它负责委派,同时自己完成最少的工作;)。

一个请求进来,老板看着它,心想“嗯……这里涉及很多工作。我需要从我的数据库中获取一些数据。幸好我有下属!嘿模型,过来这里。我需要你去从 DAL 获取所有信息。”

将模型视为开发人员。做所有真正艰苦的工作,亲自动手,如果没有模型,整个系统就会崩溃。它会启动并执行所有业务和领域逻辑。然后它跑回老板那里,交出他所有的辛苦工作。 “给你,政府”。

然后老板需要将数据返回给用户。再说一遍——对他来说工作太多了。所以他找了一个愚蠢的初级开发人员来做这件事。 (并非所有的初级者都是愚蠢的,但它适用于这个例子!)。视图什么都不知道。它并不关心数据是如何产生的,只关心数据存在并且有效。老板可能会对数据进行一些小的更改,因为我们不希望视图承担超出其需要的责任,然后视图就会关闭并显示数据。根据工作量的不同,老板可能会为该工作招募多个观点。

基本上,控制器控制 MVC 模式中的所有流程。它决定调用哪些模型、渲染哪些视图以及将模型中的哪些数据传递到视图 (ViewModels)。

您可以从一个控制器调用其他控制器。例如,如果您的主页上有一个搜索框,当用户点击搜索时,它可以调用搜索控制器上的索引函数。在我上面的比喻中,这类似于一个部门将工作转移到另一个部门。然后整个过程再次开始,老板为当前的要求招募他所需的员工。

The controller is the boss in the MVC pattern. It is responsible for delegation whilst doing minimal work itself ;).

A request comes in and the boss looks at it and thinks "Hrm... there is a lot of work involved here. I need to get some data from my database. Luckily I have subordinates! Hey model, come over here. I need you to go off and get all the information form the DAL."

Think of the model as a developer. Does all the real hard work, gets his hands dirty, and without the model, the whole system would come crashing down. It goes off and does all the business and domain logic. It then runs back to the boss and hands in all his hard work. "Here you go gov".

The boss then needs to return the data to the user. Again - too much work for him. So he gets a stupid junior dev aka the view to do it. (Not all juniors are stupid, but it works for this example!). The view knows nothing. It doesn't care how the data came to be, just that it's there and works. The boss may make some minor alterations to the data as we don't want the view having more responsibility than it needs, then the view goes off and displays the data. Depending on the work load, the boss may recruit multiple views for the job.

Basically the controller controls all flow in the MVC pattern. It decides what models to call, and what views to render, and what data from the model to pass to the view (ViewModels).

You can call other controllers from a controller. If you have a search box on your homepage for example, when a user hits search, it can call the index function on the search controller. In my metaphor above, it would be akin to one department passing a job to another department. Then this whole process starts again, with the boss recruiting the staff he needs for the current request.

那伤。 2024-09-25 21:52:08

控制器。控制器视图不是一对一的关系。控制器可以有许多视图(想象一下向导类型的情况)。如果这是你无法控制的,那么控制器的视图实际上可以是另一个控制器,这个控制器负责你的多个视图。

The controller. The controller view is not a one to one relationship. A controller can have many views (think of a wizard type situation). If this is out of your control, the controller's view can actually be another controller, this controller responsible for your multiple views.

终遇你 2024-09-25 21:52:08

您正在寻找的是应用程序工作流程管理器或应用程序控制器。

http://martinfowler.com/eaaCatalog/applicationController.html

您的控制器应该询问应用程序控制器什么接下来要做的事。这避免了导航中的硬编码,并允许多种情况。

举例来说,您有一个联系人列表,然后单击“创建”按钮来创建新联系人。成功完成保存后,创建表单应该消失,您应该再次看到联系人列表。

但是,假设您要创建联系人作为报价的一部分。创建联系人后,您不想返回联系人列表,而是想返回订单表单。

您可以使用 switch 语句来确定下一步要做什么,或者使用状态机,或者类似的东西:

Spring Web Flow http://www.springsource.org/spring-web-flow

Windows Workflow Foundation http://msdn.microsoft.com/en-us/netframework/aa663328

What you are looking for is an Application Workflow Manager, or Application Controller.

http://martinfowler.com/eaaCatalog/applicationController.html

Your controller should ask the Application Controller what to do next. This avoids hard-coding in the navigation, and allows for multiple situations.

Say for example, you have a contacts list, and hit the create button, to create a new contact. When save is completed successfully, the create form should go away, and you should see the contact list again.

However, say you are creating a contact as part of a quote. After the contact is created, you don't want to go back to the contact list, you want to go back to the order form.

You could use a switch statement to determine what to do next, or a state machine, or something like:

Spring Web Flow http://www.springsource.org/spring-web-flow

Windows Workflow Foundation http://msdn.microsoft.com/en-us/netframework/aa663328

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文