I may be a little late to the party on mentioning how much extension methods have made my life easier. Extension methods tend to fill that gap between wanting to add an extra little tweak to an existing class without actually having to implement a customized inherited version. Previously whenever you wanted to use one extra tweak or feature you’d be responsible for making sure to replace all instances of the inherited class with your new version. That gets into supporting the new class when all you wanted to do was change or add one thing to an existing class!
In my case I just really wanted to stop having to check whether a ViewState value was null before trying to unbox the value into whatever value type or class I thought I stored in that key’s value field. In my case I really wanted to add generic typing support to the ViewState property of the page with as little work as possible. That is exactly where Extension methods come to the rescue.
For example normally one would see the following code to access a ViewState property:
1: private int GetGroupId()
2: {
3: int groupId = -1;
4:
5: if(ViewState["CurrentGroupId"] != null)
6: {
7: groupId = (int)ViewState["CurrentGroupId"];
8: }
9:
10: return groupId;
11: }
Ideally I would like to do the following:
1: private int GetGroupId()
2: {
3: return ViewState.Get<int>("CurrentGroupId", delegate { return -1; });
4: }
To do this I’d use a .Net extension method to extend the ViewState property through it’s StateBag class. Below you’ll find the ViewStateHelper class with all the required code to extend the StateBag class, thereby extending the ViewState property, and adding support for generic Get/Set functions. In order to use it just add the namespace where the static class is contained to the “usings” on the code-behind file of the page needing the ViewState extension support.
1: public static class ViewStateHelper
2: {
3: /// <summary>
4: /// Returns an object from view state
5: /// </summary>
6: /// <typeparam name="T">Type of object</typeparam>
7: /// <param name="key">View state key of object</param>
8: /// <param name="delegateDefault">If view state is empty, a delegate to return a value to put in view state.</param>
9: /// <returns></returns>
10: public static T Get<T>(this StateBag viewState, string key, DefaultMethod<T> delegateDefault)
11: {
12: T viewStateValue;
13:
14: if (viewState != null &&
15: viewState[key] != null &&
16: viewState[key].GetType() == typeof(T))
17: {
18: viewStateValue = (T)viewState[key];
19: }
20: else
21: {
22: viewStateValue = delegateDefault();
23: viewState.Set<T>(key, viewStateValue);
24: }
25:
26: return viewStateValue;
27: }
28:
29: /// <summary>
30: /// Put an object in view state
31: /// </summary>
32: /// <typeparam name="T">Type of object to put in view state</typeparam>
33: /// <param name="key">Key of object to put in view state</param>
34: /// <param name="value">Object to put in view state</param>
35: public static void Set<T>(this StateBag viewState, string key, T value)
36: {
37: if (value != null)
38: viewState[key] = value;
39: else
40: viewState.Remove(key);
41: }
42: }
.Net, ASP.Net, Programming