This is a migrated thread and some comments may be shown as answers.

Reading OperatorValueFilterDescriptorBase.UnsetValue

3 Answers 103 Views
PersistenceFramework
This is a migrated thread and some comments may be shown as answers.
Pawel
Top achievements
Rank 1
Pawel asked on 02 Dec 2014, 03:22 PM
I came across a strange case when I was writing code for saving FilterDescriptors settings for RadDataFilter. The default value for string filter editor is OperatorValueFilterDescriptorBase.UnsetValue, the FilterDescriptor.Value is equal to OperatorValueFilterDescriptorBase.UnsetValue (FilterDescriptor .Value == OperatorValueFilterDescriptorBase.UnsetValue). When I save and read this value from isolated storage is not equal to OperatorValueFilterDescriptorBase.UnsetValue. See example bellow.

The following example is completely useless but it shows error.

MainWindow.xaml:
<Window x:Class="TestTelerikWpfApp.MainWindow"
        xmlns:local="clr-namespace:TestTelerikWpfApp"
        Title="MainWindow" Height="300">
    <Window.Resources>
    </Window.Resources>
    <StackPanel>
        <TextBox telerik:PersistenceManager.StorageId="textBox" />
        <StackPanel Orientation="Vertical">
            <telerik:RadButton Content="Save" Click="SaveButton_Click" />
            <telerik:RadButton Content="Read" Click="ReadButton_Click" />
        </StackPanel>
    </StackPanel>
</Window>

MainWindow.xaml.cs
namespace TestTelerikWpfApp
{
    using System.Windows;
    using System.Windows.Controls;
    using Telerik.Windows.Persistence.Services;
    using Telerik.Windows.Persistence.Storage;
 
    public partial class MainWindow : Window
    {
        private IsolatedStorageProvider isoProvider;
 
        public MainWindow()
        {
            InitializeComponent();
 
            ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(TextBox), new UnsetValuePropertyProvider());
 
            isoProvider = new IsolatedStorageProvider();
        }
 
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            isoProvider.SaveToStorage();
        }
 
        private void ReadButton_Click(object sender, RoutedEventArgs e)
        {
            isoProvider.LoadFromStorage();
        }
    }
}

For one TextBox I save or read data to isolated storage.

UnsetValuePropertyProvider.cs:
namespace TestTelerikWpfApp
{
    using Telerik.Windows.Data;
    using Telerik.Windows.Persistence.Services;
 
    class UnsetValuePropertyProvider : ICustomPropertyProvider
    {
        public CustomPropertyInfo[] GetCustomProperties()
        {
            return new CustomPropertyInfo[]
            {
                new CustomPropertyInfo("UnsetValue", typeof(UnsetValueProxy))
            };
        }
 
        public void InitializeObject(object context)
        {
        }
 
        public object InitializeValue(CustomPropertyInfo customPropertyInfo, object context)
        {
            return null;
        }
 
        public object ProvideValue(CustomPropertyInfo customPropertyInfo, object context)
        {
            return new UnsetValueProxy() { Value = OperatorValueFilterDescriptorBase.UnsetValue };
        }
 
        public void RestoreValue(CustomPropertyInfo customPropertyInfo, object context, object value)
        {
            var unsetValueProxy = value as UnsetValueProxy;
            var isUnsetValueSame = unsetValueProxy.Value == OperatorValueFilterDescriptorBase.UnsetValue; // isUnsetValueSame = false !!!
        }
    }
}

UnsetValueProxy.cs:
namespace TestTelerikWpfApp
{
    class UnsetValueProxy
    {
        public object Value { get; set; }
    }
}

In ProvideValue method I pass to proxy object OperatorValueFilterDescriptorBase.UnsetValue value and in RestoreValue unsetValueProxy.Value is not equal to OperatorValueFilterDescriptorBase.UnsetValue and it should be. See also locals tab in debug mode (example.png).

3 Answers, 1 is accepted

Sort by
0
Pawel
Top achievements
Rank 1
answered on 02 Dec 2014, 03:28 PM
I add that the problem can be easily solved by replace OperatorValueFilterDescriptorBase.UnsetValue value with null value in saving to isolated storage and in reading from isolated storage replace null value with OperatorValueFilterDescriptorBase.UnsetValue value.
0
Pavel R. Pavlov
Telerik team
answered on 04 Dec 2014, 11:07 AM
Hi Pawel,

Thank you for contacting us on that matter. I created a sample project with the snippets you provide and I confirm that the issue exists as you describe it. However, when working with the RadPersistenceFramework you should keep in mind that it persists primitive types, UIElements and collections of UIElements. What this means for your application is that you should return primitive types in the ProvideValue() method. Furthermore, based on those types you should create the object that you need to restore in the RestoreValue() method.

In your particular implementation, in the ProvideValue() method you can return the string "UnsetValue" instead of OperatorValueFilterDescriptorBase.UnsetValue. 

public object ProvideValue(CustomPropertyInfo customPropertyInfo, object context)
{
    return new UnsetValueProxy() { Value = "UnsetValue" };
    //return new UnsetValueProxy() { Value = OperatorValueFilterDescriptorBase.UnsetValue };
}
Please give this approach a try and let me know if it works in your scenario.

Regards,
Pavel R. Pavlov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Pawel
Top achievements
Rank 1
answered on 04 Dec 2014, 11:15 AM
Of course it will work. Aproach with string is similar to aproach with null value. For me, may be one or the other. Given what you said problem I described is not an error.
Tags
PersistenceFramework
Asked by
Pawel
Top achievements
Rank 1
Answers by
Pawel
Top achievements
Rank 1
Pavel R. Pavlov
Telerik team
Share this question
or