What is MVVM?
MVVM divides the application into three distinct layers:Model
Business logic and data
View
User interface (XAML)
ViewModel
Mediator exposing Model data to View
The Three Layers
Model
The Model layer contains pure business logic with no knowledge of the UI. It’s responsible for calculations, data storage, and core application functionality.
CalcManager project with three components:
- CalculatorManager - Manages calculator modes, history, and memory
- CalcEngine - Performs calculations and maintains state
- RatPack - Provides infinite precision arithmetic
View
The View layer consists of XAML files that define the visual interface. Views should contain minimal code-behind logic.ViewModel
ViewModels expose data from the Model in a format suitable for the View. They:- Implement
INotifyPropertyChangedfor data binding - Expose commands for user actions
- Transform Model data for display
- Contain no UI-specific code
Data Flow
The following diagram shows how data flows through the MVVM layers:ViewModel Hierarchy
Calculator’s ViewModels form a hierarchy matching the View structure:ApplicationViewModel
- Overview
- Responsibilities
ApplicationViewModel is the root ViewModel corresponding to MainPage.xaml. It manages mode switching and coordinates child ViewModels.Location: src/CalcViewModel/ApplicationViewModel.hMode-Specific ViewModels
StandardCalculatorViewModel
StandardCalculatorViewModel
Supports Standard, Scientific, and Programmer calculator modes.Location:
src/CalcViewModel/StandardCalculatorViewModel.hKey features:- Display value management
- Expression tokens for display
- History and memory management
- Button command handlers
DateCalculatorViewModel
DateCalculatorViewModel
Handles date difference and date addition/subtraction calculations.Location:
src/CalcViewModel/DateCalculatorViewModel.hUnitConverterViewModel
UnitConverterViewModel
Manages all unit conversion modes including currency.Location:
src/CalcViewModel/UnitConverterViewModel.hBenefits of MVVM in Calculator
Testability
ViewModels can be tested without UI, enabling comprehensive unit tests for business logic.
Separation of Concerns
UI designers work on XAML while developers focus on ViewModels and Models independently.
Reusability
ViewModels can be reused across different Views or platforms.
Maintainability
Clear boundaries make the codebase easier to understand and modify.
Data Binding
MVVM relies heavily on data binding to connect Views and ViewModels:Example: Binding in Calculator.xaml
Calculator prefers
x:Bind over Binding for better performance. See Data Binding for details.Command Pattern
User actions trigger commands defined in ViewModels:Button Command Binding
StandardCalculatorViewModel.h
Property Change Notifications
When ViewModel properties change, the UI automatically updates through theINotifyPropertyChanged interface:
See the ViewModel Layer documentation for implementation details.
Best Practices
Example: Display Value Flow
Here’s how a calculation result flows through MVVM:- User presses the ”=” button
- View (Calculator.xaml) executes
ButtonPressedcommand - ViewModel (StandardCalculatorViewModel) processes the command
- ViewModel calls Model (CalculatorManager)
- Model performs calculation via CalcEngine
- Model returns result to ViewModel
- ViewModel updates
DisplayValueproperty RaisePropertyChanged("DisplayValue")fires- View receives PropertyChanged event
- UI binding re-evaluates and updates display
Next Steps
View Layer
Explore XAML implementation
ViewModel Layer
Deep dive into ViewModels
Model Layer
Understand the calculation engine