Using the session wrapper to replace direct invocation with session variables

I use the session wrapper as follows:

public interface ISessionWrapper
{
   // ...
   CultureInfo Culture { get; set; }
}

public class SessionWrapper: ISessionWrapper
{
    private T GetFromSession<T>(string key)
    {
        return (T)HttpContext.Current.Session[key];
    }

    private void SetInSession(string key, object value)
    {
        HttpContext.Current.Session[key] = value;
    }

    // ...

    public CultureInfo Culture
    {
        get { return GetFromSession<CultureInfo>("Culture"); }
        set { SetInSession("Culture", value); }
    }
}

I can use this interface in my controller as follows:

private readonly ISessionWrapper sessionWrapper = new SessionWrapper();
// ...
ci = new CultureInfo(langName);
sessionWrapper.Culture = ci;

But how can I access this shell in the view below to replace the session variable (direct call)?

@switch (Session["Culture"].ToString()) 
{    
    case "fr":  
        // ...

    case "uk":  
        // ...
}
+3
source share
1 answer

You can use the base view:

public abstract class BaseViewPage : WebViewPage
{
    public virtual ISessionWrapper SessionWrapper
    {
        get
        {
            return new SessionWrapper();
        }
    }
}

public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
{
    public virtual ISessionWrapper SessionWrapper
    {
        get
        {
            return new SessionWrapper();
        }
    }
}

your views will have access to the SessionWrapper property.

Be sure to add the pageBaseType = "SessionWrapper" attribute to the page tag in web.config.

+2
source