Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

SelectListManager

Necros edited this page Oct 3, 2013 · 2 revisions

SelectListManager is a library used to easily manage select lists in ASP.NET MVC. Supports caching, ordering, filtering, empty first line, choosing text and value and various optimalizations. Works with any data access method that uses POCOs.

Select list configurations are created once at the start of the application, in once place. Then accessing them with a simple call does all work for you (ordering, formatting, filtering etc.).

How to use

Instalation

Download SelectListManager from nuget.org. It includes the library as well as sample config class file.

Initialization

Initialization is illustrated in the sample config class installed with the nuget package. It's located in the App_Start folder by default.

Starting point is the Initialize method.

SelectListManager.Initialize(c =>
{
	...
});

There is a number of thing you can do with the c parameter above. The most important one is setting the data provider. First you need to create a class that implements ISelectListManagerDataProvider. Here is an example that uses EntityFramework.

public class SelectListDataProvider : ISelectListManagerDataProvider
{
	public IEnumerable<T> GetData<T>() where T : class
	{
		using (var context = new TestContext())
		{
			return context.Set<T>().ToList();
		}
	}
}

Once that is done, add the configuration.

c.SetDataProviderFactory(() => new SelectListDataProvider());

You also may want to use dependency injection instead of just newing up the data provider. Here's an example using Ninject. You obviously need to pass the kernel as a parameter to the configuration method.

c.SetDataProviderFactory(() => kernel.Get<ISelectListManagerDataProvider>())

Other settings are optional.

Cache duration - a number of seconds that base data for the select lists stay in cache (default is 1440).

c.CacheDuration = ...;

Cache provider - an implementation of cache (default uses ASP.NET Cache)

c.SetCacheProviderType(typeof(...));

Default null line text - text of first empty line (with null value) in a select list (default is the string "--")

c.DefaultNullLineText = ...;

Select list registration

There are two ways to register select lists. Either through initialization or later dynamically.

c.Registry.Add<Test1>(i => i.Name, i => i.Id);

or

SelectListManager.Registry.Add<Test1>(i => i.Name, i => i.Id);

There is a number of options you can choose to configure select list to act exactly as needed.

Parameter: textSelector required - A function that transforms an entity into text representing the options in select list.
Parameter: valueSelector required - A function that transforms an entity into value of the option in select list.
Parameter: key - Sometimes there can be need for different select list configurations of the same entity. The key is used to differentiate between such configurations.
Parameter: predicate - Enables filtering before the select list is created.
Parameter: orderBySelector - Enables ordering before the select list is created.
Parameter: nullLine (default false) - Defines whether to insert an empty line representing null to the first position of the select list.
Parameter: nullLineText (defaults to the text in configuration) - The text of the null line.
Parameter: noCache (default false) - Option to disable caching.
Parameter: optimization - (default None) - An optimization can be used to preselect a value or remove a null line.

Options for optimization are (the names are self-explanatory, so no commentary is needed): DeleteNullLineIfSelectListHasOneItemAndNullLine
PreselectIfSelectListHasOneItemAndNullLine
AlwaysPreselectFirstItem

There is also an option to remove a select list configuration if ever needed:

SelectListManager.Registry.Remove<T>();

Usage

Once everything is initialized, using SelectListManager is very easy.

ISelectListManager selectListManager // = new SelectListManager, or inject using DI
var items = selectListManager.GetItems<Test1>();

The GetItems<T> method returns an IEnumerable<SelectListItem> which is a standard select list representation in ASP.NET MVC. It also has some useful parameters:
Paramater: key - See key in registration, use this parameter to get different configurations of select lists for the same T.
Parameter: selectedValue - Sets the select list item with this value as selected (sets the property Selected to true.
Parameter: predicate - An additional filter before the select list is created. This parameter does not override the predicate from the configuration, but is applied after it, and it's not cached (the caching occurs before this filter is applied).
Parameter: customData - If this is specified, SelectListManager does not load the data for the select list using a data provider, but instead uses data from this parameter. The data is not cached. This should only be used in special circumstances when there is something preventing the default data provider use.

Clone this wiki locally