Abstract
The case: Application contains a lot of views with a list of data. Data views (lists of records) have pagination, filtering and sorting options. The user must be able to select a "sorting property" to sort the shown data by a specific property. The user-interface contains a selection control (ComboBox) the user can interact with. The combobox options represent the sorting properties that are currently available in the displayed data. It's a very standard/common case.
Example image:
The image shows a typical datalist view. The bar above the data grid shows the ComboBox. I wanted to create a class structure to design a general pagination solution. One part of my code is as follows:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
enum SortProperty {
Id, Name, Date, Balance
}
public class Program
{
public static void Main()
{
Program main = new Program();
main.Test();
}
private void Test() {
ObservableCollection<SortPropertyOption<SortProperty>> sortingOptions = new ObservableCollection<SortPropertyOption<SortProperty>>();
// The outside world can create options and use its own data type (class generic type) to represent the
// property in the data. In this case an enum is used by the outside world.
// but it could also be a string (maybe a database field name) or an integer, etc...
sortingOptions.Add(new SortPropertyOption<SortProperty>(SortProperty.Name, "Name"));
sortingOptions.Add(new SortPropertyOption<SortProperty>(SortProperty.Date, "Date"));
sortingOptions.Add(new SortPropertyOption<SortProperty>(SortProperty.Balance, "Balance"));
// SortPropertyOption collection could be passed to the view (by a binding or something).
// When an option is selected in the view, the SortPropertyOption.OptionValue can be accessed by the using-code (outside world)
// which then knows what to do.
}
}
public class SortPropertyOption<T> {
private T _optionValue;
private string _text;
public T OptionValue { get { return _optionValue; } }
public string Text { get { return _text; } }
public SortPropertyOption(T optionValue, string text) {
_optionValue = optionValue;
_text = text;
}
}
The reason for the generic in SortPropertyOption
is that the outside world that uses my classes, can use its own data type for identifying the property/column in the dataset. The datalayer can provide an enum or strings that say what properties are available in the data.
In my question, an enum is used as generic type for SortPropertyOption
. Is that acceptable?
Also, a .NET Fiddle link: https://dotnetfiddle.net/vyVWaf
[DisplayName]
or[Description]
) to be a useful way of populating GUI elements from enums.