| name | wpf-mvvm-generator |
| description | WPF MVVM code generator. Creates ViewModel, View, and Model with CommunityToolkit.Mvvm source generators. Use when scaffolding new WPF features or entities following MVVM pattern. |
| user-invocable | true |
| argument-hint | <entity_name> [type] |
| context | fork |
| model | sonnet |
| allowed-tools | ["Read","Glob","Write","Edit"] |
WPF MVVM Generator ์คํฌ
CommunityToolkit.Mvvm source generator๋ฅผ ์ฌ์ฉํ์ฌ WPF MVVM ์ปดํฌ๋ํธ๋ฅผ ์์ฑํฉ๋๋ค.
์ค์: ๋ชจ๋ ๊ฒฐ๊ณผ๋ ๋ฐ๋์ ํ๊ตญ์ด๋ก ์์ฑํฉ๋๋ค. ์ฝ๋ ์๋ณ์, ๊ธฐ์ ์ฉ์ด, ํจํด ์ด๋ฆ ๋ฑ์ ์๋ฌธ ๊ทธ๋๋ก ์ ์งํ๋, ์ค๋ช
๋ถ๋ถ์ ํ๊ตญ์ด๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ธ์
$ARGUMENTS[0]: ์ํฐํฐ ์ด๋ฆ (ํ์) - ์: User, Product, Order
$ARGUMENTS[1]: ์์ฑ ์ ํ (์ ํ): viewmodel, view, model, all (๊ธฐ๋ณธ๊ฐ: all)
all: Model + ViewModel + View + Code-Behind + Messages + Service Interface ๋ชจ๋ ์์ฑ
์คํ ๋จ๊ณ
1๋จ๊ณ: ์ธ์ ๊ฒ์ฆ
$ARGUMENTS[0]์ด ๋น์ด์๋ ๊ฒฝ์ฐ:
- ์ฌ์ฉ์์๊ฒ ์ํฐํฐ ์ด๋ฆ ์์ฒญ
- ํ๋ก์ ํธ์ ๊ธฐ์กด Model ๊ธฐ๋ฐ์ผ๋ก ์ ์
์ํฐํฐ ์ด๋ฆ ๊ฒ์ฆ:
- PascalCase ์ฌ๋ถ ํ์ธ
- C# ์์ฝ์ด ์ฌ์ฉ ํ์ธ
- ํน์๋ฌธ์, ๊ณต๋ฐฑ ํฌํจ ์ ์ฌ์ฉ์์๊ฒ ์ฌ์
๋ ฅ ์์ฒญ
2๋จ๊ณ: ํ๋ก์ ํธ ๊ตฌ์กฐ ํ์
๊ธฐ์กด ํจํด์ ์๋ณ:
Glob("**/*ViewModel.cs")๋ก ๊ธฐ์กด ViewModel ํจํด ํ์ธ
- ์ฒซ ๋ฒ์งธ ๋ฐ๊ฒฌ๋ ํ์ผ์์ ๋ค์์คํ์ด์ค ์ถ์ถ
/ViewModels, /Views, /Models ๋๋ ํ ๋ฆฌ ์กด์ฌ ์ฌ๋ถ ํ์ธ
- ๊ธฐ์กด ๋ฒ ์ด์ค ํด๋์ค ๋๋ ์ธํฐํ์ด์ค ํ์
3๋จ๊ณ: ์ฝ๋ ์์ฑ
$ARGUMENTS[1] ๊ธฐ์ค์ผ๋ก ์์ฑ:
์์ฑ ์ปดํฌ๋ํธ
Model (model)
namespace {Namespace}.Models;
public sealed class {EntityName}
{
public required int Id { get; init; }
public required string Name { get; init; }
}
ViewModel (viewmodel)
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
namespace {Namespace}.ViewModels;
public partial class {EntityName}ViewModel : ObservableObject
{
private readonly I{EntityName}Service _{entityName}Service;
public {EntityName}ViewModel(I{EntityName}Service {entityName}Service)
{
_{entityName}Service = {entityName}Service;
}
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(HasSelection))]
[NotifyCanExecuteChangedFor(nameof(DeleteCommand))]
private {EntityName}? _selected{EntityName};
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private bool _isModified;
[ObservableProperty]
private bool _isLoading;
public bool HasSelection => Selected{EntityName} is not null;
[RelayCommand]
private async Task LoadAsync(CancellationToken cancellationToken = default)
{
IsLoading = true;
try
{
}
finally
{
IsLoading = false;
}
}
[RelayCommand(CanExecute = nameof(CanSave))]
private async Task SaveAsync(CancellationToken cancellationToken = default)
{
await _{entityName}Service.SaveAsync(Selected{EntityName}!, cancellationToken);
IsModified = false;
}
private bool CanSave() => IsModified && Selected{EntityName} is not null;
[RelayCommand(CanExecute = nameof(CanDelete))]
private async Task DeleteAsync(CancellationToken cancellationToken = default)
{
if (Selected{EntityName} is null) return;
await _{entityName}Service.DeleteAsync(Selected{EntityName}.Id, cancellationToken);
WeakReferenceMessenger.Default.Send(
new {EntityName}DeletedMessage(Selected{EntityName}));
Selected{EntityName} = null;
}
private bool CanDelete() => Selected{EntityName} is not null;
}
View (view)
<UserControl x:Class="{Namespace}.Views.{EntityName}View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="{Namespace}.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:{EntityName}ViewModel, IsDesignTimeCreatable=False}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{EntityName} Management"
Style="{StaticResource HeaderStyle}"/>
<ContentControl Grid.Row="1"
Content="{Binding Selected{EntityName}}"
Visibility="{Binding HasSelection, Converter={StaticResource BoolToVisibility}}"/>
<StackPanel Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Right">
<Button Content="Save"
Command="{Binding SaveCommand}"/>
<Button Content="Delete"
Command="{Binding DeleteCommand}"/>
</StackPanel>
<Border Grid.RowSpan="3"
Background="#80000000"
Visibility="{Binding IsLoading, Converter={StaticResource BoolToVisibility}}">
<ProgressBar IsIndeterminate="True" Width="200"/>
</Border>
</Grid>
</UserControl>
Code-Behind (์ต์ํ)
namespace {Namespace}.Views;
public partial class {EntityName}View : UserControl
{
public {EntityName}View()
{
InitializeComponent();
}
}
Message Types
namespace {Namespace}.Messages;
public sealed record {EntityName}DeletedMessage({EntityName} Deleted{EntityName});
public sealed record {EntityName}SelectedMessage({EntityName} Selected{EntityName});
public sealed record {EntityName}SavedMessage({EntityName} Saved{EntityName});
Service Interface
namespace {Namespace}.Services;
public interface I{EntityName}Service
{
Task<IReadOnlyList<{EntityName}>> GetAllAsync(CancellationToken cancellationToken = default);
Task<{EntityName}?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
Task SaveAsync({EntityName} entity, CancellationToken cancellationToken = default);
Task DeleteAsync(int id, CancellationToken cancellationToken = default);
}
์ถ๋ ฅ ํ์
๋ชจ๋ ๋ด์ฉ์ ํ๊ตญ์ด๋ก ์์ฑํฉ๋๋ค. ์ฝ๋ ์๋ณ์์ ๊ธฐ์ ์ฉ์ด๋ ์๋ฌธ์ ์ ์งํฉ๋๋ค.
# MVVM ์์ฑ ๊ฒฐ๊ณผ
## ์ํฐํฐ: {EntityName}
### ์์ฑ๋ ํ์ผ
| ํ์ผ | ๊ฒฝ๋ก | ์ํ |
|------|------|------|
| Model | /Models/{EntityName}.cs | ์์ฑ๋จ |
| ViewModel | /ViewModels/{EntityName}ViewModel.cs | ์์ฑ๋จ |
| View | /Views/{EntityName}View.xaml | ์์ฑ๋จ |
| Code-Behind | /Views/{EntityName}View.xaml.cs | ์์ฑ๋จ |
| Messages | /Messages/{EntityName}Messages.cs | ์์ฑ๋จ |
| Service Interface | /Services/I{EntityName}Service.cs | ์์ฑ๋จ |
### ๋ค์ ๋จ๊ณ
1. `I{EntityName}Service` ๊ตฌํ
2. DI ์ปจํ
์ด๋์ ๋ฑ๋ก
3. View ๋ด๋น๊ฒ์ด์
์ถ๊ฐ
4. ํ์ ์ ๋์์ธ ํ์ ๋ฐ์ดํฐ ์์ฑ
์๋ฌ ์ฒ๋ฆฌ
| ์ํฉ | ์ฒ๋ฆฌ |
|---|
| ์ํฐํฐ ์ด๋ฆ ๋ฏธ์
๋ ฅ | ์ฌ์ฉ์์๊ฒ ์์ฒญ, ๊ธฐ์กด Model ๊ธฐ๋ฐ ์ ์ |
| ์ ํจํ์ง ์์ ์ด๋ฆ (ํน์๋ฌธ์, ์์ฝ์ด) | ์ฌ์ฉ์์๊ฒ ์ฌ์
๋ ฅ ์์ฒญ |
| ViewModels/Views/Models ๋๋ ํ ๋ฆฌ ์์ | ๋๋ ํ ๋ฆฌ ์๋ ์์ฑ ํ ์งํ |
| ๋์ผ ์ด๋ฆ ํ์ผ ์กด์ฌ | ์ฌ์ฉ์์๊ฒ ๋ฎ์ด์ฐ๊ธฐ ํ์ธ |
๊ฐ์ด๋๋ผ์ธ
- CommunityToolkit.Mvvm source generator๋ฅผ ์ฌ์ฉํฉ๋๋ค
- ๊ธฐ์กด ํ๋ก์ ํธ ๋ช
๋ช
๊ท์น์ ๋ฐ๋ฆ
๋๋ค
- ์ต์ํ์ Code-Behind์ ์์ฑํฉ๋๋ค (UI ๋ก์ง๋ง)
- ์ ์ ํ XML ๋ฌธ์ํ๋ฅผ ํฌํจํฉ๋๋ค
- ๋น๋๊ธฐ ์์
์ CancellationToken์ ์ง์ํฉ๋๋ค
- ViewModel ๊ฐ ํต์ ์ WeakReferenceMessenger๋ฅผ ์ฌ์ฉํฉ๋๋ค