# Mvvm

Implement the Model-View-ViewModel pattern in .NET applications with proper separation...

- Repository: https://github.com/managedcode/dotnet-skills (500 stars)
- Category: Design
- License: MIT
- Last updated: 2026-10-29
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add managedcode/dotnet-skills --skill mvvm -g -a claude-code -y`
Codex: `npx skills add managedcode/dotnet-skills --skill mvvm -g -a codex -y`
Cursor: `npx skills add managedcode/dotnet-skills --skill mvvm -g -a cursor -y`
Gemini CLI: `npx skills add managedcode/dotnet-skills --skill mvvm -g -a gemini-cli -y`

## Description

Implement the Model-View-ViewModel pattern in .NET applications with proper separation of concerns, data binding, commands, and testable ViewModels using MVVM Toolkit. USE FOR: implementing UI separation with Model-View-ViewModel; using MVVM Toolkit (CommunityToolkit.Mvvm) for ViewModels; designing testable UI architecture. DO NOT USE FOR: unrelated stacks; generic tasks that do not need this specific guidance. INVOKES: inspect the repository context, edit targeted files, and run relevant build, test, lint, or validation commands when changes are made.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# MVVM Pattern for .NET

## Trigger On

- implementing UI separation with Model-View-ViewModel
- using MVVM Toolkit (CommunityToolkit.Mvvm) for ViewModels
- designing testable UI architecture
- handling commands, property changes, and messaging
- choosing between MVVM frameworks

## Documentation

- [MVVM Toolkit Overview](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/)
- [ObservableObject](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/observableobject)
- [RelayCommand](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/relaycommand)
- [Messenger](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/messenger)
- [Source Generators](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/overview)

## References

See detailed examples in the `references/` folder:
- [`patterns.md`](references/patterns.md), ViewModel, command, navigation, and state patterns
- [`anti-patterns.md`](references/anti-patterns.md), Common mistakes and how to fix them

## Core Concepts

| Component | Responsibility | Example |
|-----------|---------------|---------|
| **Model** | Business logic and data | `Product`, `Order`, `User` |
| **View** | UI presentation (XAML/Razor) | `ProductPage.xaml` |
| **ViewModel** | UI logic and state | `ProductViewModel` |

## Workflow

1. **Keep Views dumb**, no business logic in code-behind
2. **Use data binding**, connect View to ViewModel properties
3. **Commands for actions**, handle user interactions via ICommand
4. **Inject dependencies**, services go into ViewModel constructors
5. **Test ViewModels**, they should be unit testable without UI

## MVVM Toolkit Setup

```xml
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.*" />
```

## ViewModel Patterns

### Basic ViewModel with Source Generators
```csharp
public partial class ProductViewModel(IProductService productService) : ObservableObject
{
    [ObservableProperty]
    private string _name = string.Empty;

The rest: https://github.com/managedcode/dotnet-skills/blob/HEAD/catalog/Libraries/MVVM-Toolkit/skills/mvvm/SKILL.md
