緣起mvc
通常在作 WINFORM 開發時,一般都是將不少業務邏輯直接寫在 Form 的事件代碼裏,形成業務邏輯就和界面邏輯緊密耦合在一塊兒。這種狀況下,若是業務邏輯稍有變化,涉及的改動量很是大,甚至於整個界面可能都須要從新調整,測試起來也很麻煩。app
幾年前,給某市城管委作一個瓶裝液化氣智能監管信息項目,就在這方面吃了一點虧。框架
所以,我後來花了一點時間,作了一個簡單的 WinForm MVC 框架(我的獨立開發,與我當時所在的公司無關,也從未在當時的公司用過),設想着之後若是再遇到相似項目,能夠直接拿來用。結果作完以後,也沒有在後來的實際項目中用到。多少有點遺憾,權當是一次練手吧,也許之後再用到也說很差。因此如今我決定把這個東西開源出來,有須要的人能夠拿去用。ide
代碼和用法測試
我給這個 winform mvc 框架寫了一個示例,下面的代碼是從示例中摘取出來的一部分源碼:spa
1 using ContactManager.DataMapping; 2 using ContactManager.Filters; 3 using ContactManager.Services; 4 using ContactManager.Utils; 5 using ContactManager.Views.Model; 6 using ContactManager.Views.Utils; 7 using My.WinformMvc; 8 9 namespace ContactManager.Controllers 10 { 11 [MvcController(Constant.ListController)] 12 public class ListController : BaseController 13 { 14 readonly IContactService _contactService; 15 16 public ListController(IContactService contactService, IView view) 17 : base(view) 18 { 19 _contactService = contactService; 20 } 21 22 public override IActionResult DisplayView() 23 { 24 var myContacts = _contactService.GetContacts().ToModel(); 25 return base.DisplayView(myContacts); 26 } 27 28 [RoleBasedAuthorization(Roles = Constant.AdminRoles)] 29 [ConfirmDelete] 30 [NotifyDelete] 31 [WriteResult] 32 public IActionResult Delete(ContactModel model) 33 { 34 Logger.Log("Before ListController.Delete"); 35 _contactService.DeleteContact(model.Id); 36 Logger.Log("After ListController.Delete"); 37 return DisplayView(); 38 } 39 } 40 }
(ListController 源碼)code
1 using System; 2 using System.Collections.Generic; 3 using System.Windows.Forms; 4 using ContactManager.Utils; 5 using ContactManager.Views.Model; 6 using ContactManager.Views.Utils; 7 using My.WinformMvc; 8 9 namespace ContactManager.Views 10 { 11 [MvcView("ListView")] 12 public partial class ListView : BaseView, IView<List<ContactModel>> 13 { 14 public ListView() 15 { 16 InitializeComponent(); 17 } 18 19 public void BindModel(List<ContactModel> model) 20 { 21 Logger.Log("ListView.BindModel"); 22 contactDataGridView.DataSource = model; 23 } 24 25 private void btClose_Click(object sender, EventArgs e) 26 { 27 Application.Exit(); 28 } 29 30 private void btnDelete_Click(object sender, EventArgs e) 31 { 32 if (contactDataGridView.SelectedRows.Count == 0) 33 { 34 MessageBox.Show("Please select a row first!", "Information"); 35 return; 36 } 37 38 var contact = contactDataGridView.CurrentRow.DataBoundItem as ContactModel; 39 InvokeAction("Delete", contact); 40 } 41 42 private void btnAdd_Click(object sender, EventArgs e) 43 { 44 OpenView(Constant.EditController, new ContactModel{ IsEdit = false }); 45 } 46 47 private void btnUpdate_Click(object sender, EventArgs e) 48 { 49 if (contactDataGridView.SelectedRows.Count == 0) 50 { 51 MessageBox.Show("Please select a row first!", "Information"); 52 return; 53 } 54 var contact = contactDataGridView.CurrentRow.DataBoundItem as ContactModel; 55 contact.IsEdit = true; 56 OpenView(Constant.EditController, contact); 57 } 58 } 59 }
(ListView 源碼)orm
如下是運行這個示例的幾張截圖:blog
若是您對這個框架感興趣,能夠直接點擊 此處 下載框架源碼(含示例)。事件