Open bradphelan opened 10 years ago
You may wonder how the immutable objects are updated without a constructor passing in new values. The interface is
MoineauCorrection correction;
correction = correction.Set(p=>p.Peak, 10);
correction = correction.Set(p=>p.Name,"Fred");
underneath this there is some reflection code that shallow clones the objects then using reflection set's the private property. It's as good as you can get in C# to the F# method of updating records.
Given a DTO object defined like below
}
Note there are no public setters. The object once created is immutable and can be treated like a value.
I generate a view model for this via the
ImmutableSupportViewModelBase
class. For example.Bindings are made within XAMLto the dynamic properties generated above in the normal way. The
Data
property on the viewmodel holds the most current version of the edited object.Theoretically some typesafe binders could be created for static binding but formyself I prefer binding within XAML
You can subscribe to changes on the DTO by
in the usual ReactiveUI way. If you wish to subscribe to changes on specific combinations of subproperties of the DTO you can do
To get a
Lens
( a read write sub view ) onto a sub property of the DTO you could doand set the current value of the property ( thus creating a new current version of the DTO )
Lens's are very clever in that they can give you read write access to a nested immutable property. For example your DTO had a nested property called Keys with
The inbuilt validation let's you subscribe easily to changes in your DTO only if they are valid.
The ViewModel can be decorated with commands and custom properties in the the usual RUI way.
The above code is based on but not integrated into any ReactiveUI branch. Maybe it doesn't interest anybody. If such magic is interesting let me know and I can spend some time extracting it from my production code.