如何“介绍”查看& MVC 模式中的控制器?
在桌面应用程序中使用 MVC 模式,向其各自的控制器引入视图的好方法是什么?反之亦然?例如,您是否应该使用构造函数注入来为视图提供控制器,然后让视图在控制器上调用 setView 方法并将其自身作为参数传递?
(问题不特定于任何框架/平台。)
Using the MVC pattern in a desktop application, what's a good approach to introducing a view to its respective controller, and vice-versa? E.g., should you use constructor injection to give the view its controller, then have the view call a setView method on the controller and pass itself as the argument?
(Question isn't specific to any framework/platform.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
视图应该尽可能愚蠢。他们不应该知道或依赖特定的控制器来实例化它们。最好的情况是,他们应该能够访问某种在构造时传递给他们的基本控制器类引用,通常作为“视图数据”对象的一部分,该对象也包装了要使用的模型。
您的控制器应该负责实例化视图,为视图提供将要显示的模型,并返回视图的结果。视图不应该回调控制器来告诉它要返回什么,因为这会将逻辑流的控制权交给视图,而这实际上并不像 MVC。
Views should be as dumb as possible. They shouldn't know about, or rely on having specific controllers instantiate them. At best they should have access to some kind of base controller class reference that is handed to them upon construction, usually as part of the "view data" object which wraps the Model to be used as well.
Your controller should be responsible for instantiating the view, giving the view the model it will be displaying, and returning the view's result. The view shouldn't be calling back into the controller to tell it what to return, as this gives control of the logical flow to the View, which isn't really MVC-like.