MVC,控制器 - 用例 [英] MVC, controller - use cases

查看:14
本文介绍了MVC,控制器 - 用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到您应该在 MVC-OOD 中设置控制器类作为用例,从上到下仅在一个运行 MVC 类的方法中设置.

I've learned that you should set up the controller-class in a MVC-OOD as a use case, from top to bottom in only one method that run the MVC-classes.

是否可以在一个控制器中使用不同的方法来获得更多控制和更好的概览?

Is it OK to use different methods in one controller to get more control and better overview?

假设您想运行一个控制器来显示登录表单(从视图中获取 html 等).如果用户未登录,同一控制器还将显示一个注销按钮.这可以通过控制器中的单个方法来完成,但使用两种方法似乎更好.如果您想要登录表单,则调用一种方法,如果您想要注销按钮,则调用一种方法.(只是一个例子)

Let's say you wanna run a controller that will display a login form (getting the html etc from the View). And the same controller will also display a log-out button IF the user is NOT logged in. This could be done with a single method in the controller, but using two methods seems better. One method to call if you want the login form, and one to call if you want to log-out button. (just an example)

那么,专业人士怎么说.每个控制器应该只包含一个用例"方法,还是可以有多个?

So, what does the pros say. Should each controller contain one "use case" method only, or could it be several?

推荐答案

在 MVC 中记住两件事很重要,首先,它是面向对象的体系结构,其次,它应该用于分离关注点.

Its important to remember two things with MVC, firstly, its an Object-Oriented Architecture, and secondly, It should be used for separating concerns.

Separation of Concerns 与 Abstraction 相关,它是为了帮助我们理解手头的代码段.模型和视图都是相关对象的集合/域.每个对象都完全完整并与其领域相关.

Separation of Concerns is related to Abstraction, It is to aid us in understanding the section of code at hand. The Model and View are both collections/domains of related objects. Each object is fully complete and relevant to its domain.

您将在您的视图中找到具有按钮、图像、文本输入等类型的对象,您将在您的模型中找到与业务相关的对象(用户、帐户、配置文件等).

You will find objects with types such as Buttons, Images, Text Inputs etc inside your View, and you will find business related objects (User, Account, Profile etc) within your Model.

模型中的对象集合不会做太多事情,它们需要将对象连接在一起的逻辑.(或者干脆将简单的单个对象请求委托给正确的对象)

The collection of objects inside your Model don't tend to do much, They require logic to wire the objects together. (Or simply delegate simple single object requests to the correct object)

控制器为您的模型提供接口,并包含与模型相关的业务逻辑以及模型对象之间的交互.您的模型将有一个控制器,而控制器将有多个方法,这些方法将与您的用例保持一致.

The Controller provides the interface into your Model, and contains the business logic related to the Model and the interactions between the Model objects. You will have a single Controller for your Model, and the Controller will have multiple methods which will align with your use-cases.

这篇关于MVC,控制器 - 用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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