`
devgis
  • 浏览: 133930 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

MVP模式

 
阅读更多

MVC模式主要解决的问题就是将表示层和业务层进行分离,在以往做WINFORM项目的时候,通常都是将很多的逻辑代码直接写在了Form.cs代码的事件里,这样的话业务逻辑就和界面紧耦合在一起了,现在我们采用MVC来解耦。

首先建立Model:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.ComponentModel;
  6. namespaceWindowsFormsApplication10
  7. {
  8. publicclassPerson:INotifyPropertyChanged
  9. {
  10. privatestring_id;
  11. publicstringID
  12. {
  13. get{return_id;}
  14. set{_id=value;OnPropertyChanged("ID");}
  15. }
  16. privatestring_name;
  17. publicstringName
  18. {
  19. get{return_name;}
  20. set{_name=value;OnPropertyChanged("Name");}
  21. }
  22. #regionINotifyPropertyChanged成员
  23. publiceventPropertyChangedEventHandlerPropertyChanged;
  24. protectedvoidOnPropertyChanged(stringPropertyName)
  25. {
  26. PropertyChangedEventHandlerhandler=PropertyChanged;
  27. if(handler!=null)
  28. {
  29. handler(this,newPropertyChangedEventArgs(PropertyName));
  30. }
  31. }
  32. #endregion
  33. }
  34. }

为了能支持双向绑定数据,Model实现了INotifyPropertyChanged接口.

再看看Controllor的实现:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. namespaceWindowsFormsApplication10
  6. {
  7. publicclassPersonControllor
  8. {
  9. publicPersonFormView;
  10. publicPersonModel;
  11. publicPersonControllor(PersonFormview)
  12. {
  13. //初始化了一个Model
  14. Model=newPerson(){ID="1",Name="xiaojun"};
  15. //通过构造函数将View注入到Controllor中
  16. this.View=view;
  17. //建立起View和Controllor的关联
  18. //这时候View中能使用它所对应的Controllor进行业务逻辑的操作,Model也能和VIEWUI控件进行双向绑定
  19. this.View.Controllor=this;
  20. }
  21. ///<summary>
  22. ///执行一个业务逻辑
  23. ///</summary>
  24. publicvoidUpdatePerson()
  25. {
  26. UpdateToDataBase(Model);
  27. }
  28. privatevoidUpdateToDataBase(Personp)
  29. {
  30. //dosomething
  31. //执行将数据插入到数据库的操作
  32. System.Windows.Forms.MessageBox.Show("ID:"+p.ID+"Name:"+p.Name);
  33. }
  34. }
  35. }


然后是View的实现:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. namespaceWindowsFormsApplication10
  10. {
  11. publicpartialclassPersonForm:Form
  12. {
  13. privatePersonControllor_controllor;
  14. publicPersonControllorControllor
  15. {
  16. get{return_controllor;}
  17. set
  18. {
  19. this._controllor=value;
  20. //绑定一定只能写在给Controllor赋值以后而不能写在PersonForm的构造函数中(此时Controllor还未被实例化)
  21. //因为我们这里采用的是Controllor-First而不是View-First,不然Controllor.Model为null会异常
  22. //将View通过构造函数注入到Controllor中的属于Controllor-First,这时候Controllor先创建
  23. //将Controllor通过构造函数注入到View中的属于View-First,这时候View先创建
  24. this.textBox1.DataBindings.Add("Text",Controllor.Model,"ID");
  25. this.textBox2.DataBindings.Add("Text",Controllor.Model,"Name");
  26. }
  27. }
  28. publicPersonForm()
  29. {
  30. InitializeComponent();
  31. }
  32. privatevoidbutton1_Click(objectsender,EventArgse)
  33. {
  34. //改变VIEW的UI控件的值,Controllor的Model会跟着变
  35. this.textBox1.Text="2";
  36. this.textBox2.Text="jacky";
  37. Controllor.UpdatePerson();
  38. }
  39. privatevoidbutton2_Click(objectsender,EventArgse)
  40. {
  41. //改变Controllor的Model的值,VIEW的UI控件的值会跟着变
  42. Controllor.Model.ID="2";
  43. Controllor.Model.Name="jacky";
  44. Controllor.UpdatePerson();
  45. }
  46. privatevoidPersonForm_Load(objectsender,EventArgse)
  47. {
  48. }
  49. }
  50. }


最后是程序启动:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Windows.Forms;
  5. namespaceWindowsFormsApplication10
  6. {
  7. staticclassProgram
  8. {
  9. ///<summary>
  10. ///应用程序的主入口点。
  11. ///</summary>
  12. [STAThread]
  13. staticvoidMain()
  14. {
  15. Application.EnableVisualStyles();
  16. Application.SetCompatibleTextRenderingDefault(false);
  17. //Controllor-First模式,先创建Controllor(PersonControllor)再将View(PersonForm)注入到Controllor(PersonControllor)中
  18. PersonControllorcontrollor=newPersonControllor(newPersonForm());
  19. Application.Run(controllor.View);
  20. }
  21. }
  22. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics