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

DataFilter Persisted

4 Answers 308 Views
PersistenceFramework
This is a migrated thread and some comments may be shown as answers.
Guru
Top achievements
Rank 2
Guru asked on 30 Jan 2013, 10:47 PM
Is it possible to persist a RadDataFilter and all of its filter descriptors?

We have a large application with many controls and just need to persist the datafilter within it.
We tried some user submitted coded to iterate filter descriptors but it constantly has issues with different member types...
Can the persistence framework save our filter and all of its descriptors so we can reload it and reattach binding to the gridview it filters?

4 Answers, 1 is accepted

Sort by
0
Guru
Top achievements
Rank 2
answered on 31 Jan 2013, 03:27 AM
I think I found the basic solution in one of your Silverlight forums:
ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(RadDataFilter), new FilterDescriptorCustomPropertyProvider());

What would be the best way to save and load as an xml string with persistence framework?

Doing it like this for now:

//************************************* XAML

<telerik:RadDataFilter Name="datafilter" Grid.Row="1" telerik:PersistenceManager.StorageId="datafilter"/>

//*********** Init with control/window

ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(RadDataFilter), new FilterDescriptorCustomPropertyProvider());


//*************** Methods to save/pull as xml string

        private void Save(object sender, RoutedEventArgs e)
        {
            PersistenceManager manager = new PersistenceManager();
            using (Stream s = manager.Save(datafilter))
            {
                s.Seek(0, SeekOrigin.Begin);
                StreamReader r = new StreamReader(s);
                xml = r.ReadToEnd(); //xml saved to db
            }
        }

        private void Load(object sender, RoutedEventArgs e)
        {
            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) //xml pulled from db
            {
                PersistenceManager manager = new PersistenceManager();
                stream.Position = 0L;
                manager.Load(datafilter, stream);
            }
        }

//**************  Proxy class

    public class FilterDescriptorCustomPropertyProvider : ICustomPropertyProvider
    {
        public CustomPropertyInfo[] GetCustomProperties()
        {
            return new CustomPropertyInfo[]
            {
                new CustomPropertyInfo("LogicalOperator", typeof(FilterCompositionLogicalOperator)),
                new CustomPropertyInfo("FilterDescriptors", typeof(CompositeFilterDescriptorCollection)),
            };
        }

        public void InitializeObject(object context)
        {
            RadDataFilter filter = context as RadDataFilter;
            filter.FilterDescriptors.Clear();
        }

        public object InitializeValue(CustomPropertyInfo customPropertyInfo, object context)
        {
            return null;
        }

        public object ProvideValue(CustomPropertyInfo customPropertyInfo, object context)
        {
            RadDataFilter filter = context as RadDataFilter;
            if (customPropertyInfo.Name == "LogicalOperator")
            {
                return filter.ViewModel.CompositeFilter.LogicalOperator;
            }
            if (customPropertyInfo.Name == "FilterDescriptors")
            {
                return filter.FilterDescriptors;
            }
            return null;
        }

        public void RestoreValue(CustomPropertyInfo customPropertyInfo, object context, object value)
        {
            RadDataFilter filter = context as RadDataFilter;

            if (customPropertyInfo.Name == "LogicalOperator")
            {
                filter.ViewModel.CompositeFilter.LogicalOperator = (FilterCompositionLogicalOperator)value;
            }
            if (customPropertyInfo.Name == "FilterDescriptors")
            {
                CompositeFilterDescriptorCollection filters = value as CompositeFilterDescriptorCollection;
                if (filter != null)
                {
                    foreach (var item in filters)
                    {
                        filter.FilterDescriptors.Add(item);
                    }
                }
            }
        }
    }

0
Tina Stancheva
Telerik team
answered on 04 Feb 2013, 01:33 PM
Hi Zack,

The approach you've chosen is correct. Basically the RadPersistenceFramework needs a CustomPropertyProvider to understand how to persist complex controls like the RadDataFilter. In the CustomPropertyProvider, you need to set the list of properties that have to be persisted by the framework. This logic should be placed in the GetCustomProperties() method. Then you should customize the ProvideValue() method to get the values of the appropriate RadDataFilter properties and pass them to the PersistenceManager. The RestoreValue() method, on the other hand, should determine how to use the persisted values to restore the saved state of the RadDataFilter.

It is also important to note that the RadPersistenceFramework can save the persisted settings either in a file in the isolated storage or in a stream. This is why in your scenario, using the Save(object), Load(object,stream) methods is the better approach - you can use a StreamReader to read the contents of the stream and write the data in an xml file.

I hope this information helps you better understand the RadPersistenceFramework logic. Please let us know if you have any additional questions or if you encounter any issues while implementing your scenario.

Kind regards,
Tina Stancheva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
sebastien
Top achievements
Rank 1
answered on 04 Mar 2016, 08:01 AM

Hello,

I've use this solution to save the filtor of my DataFilter but it doesn't work for custom filter ( not raise the rEditorCreatedEvent and the FilterOperatorsLoadingEvent).

Is it possible to persist the custom filter? (the save is good and the filters are correctly load but can't be display).

best regards,

Sébastien

 

0
Kiril Vandov
Telerik team
answered on 09 Mar 2016, 06:52 AM
Hello Sébastien,

The RadPersistenceFramework should not have any problem saving and then loading any Properties as long as they have a public getters/setters in order to read and then write their values. As the entire save/load logic should be located in your CustomPropertyProvider I would like you to provide us with a sample project and steps reproducing the explained behavior. Doing so we will be able to better understand your scenario and provide you with the best possible answer based on your scenario. Please note that the RadPersistenceFramework cant persist events and event firing.

Looking forward to hearing from you.

Kind regards,
Kiril Vandov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
PersistenceFramework
Asked by
Guru
Top achievements
Rank 2
Answers by
Guru
Top achievements
Rank 2
Tina Stancheva
Telerik team
sebastien
Top achievements
Rank 1
Kiril Vandov
Telerik team
Share this question
or