fsprojects-archive / zzarchive-FSharp.Desktop.UI

F# MVC framework for WPF.
http://fsprojects.github.io/FSharp.Desktop.UI/
Other
81 stars 21 forks source link

Model Reset/Clear #7

Closed StefanBelo closed 10 years ago

StefanBelo commented 10 years ago

I think you should consider to add model reset or clear method for abstract properties. In your implementation, abstract properties are held in dictionary:

and internal AbstractProperties() =
    let data = Dictionary()

So if model needs to refresh those properties, mainly those which content is set by wpf data binding, those properties remain with old data, if wpf control did not reset them directly.

Of course for most of properties when model context changes,there is possible to set those properties directly by model setter, but really not for all of them in my case.

dmitry-a-morozov commented 10 years ago

Please, provide me with more information because I struggle to understand the use case. Do you want to clean up the property values in controller method in respond of some event? If so, why can't you just reset/set them to some "clean" values inside controller? A generic reset can be tricky because it raises too many questions. For example: Does it reset reference properties to nulls? Should it send PropertyChanged notifications for each property that has be reset?

StefanBelo commented 10 years ago

For instance, you have got DataGrid showing some items. Now you want an user to able to select one or more items, to proceed with some action on the item/s. In the initial state no item is selected, so in WPF control word it is represented by null, either SelectItem or SelectItems.

Thanks to your abstract properties we can push from our F# world null value to WPF binding in an initial model state, that is very nice. But if I want to unselect such item/s from my model (through a controller) then we are not able to do so, because we cannot do:

model.SelectedItem <- null

Therefore your model need a method for clearing abstract property, for instance:

model.ClearProperty "PropertyName"

Such method could set default value to property, if it is type/object property then default value is null, am I right?

Again in similar use case scenario where you could have an UserControl with DataGrid and other controls for presenting some data, or entering input values. And now you want to populate such control with other data, you need to reset/clear all abstract properties which could be accessible only from WPF binding, before setting new values in the model.

Similar like in example above, so a method Clear/Reset abstract properties could be necessary.

method.ClearProperties()

You could generate NotifyPropertyChanged for each property when setting to default value, or you can simply NotifyPropertyChanged ""

I think that if empty string is set to PropertyChangedEventArgs then wpf refresh binding to all object properties.

dmitry-a-morozov commented 10 years ago

Why cannot you do ?

model.SelectedItem <- null

Because type of SelectedItem is non-nullable F# type?

StefanBelo commented 10 years ago

Yes

dmitry-a-morozov commented 10 years ago

Can you use Unchecked.defaultof<'T> to get null ref for F# type?

StefanBelo commented 10 years ago

Thank you Dmitry, I did not know about this function, of course this fixes this problem.

dmitry-a-morozov commented 10 years ago

You're welcome. Of course it's little hackish but I'm trying to keep public API surface to minimal and only add features I'm absolutely sure of. General theme that it's always going to be tension between F# intolerance to null values and WPF that relies on nulls heavily. Also keep in mind that you can use https://www.nuget.org/packages/PropertyChanged.Fody/ to avoid manually implementing INotifyPropertyChanged. It supports F# and similar notion of derived properties (expect it doesn't support nested paths)