• Testabiltiy ( ViewModel) is easier to unit test than code-behind or event driven code)• Clear seperation between UX designer and developer• Increases the "Blendability" of your view• Model never needs to be changed to support changes to the view• ViewModel rarely needs to be changed to support changes to the view• No duplicated code to update views
View ViewModel Model• User interface• Window, UserControl, Page or DataTemplate• DataContext is the ViewModel• Little or no code-behind• Updated via data bindings• Command Sources• Behaviors
View ViewModel Model• Abstraction of the View• Adapts Model to View• Maintains State• Exposes properties the View binds to (data and ICommand properties)• Exposes methods View behaviors can call• Loose coupling and testability is the high order bit • Stay away from calling singletons • Stay away from creating up dependencies • Stay away from launching dialogs directly • If not using TDD or BDD, test your ViewModels early on
View ViewModel Model• Your domain• Data objects – DTO, POCO – Generated data model – Generated proxy model• Service Layer – Repositories – Business objects
1. 코드 비하인드에 대한 작업을 줄이거나 없어야 한다.2. UI(View)의 입출력은 ViewModel과 바인딩되어 있어야 한다.3. ViewModel에 INotifyPropertyChanged 구현4. ViewModel에 로직(behavior)이 있어야 한다.5. Model에 View에 대한 상태값이 있으면 안된다.6. View와 관련이 없는 정보는 Model에 존재하여야 한다.7. ViewModel이 테스팅에서의 주체가 된다.8. 이벤트를 지양하고 Command를 사용한다
1. 개인 코딩 스타일에 따른 표준 확립의 어려움(이벤트 처리)2. 간단한 UI 프로젝트에 M-V-VM 패턴은 최악이다.3. 기존 개발자가 새로운 개념을 이해하는데 시간이 오래 걸린다.4. 디자이너도 어느 정도의 학습이 필요하다
• Routed Commands – Built in commands like Copy, Cut, Paste – Custom commands – Typically uses the CommandManager – Not used in MVVM – Many routed commands and cause poor performance – Routed commands are great for document centric applications like a text editor• Commands in MVVM – Not routed. Use data binding to directly connect source & target – Can use the CommandManager
• Separate the invoker and logic that executes the command• Provides indication whether an action is available• Are exposed in the ViewModel as ICommand properties• Enables the View to invoke logic in the ViewModel View ViewModel Save Data Binding ICommand Property
• Command sources ButtonBase, MenuItem, Hyperlink, InputBinding, KeyBinding with KeyGesture, ListBoxItem• Classes that implement ICommandSourceICommandSource PropertiesName DescriptionCommand Gets the command that will be executed when the c ommand source is invoked.CommandParameter Represents a user defined data value that can be pa ssed to the command when it is executed.CommandTarget Only applies to routed commands, normally not used in MVVM. The object that the command is being exe cuted on.
• Classes that implement ICommand• RelayCommand & DelegateCommand• Custom classesICommand MethodsName DescriptionExecute Defines the method to be called when the command is invoked.CanExecute Defines the method that determines whether the co mmand can execute in its current state.ICommand EventsCanExecuteChanged Occurs when changes occur that affect whether or n ot the command should execute.
ViewModel Command SourceButtonBase, Hyperlink, M Execute ICommand PropertyenuItem, InputBinding, K eyGesture, ListBoxItem Execute (Action)
• RelayCommand, RelayCommand<T> – Uses CommandManager which uses callbacks for CanExecute• DelegateCommand, DelegateCommand<T> – Does not use CommandManager, must implement CanExecute logic• Others or different flavors