Replies: 1 comment 2 replies
-
|
I rather the whole process of having to manually write all the model's properties is tedious and error prone. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m using the MVVM Community Toolkit to keep a ViewModel synchronized with a Model, handling both properties and collections. The
[ObservableProperty]attribute already simplifiesINotifyPropertyChangedimplementation, 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
Nameproperty and anItemscollection with aModel:Issue:
While functional, this code involves a lot of boilerplate:
PropertyChangedandCollectionChanged.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.
Hypothetical Example:
Here’s what I envision the code could look like with source generators:
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:
nameanditemsfrom the Model in the constructor.PropertyChangedon the Model to updatename.CollectionChangedon bothModel.ItemsandViewModel.Itemsfor two-way updates.isSyncingguard to prevent infinite loops.Request:
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.
Beta Was this translation helpful? Give feedback.
All reactions