Can Source Generators Further Simplify Synchronization in MVVM Community Toolkit
まだ誰も着手していません。
評価
調査の方向性
リポジトリのファイル、テスト、またはジェネレーターのエントリポイントは指定されていません。まず既存の ObservableProperty ソースジェネレーターの動作と、提案されている同期属性を確認し、要求されているプロパティおよびコレクションの宣言的な配線が実現可能かどうかを判断してください。完了条件は、合意された API と、ループ防止を含む生成された双方向同期動作が定まっていることです。
索引モデルが issue の本文から書いたものです。
説明
Overview
I’m using the MVVM Community Toolkit to keep a ViewModel synchronized with a Model, handling both properties and collections. The [ObservableProperty] attribute already simplifies INotifyPropertyChanged implementation, but bidirectional synchronization between the ViewModel and Model still requires manually writing event handlers and update logic. This becomes repetitive and prone to errors, especially with multiple properties or collections.
Current Code:
Here’s my current ViewModel implementation, which synchronizes a Name property and an Items collection with a Model:
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
public partial class ViewModel : ObservableObject
{
private readonly Model model;
private bool isSyncing = false;
[ObservableProperty]
private string name;
[ObservableProperty]
private ObservableCollection<string> items = new ObservableCollection<string>();
public ViewModel(Model model)
{
this.model = model;
// Initialize properties from the Model
Name = model.Name;
// Initialize the collection from the Model
foreach (var item in model.Items)
{
Items.Add(item);
}
// Sync properties: Update ViewModel when Model changes
model.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == nameof(Model.Name))
{
Name = model.Name;
}
};
// Sync collections: Update ViewModel.Items when Model.Items changes
model.Items.CollectionChanged += (sender, e) =>
{
if (!isSyncing)
{
isSyncing = true;
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (string item in e.NewItems)
{
Items.Add(item);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (string item in e.OldItems)
{
Items.Remove(item);
}
}
isSyncing = false;
}
};
// Sync collections: Update Model.Items when ViewModel.Items changes
Items.CollectionChanged += (sender, e) =>
{
if (!isSyncing)
{
isSyncing = true;
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (string item in e.NewItems)
{
model.Items.Add(item);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (string item in e.OldItems)
{
model.Items.Remove(item);
}
}
isSyncing = false;
}
};
}
// Sync properties: Update Model when ViewModel's Name changes
partial void OnNameChanged(string value)
{
model.Name = value;
}
}
Issue:
While functional, this code involves a lot of boilerplate:
- Manual event subscriptions for
PropertyChangedandCollectionChanged. - Explicit initialization of properties and collections from the Model.
- Repeated logic to update one side when the other changes, including an
isSyncingflag to avoid infinite loops.
Question:
Could the MVVM Community Toolkit leverage source generators to eliminate this boilerplate? I’d love to see a solution where synchronization is defined declaratively (e.g., via attributes), and the source generator handles all the event wiring and updates behind the scenes.
API breakdown
How It Could Work:
[SyncWithModel(typeof(Model))]specifies that this ViewModel synchronizes with theModelclass.[SyncProperty("Name")]marks thenameproperty for bidirectional synchronization withModel.Name.[SyncCollection("Items")]marks theitemscollection for bidirectional synchronization withModel.Items.
The source generator could then:
- Initialize
nameanditemsfrom the Model in the constructor. - Subscribe to
PropertyChangedon the Model to updatename. - Subscribe to
CollectionChangedon bothModel.ItemsandViewModel.Itemsfor two-way updates. - Generate the update logic with an
isSyncingguard to prevent infinite loops.
Usage example
Hypothetical Example:
Here’s what I envision the code could look like with source generators:
using CommunityToolkit.Mvvm.ComponentModel;
[SyncWithModel(typeof(Model))]
public partial class ViewModel : ObservableObject
{
private readonly Model model;
public ViewModel(Model model)
{
this.model = model;
}
[SyncProperty("Name")]
[ObservableProperty]
private string name;
[SyncCollection("Items")]
[ObservableProperty]
private ObservableCollection<string> items = new ObservableCollection<string>();
}
Breaking change?
No
Alternatives
None
Additional context
Would the MVVM Community Toolkit team consider adding this kind of source-generator-based synchronization? It would make the code much cleaner and more maintainable. If this is doable, what challenges might arise in implementing it? Alternatively, if there’s an existing way to achieve this with less boilerplate, I’d appreciate pointers or examples.
Help us help you
No, just wanted to propose this
- 主要言語
- C#
- スター
- 3.8k
- フォーク
- 401
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
CommunityToolkit/dotnet のほかの issue
-
bug :bug:
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
CommunityToolkit/dotnet#1206 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
CommunityToolkit/dotnet#1186 ·
-
bug :bug:
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
CommunityToolkit/dotnet#648 ·
-
bug :bug:
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
CommunityToolkit/dotnet#1215 ·
-
feature request :mailbox_with_mail:
難易度 5/5 1週間以上 初心者へのやさしさ 45/100
CommunityToolkit/dotnet#1214 ·
CommunityToolkit/dotnet の issue をすべて見る
似ている issue
-
copilot documentation
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 2 日以内に返信
-
[Rust][Flaky Test] multiple_deadlines_fire_in_order asserts a wall-clock gap instead of firing orderオープンCI/CD ⚒️ Flaky-tests 🐦
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
valkey-io/valkey-glide#7255 ·
メンテナーはふだん 3 日以内に返信
-
bug good first issue
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
unoplatform/Uno.Core#99 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
aws/aws-dotnet-ai#75 ·