I've got a problem that I'm fairly sure ReactiveUI will help with but I can't quite wrap my brain around how to implement it. Basically I have a simple model:
public class Model
{
public bool IsThingEnabled { get; set; }
public string MyStringSetting { get; set; }
public bool IsModified { get; set; }
}
And then I have a ViewModel that can edit this model, or swap out the model for a completely different one.
So it has the IsThingEnabled
and MyStringSetting
properties but also a Model
field/property. Before my approach was to have a property getter and setter pass down to the underlying model, with some extra logic on the setters to update other dependent fields on the Model
, set its IsModified
to true, raise property changed event(s) and persist the model to disk.
This works pretty well, but the problem is that I have to raise a property changed event on every property when I swap out for a new Model
, and it's easy to forget. I was thinking I could use ReactiveUI to streamline this in some manner but so far I haven't come up with anything. I thought about making an Observable
and using ObservableAsPropertyHelper
but that is apparently geared toward properties with only a getter.
Then I considered using a standard property with a backing store on the VM and a setter with RaiseAndSetIfChanged
, but I couldn't figure out quite what Observables
or subscriptions to do. I can observe a new Model
getting set, but then would still be in the position where I need to distribute property change notifications for everything. And I'd still need to set up subscriptions to pipe each VM property down to the model when it's changed.
Am I missing something about ReactiveUI or should I use a different approach?