I have to implement a ‘global’ search/filter function in my application. Each window that contains a list of information, whether it’s a DataGrid or some other implementation of a list has to have a search box, and if the user enters text in the search box, it’ll filter that list by whatever’s being searched for. I only want to implement the search logic once.
For the most part, this won’t necessarily be too difficult. The reason is that most windows that contain a list will be based on the same datatype. These are all ViewModels, and each of these ViewModels extends ViewModelBase, and ViewModelBase contains the data that I’ll be searching.
A rudimentary example:
public class ZoneVm : ViewModelBase
{
// Zone specific functionality
}
public class UserVm : ViewModelBase
{
// User specific functionality
}
public class ViewModelBase : INotifyPropertyChanged
{
public string Name { get; set; }
public int Value { get; set; }
// The handy thing about the common view model base is that
// I can contain the model binding in a single place
// It will be easy to search List<ZoneVm> and List<UserVm> because
// they are both effectively List<ViewModelBase>.
}
The difficulty lies in the outlier objects that I have to search. Some windows contain a list of objects that don’t extend ViewModelBase, so I won’t have this predictable list of properties to search, e.g.
public class PanelData // doesn't implement ViewModelBase :-(
{
public int SerialNumber { get; set; }
public Customer Customer { get; set; }
// other properties that I'll have to search/filter on
}
Is there a ‘best practices’ approach to this kind of task? Are there design patterns that solve this problem? How should I approach having to search/filter 2 (and probably more) different kinds of list?
I think you will not want to have each data to have a common searchable member?
(this could be handeled by using a common IData-Interface with this abstract member as Ofer said)
I would have them in a queriable collection and implement the search in an abstract fasion (please fill in the blanks yourself):
You can then use this as a collection for your data and provide Commands and Properties for the filtering/searching.