재우니의 블로그

MVVM (Model-View-ViewModel) Pattern For Windows Form Applications, using C#

 

Introduction

The MVVM pattern is in favour of WPF/Silverlight developers because of its advantages. Microsoft says that MVVM:

  • Separates the business and presentation logic of the application from its UI, and makes it much easier to test, maintain, and evolve.
  • Allows developers and UI designers to more easily collaborate when developing their respective parts of the application.(Look at the link)

MVVM is for WPF, but is there any way to have the advantages for WinForms too? The answer is yes (good news or maybe not so new). Windows Form Applications can also have the advantages, and of course it doesn't mean the exact real Model-View-ViewModel (MVVM) pattern. I searched and found many approaches (Google Search) some of them were great and some not(!) But I've tried to present a simple useful solution. I hope you'll be interested.

Background

As you know, Program Logic and Content are separated In ASP.Net. And also in WPF, Program Logic is separated from UI. In Windows Forms, I preferred to use partial classes to separate Program Logic from UI until MVVM pattern has been introduced to me. I interested in using the pattern for WPF, so I decided to find a same way for WinForms too, but I was always too busy to work on it. One day, I tried to impose MVVM on WinForms, and here is the result of 3 spare hours working on the subject.

A very fast and short MVVM review

MVVM means Model-View-ViewModel:

MVVM



1. View: It refers to the elements that displayed by the UI. As some people say, View is UI and some others say that View is UI "but" Logic (I like the second one). And also, it passes commands to ViewModel.



화면 UI 에서 보여줄 요소들을 참조하는데요. VIEW 는 그냥 화면 UI 로 보시면 되고, 또는 UI 이지만 나름 로직도 있다고 합니다. 이는 ViewModel 에 command 를  통해 전송됩니다. 


2. ViewModel: It's an abstraction of the View, or mediation between View and Model. It passes Data between View and Model.


ViewModel :  View 를 추상화 한것이라고 생각하시면 되며, 이는 View 와 Model 간의 중간 매체 입니다. View 와 Model 간의 데이터를 전송합니다.


3. Model: It represents data from DAL (Data Access Layer) or a Domain Model. So, Model is not DAL but refers to it.

 

 Model : 이는 data access layer 즉 데이터를 처리하는 대표적인 곳입니다. 또한 Domain model 이라고도 하죠. Model 은 Data Access Layer 가 아닌 단지 이를 참조 한답니다.

 

 

 

WindowsFormsApplication1.zip





msdn 메거진에 이론 설명이 자세히 나와 있습니다.