Extension methods as strongly typed wrappers

suggest change

Extension 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents