I created two extension methods for the StateBag type (ViewState) the other day and I find them quite handy so I thought I'd share them. They're for the very common task of accessing the values in the ViewState and are called GetValue and SetValue and makes my life a little easier.
Let's say we have two properties, the first is an integer called "Value" and the second a string called "Text", these would probably be implemented something like this (the differences in the implementations has to do with that the int is a value type and that string is a reference type):
public int Value { get { int result = 0; if (this.ViewState["Value"] != null) { result = (int)this.ViewState["Value"]; } return result; } set { this.ViewState["Value"] = value; } } public string Text { get { string result = (string)this.ViewState["Text"]; return result != null ? result : string.Empty; } set { this.ViewState["Text"] = value; } }
The extension methods I created let's us write this in a more straight forward way and we won't have to consider if the return type is a value type or a reference type.
public int Value { get { return this.ViewState.GetValue("Value", 0); } set { this.ViewState.SetValue("Value", value, 0); } } public string Text { get { return this.ViewState.GetValue("Text", string.Empty); } set { this.ViewState.SetValue("Text", value, string.Empty); } }
Note that there's no need to cast the return value, this because the extension methods are generic methods and type inference is used. If the default value would be null you'd have to specify the type of the method like "this.ViewState.GetValue<string>("Text", null);" or "this.ViewState.GetValue("Text", (string)null);".
And here they are, the extension methods in all their glory!
/// <summary> /// Gets a value from the view state collection. /// </summary> /// <typeparam name="T">The type of value to get.</typeparam> /// <param name="viewState">The view state collection to get the value from.</param> /// <param name="key">The key of the value in the view state.</param> /// <param name="defaultValue">The default value of the view state field.</param> /// <returns>The value from the view state.</returns> public static T GetValue<T>(this StateBag viewState, string key, T defaultValue) { object result = viewState[key]; return result == null ? defaultValue : (T)result; } /// <summary> /// Sets a value in the view state. /// </summary> /// <typeparam name="T">The type of value to set.</typeparam> /// <param name="viewState">The view state collection to set the value in.</param> /// <param name="key">The key of the value in the view state.</param> /// <param name="defaultValue">The default value of the view state field.</param> /// <param name="value">The value to set in the view state.</param> public static void SetValue<T>(this StateBag viewState, string key, T value, T defaultValue) { if (object.Equals(defaultValue, value)) { viewState.Remove(key); } else { viewState[key] = value; } }
That's all!
Interesting! Could you please re-write this to cobol?
ReplyDeleteHow much wood would a woodchuck chuck if a woodchuck could chuck wood? That's the real question.
ReplyDelete