Extension methods as strongly typed wrappers
suggest changeExtension methods can wrap dictionary-like objects in a strongly typed API. A cache and HttpContext.Items are two examples.
public static class CacheExtensions
{
public static void SetUserInfo(this Cache cache, UserInfo data) =>
cache["UserInfo"] = data;
public static UserInfo GetUserInfo(this Cache cache) =>
cache["UserInfo"] as UserInfo;
}
Callers no longer repeat the string key or cast the stored object. The dictionary itself stays loosely typed.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents