复杂的用户界面-> MVC模式 [英] Complex User Interface -> MVC pattern

查看:101
本文介绍了复杂的用户界面-> MVC模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关MVC/MVP模式的文章....我有一个简单的问题....如果您有一个包含大量控件的视图...例如说10个texbox和10个复选框....等等等等...我应该在IView界面中指定属性和事件中的每个属性吗?....

I have been reading a lot on MVC/MVP patterns.... I have a simple question....If you have a view with loads of controls....say 10 texboxes and 10 checkboxes....etc etc... Am I expected to specify the properties and events each one of them in my IView interface?....

推荐答案

绝对不是那样. 您的IView界面将定义可以由您的业务层访问的一组合同/方法(包括属性). 在这样的界面中暴露您的控件是完全错误的:

Definitely not that way. Your IView Interface will define set of contracts/ methods (it includes properties) that can be accessed by your business layer. It is totally wrong to exposed your control in interface like this:



public interface IView
{
  TextBox UserNameTextBox{get;set;}
}

您不应以这种方式定义接口.这确实是一个糟糕的编程. 您应该公开一些您的UI层将要实现的合同. 例如

You should not have interfaces defined in this way. This is really a bad programming. You should rather expose some contracts that your UI layer will implement. E.g.



public interface IView
{
 public void SetUserName(string Text);
}

您可以在Winform和Webform上实现此接口.

You can implement this interface on winform as well as webform.

类似地,您也不应该在interface(Contract)接口中公开UI知识. 让我们假设您必须在UI上显示Employee对象的信息的情况. 您应该通过此接口将Employee对象传递给UI,UI会处理代表此Employee对象的方式.
您的BL永远不会理会n个文本框和复选框.

Similarly, you are also not supposed to expose knowlede of UI in interface(Contract). Lets assume a scenario where you have to display information of Employee object on UI. You should pass Employee object to UI through this interface and UI will take care of way of representing this Employee object.
Your BL should never bother about n number of TextBoxes and checkboxes.



public class Employee
{ 
  //first name
  //last name
  //is manager
  //is teamleader
  //address
}

public interface IEmployeeView
{ 
  void SetEmployee(Employee employee);
}


public partial class EmployeeForm:WinForm,IEmployeeView
{
  public void SetEmployee(Employee employee)
  {
    ENameTextBox.Text = employee.FirstName+" "+employee.LastName;
  } 

}

这篇关于复杂的用户界面-> MVC模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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