sb3*_*700 11 .net code-organization winforms
我是自学编程的人,并且没有接受任何正式的.NET编程培训.
前段时间,我开始使用C#来开发一个GUI程序来控制传感器,项目已经蓬勃发展.我只是想知道如何在我的表单中最好地组织代码,特别是UI代码.
我的表格目前是一团糟,或者至少对我来说是一团糟.
我的问题是这看起来像一团糟,特别是国家财产,并且变得无法维护.此外,我的应用程序逻辑代码和UI代码在同一个文件中,在某种程度上,混合似乎是错误的,这意味着我需要做很多滚动才能找到我需要的东西.
你如何构建.net表单?
谢谢
有许多模式可以帮助您在应用程序中分离逻辑,从而使代码更清晰,更易于维护.MVP模式是一个很好的开始.它基于定义3个响应区域,即MVP M =模型,V = View,P = Presenter.如果你熟悉使用接口你会没事的,否则这将是一个很好的起点(回顾基本的OO原则:封装,抽象,多态).MVP的基本原理是将应用程序逻辑放在Presenter中.prenter通过一个接口与视图(你的表单)对话,当用户与它交互时,视图回调给演示者(我也使用这个接口).该模型是解决方案的域对象层次结构,它包含商业逻辑和实体关系.
大多数UI模式(MVP,MCV等)都试图做同样的事情,分开你的担忧.以下是一个简单的例子:
//视图界面
interface IUserDetailsView
{
string Username{set;get;}
string FirstName{get;set;}
string LastName{get;set;}
UserDetailsPresenter Presenter{get;set;}
void DisplayMessage(string message);
}
Run Code Online (Sandbox Code Playgroud)
//视图实现//标准窗口窗体,包含文本框,标签,组合等
class UserDetailsView : Form, IUserDetails
{
public string Username{set{txtUserName.text = value;}get{return txtUserName.text;}}
public string FirstName{set{txtFirstName.text = value;}get{return txtFirstName.text;}}
public string LastName{set{txtLastName.text = value;}get{return txtLastName.text;}}
Public UserDetailsPresenter Presenter{get;set;}
public void DisplayMaessage(string message)
{
MessageBox.Show(message);
}
private void saveButton_Click(object sender, EventArgs e)
{
Presenter.SaveUserDetails();
}
}
Run Code Online (Sandbox Code Playgroud)
//演示逻辑
class Presenter UserDetailsPresenter {
//Constructor
public userDetailsPresenter(IUserDetailsView view)
{
//Hold a reference to the view interface and set the view's presnter
_view = view;
_view.Presenter = this;
}
private IUserDetailsView _view;
DisplayUser(string userName)
{
//Get the user from some service ...
UserDetails details = service.GetUser(userName);
//Display the data vioa the interface
_view.UserName = details.UserName;
_view.FirstName = details.FirstName;
_view.LastName = details.LastName;
}
public void SaveUserDetails()
{
//Get the user dryaiols from the view (i.e. the screen
UserDetails details = new UserDetails();
details.UserName = _view.UserName;
details.FirstName = _view.FirstName;
details.LastName = _view.LastName;
//Apply some business logic here (via the model)
if(!details.IsValidUserDetails())
{
_view.DisplayMessage("Some detail outlining the issues");
return;
}
//Call out to some service to save the data
service.UpdateUser(details);
}
Run Code Online (Sandbox Code Playgroud)
}
//最后,模型
public class UserDetails
{
public UserName {get;set;}
public FirstName{get;set;}
public LastName{get;set;}
public bool IsValidUserDetails()
{
if(LastName == "Smith")
{
//We do not allow smiths, remember what happened last time ... or whatever
return false;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这能解释责任是如何分离的.除了显示/格式化之外,表单没有逻辑,它也可以被删除以进行测试.演示者是视图和模型之间的中介,并调用服务,模型包含您的业务逻辑.正如已经提出的那样,这种模式存在差异,这可以使您的代码更轻薄,更灵活,但这概述了基本原则.我希望这有帮助.
:-)
这是一个经常使用的架构模式的链接。
http://en.wikipedia.org/wiki/Model_View_ViewModel
我也会查找一些其他架构模式,并对这一模式进行更多研究,查找一些示例代码等。
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |