Data Retrieval made easy with Query Descriptor from Intersoft ClientUI toolkit.

A few releases ago, Intersoft introduced developers to their new UXGridView. While the grid is powerful and wonderful by itself, they made it even better by introducing two classes along with it, QueryDescriptor and SortDescriptor. And thanks to the ongoing partnership between Intersoft and IdeaBlade, QueryDescriptor plays well with the Entity Framework. In previous applications using IdeaBlade, when it was necessary to have the capability for multiple search fields especially when it was unknown at compile time what they were required the use of PredicateBuilder. PredicateBuilder was a bear to use and to understand and was not as flexible as what QueryDescriptor is.

The purpose of these two classes is to build query filters and sort filets for retrieving data into the grids. I love these two classes. They have made my development work so much easier. They have reduced the number of repository methods that I need to ceate. As an example, I have an app that fills a grid with members that the user has selected in a query screen. Prior to the introduction of the QueryDescriptor, I would have had a method called GetMembers() and passed a Query Object as the first parameter The Query Object would be populated the search criteria from the user. I have an Employers screen that shows all the members that work for the employer. I would have needed a method called GetMembersForEmployer() and passed the employer number as the first parameter. I have a screen that show the job sites for each employer and the user can view a grid of members working at that job site. I would have had a method called GetMembersForJobSite() eand pasds over the job site number as the first parameter.

Now I have one method called GetMembers() and pass over the QueryDescriptor object created in that particular view model which also includes the SortDescriptor. I have just reduced the number of methods needed.

Here is an example of how I do this. I want to get fill a grid with a list of all the job sites that belong to a particular employer. In my EmployerEditViewModel class I declare a QueryDescriptor object as follows:

private QueryDescriptor _queryDescriptor;

I then create a public property QueryDescriptor that returns the _queryDescriptor:

        public QueryDescriptor QueryDescriptor
        {
            get
            {
                if (this._queryDescriptor == null)
                {
                    this.QueryDescriptor = new QueryDescriptor();
                }
                return this._queryDescriptor;
            }
            set
            {
                if (this._queryDescriptor != value)
                {
                    if (this._queryDescriptor != null)
                        this._queryDescriptor.QueryChanged -= new System.EventHandler(OnQueryChanged);

                    this._queryDescriptor = value;
                    this._queryDescriptor.QueryChanged += new System.EventHandler(OnQueryChanged);

                    this.OnPropertyChanged("QueryDescriptor");
                }
            }
        }

        private void OnQueryChanged(object sender, System.EventArgs e)
        {
       }

I then build my filter and my sort order. In this sample of code, I am going to find all the job sites that belong to the current employer on the screen and display them in a grid and sort them by the name of the job.

       public void BuildFilter()
        {

            if (this.QueryDescriptor.FilterDescriptors.Count >= 1)
                this.QueryDescriptor.FilterDescriptors.Clear();

            CompositeFilterDescriptorCollection groupFilter = new CompositeFilterDescriptorCollection();

            groupFilter.LogicalOperator = FilterCompositionLogicalOperator.And;

            groupFilter.Add(
                new FilterDescriptor()
                {
                    PropertyName = "EmployerNo",
                    Operator = FilterOperator.StartsWith,
                    Value = Employer.EmployerNo
                }
                );
            if (groupFilter.Count >= 1)
                _queryDescriptor.FilterDescriptors.Add(groupFilter);

        }

        public void BuildSortOrder()
        {
           QueryDescriptor.SortDescriptors.Add(
               new SortDescriptor()
               {
                   PropertyName = "JobName",
                   Direction = System.ComponentModel.ListSortDirection.Ascending
               }
           );
        }

Here is my fetch method in my View Model class that calls the repository method GetJobSies() and passes the QueryDescriptor object that was populated in the BuildFilter() and BuildSortOrder() methods:

      public void FetchJobSites()
        {
            IsBusy = true;

            BuildFilter();
            BuildSortOrder();

            Repository.GetJobSites(QueryDescriptor,
              (jobs) => // query success
              {
                  this.IsBusy = false;
                  ResetJobsList(jobs);
              },
              (error) => // failed
              {
                  this.IsBusy = false;
                  DisplayMessage(error.Message);
              });
        }

        private void ResetJobsList(IEnumerable<JobSite> jobs)
        {
            this.JobSites.Clear();
            jobs.ForEach(c => JobSites.Add(new JobSiteViewModel(c)));
        }

Finally here is my GetJobSites() method in my repository.

       public void GetJobSites(QueryDescriptor queryDescriptor, Action<IEnumerable<JobSite>> onSuccess = null, Action<Exception> onFail = null)
        {
            Manager.JobSites.Parse(queryDescriptor)
               .ExecuteAsync(
                   op =>
                   {
                       if (op.CompletedSuccessfully)
                       {
                           if (onSuccess != null)
                               onSuccess(op.Results);
                       }
                       else
                       {
                           if (onFail != null)
                           {
                               op.MarkErrorAsHandled();
                               onFail(op.Error);
                           }
                       }
                   }
               );

        }

Because this GetJobSites method is so generic it can be used in multiple places where I need to get job sites as long as I build the filter and sort order ahead of time in the View Model before the call to the repository.

I hope that this simple example of how to use the QueryDescriptor and SortDescriptor in a real life situation will help you to be more productive and reduce the amount of code needed.

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Data Retrieval made easy with Query Descriptor from Intersoft ClientUI toolkit.

  1. Pingback: New Silverlight Tutorials: Building business applications with ClientUI and DevForce | Intersoft Solutions Corporate Blog

  2. Pingback: ClientUI vNext Roadmap Unveiled | Intersoft Solutions Corporate Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s